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

Hotfix: Fix Routing Form Saving related issue #4954

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 24 additions & 10 deletions packages/app-store/ee/routing-forms/components/SingleForm.tsx
Expand Up @@ -175,35 +175,32 @@ const Actions = ({
);
};

Copy link
Member Author

Choose a reason for hiding this comment

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

Will fix Eslint warning in a follow up PR>

export default function SingleForm({
form,
appUrl,
Page,
}: {
type SingleFormComponentProps = {
form: RoutingFormWithResponseCount;
appUrl: string;
Page: React.FC<{
form: RoutingFormWithResponseCount;
appUrl: string;
hookForm: UseFormReturn<RoutingFormWithResponseCount>;
}>;
}) {
};

function SingleForm({ form, appUrl, Page }: SingleFormComponentProps) {
const utils = trpc.useContext();

const hookForm = useForm({
defaultValues: form,
});
const utils = trpc.useContext();
const router = useRouter();

const mutation = trpc.useMutation("viewer.app_routing_forms.formMutation", {
onSuccess() {
router.replace(router.asPath);
showToast("Form updated successfully.", "success");
},
onError() {
showToast(`Something went wrong`, "error");
},
onSettled() {
utils.invalidateQueries(["viewer.app_routing_forms.formQuery"]);
utils.invalidateQueries(["viewer.app_routing_forms.formQuery", { id: form.id }]);
},
});
return (
Expand Down Expand Up @@ -262,3 +259,20 @@ export default function SingleForm({
</Form>
);
}

export default function SingleFormWrapper({ form: _form, ...props }: SingleFormComponentProps) {
const { data: form, isLoading } = trpc.useQuery(["viewer.app_routing_forms.formQuery", { id: _form.id }], {
Copy link
Member Author

Choose a reason for hiding this comment

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

Because we are using tRPC now, it is important that useForm is called in a child component which is rendered only when form is available. THis is to avoid initialization of useForm with defaultValues:undefined

initialData: _form,
});
const { t } = useLocale();

if (isLoading) {
// It shouldn't be possible because we are passing the data from SSR to it as initialData. So, no need for skeleton here
return null;
}

if (!form) {
throw new Error(t("something_went_wrong"));
}
return <SingleForm form={form} {...props} />;
}
13 changes: 12 additions & 1 deletion packages/app-store/ee/routing-forms/trpc-router.ts
Expand Up @@ -175,9 +175,20 @@ const app_RoutingForms = createRouter()
userId: user.id,
id: input.id,
},
include: {
_count: {
select: {
responses: true,
},
},
},
});

return form;
if (!form) {
return null;
}

return getSerializableForm(form);
},
})
.mutation("formMutation", {
Expand Down