Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dashboard): Fix stale data on test workflow page #7245

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions apps/dashboard/src/components/workflow-editor/nodes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { STEP_TYPE_TO_COLOR } from '@/utils/color';
import { useWorkflow } from '@/components/workflow-editor/workflow-provider';
import { WorkflowOriginEnum } from '@novu/shared';
import { createStep } from '@/components/workflow-editor/steps/step-provider';
import { getEncodedId, STEP_DIVIDER } from '@/utils/step';
import { getWorkflowIdFromSlug, STEP_DIVIDER } from '@/utils/step';

export type NodeData = {
name?: string;
Expand Down Expand Up @@ -54,8 +54,8 @@ const StepNode = (props: StepNodeProps) => {
}>();

const isSelected =
getEncodedId({ slug: stepSlug ?? '', divider: STEP_DIVIDER }) ===
getEncodedId({ slug: data.stepSlug ?? '', divider: STEP_DIVIDER });
getWorkflowIdFromSlug({ slug: stepSlug ?? '', divider: STEP_DIVIDER }) ===
getWorkflowIdFromSlug({ slug: data.stepSlug ?? '', divider: STEP_DIVIDER });

return <Node aria-selected={isSelected} className={cn('group', className)} {...rest} />;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { StepDataDto, StepIssuesDto, StepTypeEnum } from '@novu/shared';
import { createContextHook } from '@/utils/context';
import { Step } from '@/utils/types';
import { STEP_DIVIDER } from '@/utils/step';
import { getEncodedId } from '@/utils/step';
import { getWorkflowIdFromSlug } from '@/utils/step';
import { useWorkflow } from '@/components/workflow-editor/workflow-provider';

export type StepEditorContextType = {
Expand Down Expand Up @@ -40,8 +40,8 @@ export const StepProvider = ({ children }: { children: ReactNode }) => {
const issues = useMemo(() => {
const newIssues = workflow?.steps.find(
(s) =>
getEncodedId({ slug: s.slug, divider: STEP_DIVIDER }) ===
getEncodedId({ slug: stepSlug, divider: STEP_DIVIDER })
getWorkflowIdFromSlug({ slug: s.slug, divider: STEP_DIVIDER }) ===
getWorkflowIdFromSlug({ slug: stepSlug, divider: STEP_DIVIDER })
)?.issues;

return { ...newIssues };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import { useMemo } from 'react';
import { Link, useParams } from 'react-router-dom';
import { RiPlayCircleLine } from 'react-icons/ri';
import { useForm } from 'react-hook-form';
import { RiPlayCircleLine } from 'react-icons/ri';
import { Link, useParams } from 'react-router-dom';
// eslint-disable-next-line
// @ts-ignore
import { ToastClose, ToastIcon } from '@/components/primitives/sonner';
import { useFetchWorkflow } from '@/hooks/use-fetch-workflow';
import { useTriggerWorkflow } from '@/hooks/use-trigger-workflow';
import { buildRoute, LEGACY_ROUTES, ROUTES } from '@/utils/routes';
import { zodResolver } from '@hookform/resolvers/zod';
import type { WorkflowTestDataResponseDto } from '@novu/shared';
import { Tabs, TabsContent, TabsList, TabsTrigger } from '../../primitives/tabs';
import { buildRoute, LEGACY_ROUTES, ROUTES } from '@/utils/routes';
import { useFetchWorkflow } from '@/hooks/use-fetch-workflow';
import { Form } from '../../primitives/form/form';
import { toast } from 'sonner';
import { Button } from '../../primitives/button';
import { useTriggerWorkflow } from '@/hooks/use-trigger-workflow';
import { Form } from '../../primitives/form/form';
import { showToast } from '../../primitives/sonner-helpers';
import { Tabs, TabsContent, TabsList, TabsTrigger } from '../../primitives/tabs';
import { buildDynamicFormSchema, makeObjectFromSchema, TestWorkflowFormType } from '../schema';
import { TestWorkflowForm } from './test-workflow-form';
import { toast } from 'sonner';
import { ToastClose, ToastIcon } from '@/components/primitives/sonner';

export const TestWorkflowTabs = ({ testData }: { testData: WorkflowTestDataResponseDto }) => {
const { environmentSlug = '', workflowSlug = '' } = useParams<{ environmentSlug: string; workflowSlug: string }>();

const { workflow } = useFetchWorkflow({
workflowSlug,
});
Expand All @@ -37,8 +38,9 @@ export const TestWorkflowTabs = ({ testData }: { testData: WorkflowTestDataRespo
const form = useForm<TestWorkflowFormType>({
mode: 'onSubmit',
resolver: zodResolver(buildDynamicFormSchema({ to: testData?.to ?? {} })),
defaultValues: { to, payload: JSON.stringify(payload, null, 2) },
values: { to, payload: JSON.stringify(payload, null, 2) },
});

const { handleSubmit } = form;
const { triggerWorkflow, isPending } = useTriggerWorkflow();

Expand Down Expand Up @@ -105,7 +107,7 @@ export const TestWorkflowTabs = ({ testData }: { testData: WorkflowTestDataRespo
<Form {...form}>
<form onSubmit={handleSubmit(onSubmit)} className="roun flex h-full flex-1 flex-nowrap">
<Tabs defaultValue="workflow" className="-mt-[1px] flex flex-1 flex-col" value="trigger">
<TabsList variant="regular">
<TabsList variant="regular" className="items-center">
<TabsTrigger value="workflow" asChild variant="regular">
<Link
to={buildRoute(ROUTES.EDIT_WORKFLOW, {
Expand Down
6 changes: 3 additions & 3 deletions apps/dashboard/src/hooks/use-fetch-step.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { StepDataDto, StepUpdateDto } from '@novu/shared';
import { QueryKeys } from '@/utils/query-keys';
import { useEnvironment } from '@/context/environment/hooks';
import { getStep } from '@/api/steps';
import { getEncodedId, STEP_DIVIDER, WORKFLOW_DIVIDER } from '@/utils/step';
import { getWorkflowIdFromSlug, STEP_DIVIDER, WORKFLOW_DIVIDER } from '@/utils/step';

export const useFetchStep = ({ workflowSlug, stepSlug }: { workflowSlug: string; stepSlug: string }) => {
const client = useQueryClient();
Expand All @@ -14,8 +14,8 @@ export const useFetchStep = ({ workflowSlug, stepSlug }: { workflowSlug: string;
() => [
QueryKeys.fetchWorkflow,
currentEnvironment?._id,
getEncodedId({ slug: workflowSlug, divider: WORKFLOW_DIVIDER }),
getEncodedId({ slug: stepSlug, divider: STEP_DIVIDER }),
getWorkflowIdFromSlug({ slug: workflowSlug, divider: WORKFLOW_DIVIDER }),
getWorkflowIdFromSlug({ slug: stepSlug, divider: STEP_DIVIDER }),
],
[currentEnvironment?._id, workflowSlug, stepSlug]
);
Expand Down
12 changes: 10 additions & 2 deletions apps/dashboard/src/hooks/use-fetch-workflow-test-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@ import type { WorkflowTestDataResponseDto } from '@novu/shared';
import { QueryKeys } from '@/utils/query-keys';
import { getWorkflowTestData } from '@/api/workflows';
import { useEnvironment } from '@/context/environment/hooks';
import { getWorkflowIdFromSlug, WORKFLOW_DIVIDER } from '@/utils/step';

export const useFetchWorkflowTestData = ({ workflowSlug }: { workflowSlug?: string }) => {
export const useFetchWorkflowTestData = ({ workflowSlug }: { workflowSlug: string }) => {
const { currentEnvironment } = useEnvironment();
const { data, isPending, error } = useQuery<WorkflowTestDataResponseDto>({
queryKey: [QueryKeys.fetchWorkflowTestData, currentEnvironment?._id, workflowSlug],
queryKey: [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@desiprisg I think the test data page is better if we don't cache it at all. It's a separate tab, so I don't see the value of caching. We'd rather fetch data every time we visit the tab instead of caching and invalidating.

Copy link
Contributor Author

@desiprisg desiprisg Dec 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure we can set gcTime to 0 to achieve this. queryKey remains required though, in order to trigger a refetch if an invalidation were to happen while the query is still used.

QueryKeys.fetchWorkflowTestData,
currentEnvironment?._id,
getWorkflowIdFromSlug({ slug: workflowSlug, divider: WORKFLOW_DIVIDER }),
],
queryFn: () => getWorkflowTestData({ environment: currentEnvironment!, workflowSlug }),
enabled: !!currentEnvironment?._id && !!workflowSlug,
gcTime: 0,
});

console.log(data);

return {
testData: data,
isPending,
Expand Down
4 changes: 2 additions & 2 deletions apps/dashboard/src/hooks/use-fetch-workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import type { WorkflowResponseDto } from '@novu/shared';
import { QueryKeys } from '@/utils/query-keys';
import { getWorkflow } from '@/api/workflows';
import { useEnvironment } from '@/context/environment/hooks';
import { getEncodedId, WORKFLOW_DIVIDER } from '@/utils/step';
import { getWorkflowIdFromSlug, WORKFLOW_DIVIDER } from '@/utils/step';

export const useFetchWorkflow = ({ workflowSlug }: { workflowSlug?: string }) => {
const { currentEnvironment } = useEnvironment();
const workflowId = useMemo(
() => getEncodedId({ slug: workflowSlug ?? '', divider: WORKFLOW_DIVIDER }),
() => getWorkflowIdFromSlug({ slug: workflowSlug ?? '', divider: WORKFLOW_DIVIDER }),
[workflowSlug]
);

Expand Down
15 changes: 6 additions & 9 deletions apps/dashboard/src/hooks/use-update-workflow.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getEncodedId, WORKFLOW_DIVIDER } from '@/utils/step';
import { getWorkflowIdFromSlug, WORKFLOW_DIVIDER } from '@/utils/step';
import { OmitEnvironmentFromParameters } from '@/utils/types';
import { QueryKeys } from '@/utils/query-keys';
import { updateWorkflow } from '@/api/workflows';
Expand All @@ -18,14 +18,11 @@ export const useUpdateWorkflow = (
mutationFn: (args: UpdateWorkflowParameters) => updateWorkflow({ environment: currentEnvironment!, ...args }),
...options,
onSuccess: async (data, variables, context) => {
await queryClient.setQueryData(
[
QueryKeys.fetchWorkflow,
currentEnvironment?._id,
getEncodedId({ slug: data.slug, divider: WORKFLOW_DIVIDER }),
],
data
);
const workflowId = getWorkflowIdFromSlug({ slug: data.slug, divider: WORKFLOW_DIVIDER });
await queryClient.setQueryData([QueryKeys.fetchWorkflow, currentEnvironment?._id, workflowId], data);
await queryClient.invalidateQueries({
queryKey: [QueryKeys.fetchWorkflowTestData, currentEnvironment?._id, workflowId],
});
options?.onSuccess?.(data, variables, context);
},
});
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/src/utils/step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ShortIsPrefixEnum } from '@novu/shared';
export const WORKFLOW_DIVIDER = `_${ShortIsPrefixEnum.WORKFLOW}`;
export const STEP_DIVIDER = `_${ShortIsPrefixEnum.STEP}`;

export const getEncodedId = ({ slug, divider }: { slug: string; divider: string }) => {
export const getWorkflowIdFromSlug = ({ slug, divider }: { slug: string; divider: string }) => {
const parts = slug.split(divider);
return parts[parts.length - 1];
};
Loading