Skip to content

Commit

Permalink
Switch to using tRPC as router.push weirdly doesnt refetch when using…
Browse files Browse the repository at this point in the history
… middleware and that too after build (#4954)
  • Loading branch information
hariombalhara committed Oct 11, 2022
1 parent 2c6c768 commit 7e5c686
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
34 changes: 24 additions & 10 deletions packages/app-store/ee/routing-forms/components/SingleForm.tsx
Expand Up @@ -175,35 +175,32 @@ const Actions = ({
);
};

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 }], {
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

0 comments on commit 7e5c686

Please sign in to comment.