Skip to content

Commit

Permalink
fix(dashboard): Fix stale data on test workflow page
Browse files Browse the repository at this point in the history
  • Loading branch information
desiprisg committed Dec 9, 2024
1 parent 64a1c99 commit ecb497a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMemo } from 'react';
import { useEffect, useMemo, useState } from 'react';
import { Link, useParams } from 'react-router-dom';
import { RiPlayCircleLine } from 'react-icons/ri';
import { useForm } from 'react-hook-form';
Expand All @@ -20,6 +20,7 @@ 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
9 changes: 7 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,11 +3,16 @@ import type { WorkflowTestDataResponseDto } from '@novu/shared';
import { QueryKeys } from '@/utils/query-keys';
import { getWorkflowTestData } from '@/api/workflows';
import { useEnvironment } from '@/context/environment/hooks';
import { getEncodedId, 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: [
QueryKeys.fetchWorkflowTestData,
currentEnvironment?._id,
getEncodedId({ slug: workflowSlug, divider: WORKFLOW_DIVIDER }),
],
queryFn: () => getWorkflowTestData({ environment: currentEnvironment!, workflowSlug }),
enabled: !!currentEnvironment?._id && !!workflowSlug,
});
Expand Down
13 changes: 5 additions & 8 deletions apps/dashboard/src/hooks/use-update-workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = getEncodedId({ 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: 2 additions & 0 deletions apps/dashboard/src/pages/test-workflow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export const TestWorkflowPage = () => {
});
const { testData } = useFetchWorkflowTestData({ workflowSlug });

console.log(testData);

return (
<>
<PageMeta title={`Trigger ${workflow?.name}`} />
Expand Down

0 comments on commit ecb497a

Please sign in to comment.