Skip to content

Commit f6039d0

Browse files
committed
Refactor PurchaseSeatsModal to use useFetcher for isolated state and cross-route compatibility
1 parent a03061e commit f6039d0

File tree

1 file changed

+19
-15
lines changed
  • apps/webapp/app/routes/_app.orgs.$organizationSlug.settings.team

1 file changed

+19
-15
lines changed

apps/webapp/app/routes/_app.orgs.$organizationSlug.settings.team/route.tsx

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import { $replica } from "~/db.server";
4242
import { useOrganization } from "~/hooks/useOrganizations";
4343
import { useUser } from "~/hooks/useUser";
4444
import { removeTeamMember } from "~/models/member.server";
45-
import { redirectWithErrorMessage, redirectWithSuccessMessage } from "~/models/message.server";
45+
import { redirectWithSuccessMessage } from "~/models/message.server";
4646
import { TeamPresenter } from "~/presenters/TeamPresenter.server";
4747
import { requireUserId } from "~/services/session.server";
4848
import {
@@ -118,15 +118,13 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
118118
const formType = formData.get("_formType");
119119

120120
if (formType === "purchase-seats") {
121-
const redirectPath = organizationTeamPath({ slug: organizationSlug });
122-
123121
const org = await $replica.organization.findFirst({
124122
where: { slug: organizationSlug },
125123
select: { id: true },
126124
});
127125

128126
if (!org) {
129-
throw redirectWithErrorMessage(redirectPath, request, "Organization not found");
127+
return json({ ok: false, error: "Organization not found" } as const);
130128
}
131129

132130
const submission = parse(formData, { schema: PurchaseSchema });
@@ -155,13 +153,7 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
155153
return json(submission);
156154
}
157155

158-
return redirectWithSuccessMessage(
159-
redirectPath,
160-
request,
161-
submission.value.action === "purchase"
162-
? "Seats updated successfully"
163-
: "Requested extra seats, we'll get back to you soon."
164-
);
156+
return json({ ok: true } as const);
165157
}
166158

167159
const submission = parse(formData, { schema });
@@ -576,25 +568,37 @@ export function PurchaseSeatsModal({
576568
}) {
577569
const fetcher = useFetcher();
578570
const organization = useOrganization();
571+
const lastSubmission =
572+
fetcher.data && typeof fetcher.data === "object" && "intent" in fetcher.data
573+
? fetcher.data
574+
: undefined;
579575
const [form, { amount }] = useForm({
580576
id: "purchase-seats",
581-
lastSubmission: fetcher.data as any,
577+
lastSubmission: lastSubmission as any,
582578
onValidate({ formData }) {
583579
return parse(formData, { schema: PurchaseSchema });
584580
},
585581
shouldRevalidate: "onSubmit",
586582
});
587583

588584
const [amountValue, setAmountValue] = useState(extraSeats);
585+
useEffect(() => {
586+
setAmountValue(extraSeats);
587+
}, [extraSeats]);
589588
const isLoading = fetcher.state !== "idle";
590589

591590
const [open, setOpen] = useState(false);
592-
const prevFetcherState = useRef(fetcher.state);
593591
useEffect(() => {
594-
if (prevFetcherState.current !== "idle" && fetcher.state === "idle" && !fetcher.data) {
592+
const data = fetcher.data;
593+
if (
594+
fetcher.state === "idle" &&
595+
data !== null &&
596+
typeof data === "object" &&
597+
"ok" in data &&
598+
data.ok
599+
) {
595600
setOpen(false);
596601
}
597-
prevFetcherState.current = fetcher.state;
598602
}, [fetcher.state, fetcher.data]);
599603

600604
const state = updateSeatState({

0 commit comments

Comments
 (0)