Skip to content

Commit 129b29c

Browse files
committed
Refactor to use useFetcher instead
1 parent d25900d commit 129b29c

File tree

1 file changed

+16
-21
lines changed
  • apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.branches

1 file changed

+16
-21
lines changed

apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.branches/route.tsx

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { parse } from "@conform-to/zod";
33
import { ArrowUpCircleIcon, CheckIcon, EnvelopeIcon, PlusIcon } from "@heroicons/react/20/solid";
44
import { BookOpenIcon } from "@heroicons/react/24/solid";
55
import { DialogClose } from "@radix-ui/react-dialog";
6-
import { Form, useActionData, useLocation, useNavigation, useSearchParams } from "@remix-run/react";
6+
import { Form, useActionData, useFetcher, useLocation, useSearchParams } from "@remix-run/react";
77
import { type ActionFunctionArgs, json, type LoaderFunctionArgs } from "@remix-run/server-runtime";
88
import { GitMeta, tryCatch } from "@trigger.dev/core/v3";
99
import { useCallback, useEffect, useState } from "react";
@@ -184,13 +184,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
184184
return json(submission);
185185
}
186186

187-
return redirectWithSuccessMessage(
188-
`${redirectPath}?purchaseSuccess=true`,
189-
request,
190-
submission.value.action === "purchase"
191-
? "Preview branches updated successfully"
192-
: "Requested extra preview branches, we'll get back to you soon."
193-
);
187+
return json({ ok: true } as const);
194188
}
195189

196190
const submission = parse(formData, { schema });
@@ -654,7 +648,11 @@ function PurchaseBranchesModal({
654648
planBranchLimit: number;
655649
triggerButton?: React.ReactNode;
656650
}) {
657-
const lastSubmission = useActionData();
651+
const fetcher = useFetcher();
652+
const lastSubmission =
653+
fetcher.data && typeof fetcher.data === "object" && "intent" in fetcher.data
654+
? fetcher.data
655+
: undefined;
658656
const [form, { amount }] = useForm({
659657
id: "purchase-branches",
660658
lastSubmission: lastSubmission as any,
@@ -665,21 +663,18 @@ function PurchaseBranchesModal({
665663
});
666664

667665
const [amountValue, setAmountValue] = useState(extraBranches);
668-
const navigation = useNavigation();
669-
const isLoading = navigation.state !== "idle" && navigation.formMethod === "POST";
666+
useEffect(() => {
667+
setAmountValue(extraBranches);
668+
}, [extraBranches]);
669+
const isLoading = fetcher.state !== "idle";
670670

671-
const [searchParams, setSearchParams] = useSearchParams();
672-
const purchaseSuccess = searchParams.get("purchaseSuccess");
673671
const [open, setOpen] = useState(false);
674672
useEffect(() => {
675-
if (purchaseSuccess) {
673+
const data = fetcher.data;
674+
if (fetcher.state === "idle" && data !== null && typeof data === "object" && "ok" in data && data.ok) {
676675
setOpen(false);
677-
setSearchParams((s) => {
678-
s.delete("purchaseSuccess");
679-
return s;
680-
});
681676
}
682-
}, [purchaseSuccess, setSearchParams]);
677+
}, [fetcher.state, fetcher.data]);
683678

684679
const state = updateBranchState({
685680
value: amountValue,
@@ -705,7 +700,7 @@ function PurchaseBranchesModal({
705700
</DialogTrigger>
706701
<DialogContent>
707702
<DialogHeader>{title}</DialogHeader>
708-
<Form method="post" {...form.props}>
703+
<fetcher.Form method="post" {...form.props}>
709704
<input type="hidden" name="_formType" value="purchase-branches" />
710705
<div className="flex flex-col gap-4 pt-2">
711706
<div className="flex flex-col gap-1">
@@ -861,7 +856,7 @@ function PurchaseBranchesModal({
861856
</DialogClose>
862857
}
863858
/>
864-
</Form>
859+
</fetcher.Form>
865860
</DialogContent>
866861
</Dialog>
867862
);

0 commit comments

Comments
 (0)