From 769a9ea8b446aa8c6b89ec9459daf05dd26ed64d Mon Sep 17 00:00:00 2001 From: ap-justin <89639563+ap-justin@users.noreply.github.com> Date: Fri, 18 Oct 2024 20:13:59 +0800 Subject: [PATCH] sign error with fetcher --- .../Registration/SigningResult/ErrorPage.tsx | 39 ++----- .../Steps/FSAInquiry/useSubmit.ts | 27 ++--- src/pages/Registration/Steps/fsa-action.ts | 12 +- src/pages/Registration/index.tsx | 7 +- src/services/aws/aws.ts | 1 - src/services/aws/registration/index.ts | 1 - src/services/aws/registration/registration.ts | 105 ------------------ 7 files changed, 37 insertions(+), 155 deletions(-) delete mode 100644 src/services/aws/registration/index.ts delete mode 100644 src/services/aws/registration/registration.ts diff --git a/src/pages/Registration/SigningResult/ErrorPage.tsx b/src/pages/Registration/SigningResult/ErrorPage.tsx index 54457bf3d5..c87b5097e1 100644 --- a/src/pages/Registration/SigningResult/ErrorPage.tsx +++ b/src/pages/Registration/SigningResult/ErrorPage.tsx @@ -1,37 +1,15 @@ import Icon from "components/Icon"; import { appRoutes } from "constants/routes"; import { regRoutes } from "constants/routes"; -import { useErrorContext } from "contexts/ErrorContext"; -import { useState } from "react"; -import { Link } from "react-router-dom"; -import { useFiscalSponsorshipAgreementSigningURLMutation } from "services/aws/registration"; +import { Link, useFetcher } from "react-router-dom"; import type { ErrorQueryParams } from "./types"; -const initialText = "Retry"; -const redirectingText = "Redirecting..."; - export default function ErrorPage(props: ErrorQueryParams) { - const { handleError } = useErrorContext(); - const [generateSigningURL] = - useFiscalSponsorshipAgreementSigningURLMutation(); - - //use standalone state, as mutation state ends before redirect - const [submitText, setSubmitText] = useState(initialText); - - async function proceed(signerEID: string) { - try { - setSubmitText(redirectingText); - const { url } = await generateSigningURL(signerEID).unwrap(); - window.location.href = url; - } catch (err) { - setSubmitText(initialText); - handleError(err, { context: "generating signing url" }); - } - } + const fetcher = useFetcher(); - const isRedirecting = submitText === redirectingText; + const isRedirecting = fetcher.state === "submitting"; return ( - <> +
@@ -40,12 +18,13 @@ export default function ErrorPage(props: ErrorQueryParams) { {props.error}: {props.message}

Back - +
); } diff --git a/src/pages/Registration/Steps/FSAInquiry/useSubmit.ts b/src/pages/Registration/Steps/FSAInquiry/useSubmit.ts index 33bba40869..e565c83451 100644 --- a/src/pages/Registration/Steps/FSAInquiry/useSubmit.ts +++ b/src/pages/Registration/Steps/FSAInquiry/useSubmit.ts @@ -1,7 +1,6 @@ -import { useErrorContext } from "contexts/ErrorContext"; +import type { Update } from "@better-giving/registration/update"; import type { SubmitHandler, UseFormReturn } from "react-hook-form"; -import { useNavigate } from "react-router-dom"; -import { useUpdateRegMutation } from "services/aws/registration"; +import { useFetcher, useNavigate } from "react-router-dom"; import { steps } from "../../routes"; import type { Step3Data } from "../../types"; import type { FV } from "./types"; @@ -13,32 +12,30 @@ export default function useSubmit( ) { const { handleSubmit, - formState: { isDirty, isSubmitting }, + formState: { isDirty }, } = form; - const [updateReg] = useUpdateRegMutation(); - const { handleError } = useErrorContext(); + const fetcher = useFetcher(); + const navigate = useNavigate(); const submit: SubmitHandler = async (fv) => { if (!isDirty && data.irs501c3 !== undefined && possiblyTaxExempt) { return navigate(`../${steps.docs}`); } - const result = await updateReg({ - id: data.init.id, + const update: Update = { type: "fsa-inq", irs501c3: possiblyTaxExempt && fv.irs501c3 === "yes", + }; + fetcher.submit(update, { + encType: "application/json", + action: ".", + method: "patch", }); - - if ("error" in result) { - return handleError(result.error, { context: "updating registration" }); - } - - navigate(`../${steps.docs}`); }; return { submit: handleSubmit(submit), - isSubmitting, + isSubmitting: fetcher.state !== "idle", }; } diff --git a/src/pages/Registration/Steps/fsa-action.ts b/src/pages/Registration/Steps/fsa-action.ts index fc2ad60585..f0267a688a 100644 --- a/src/pages/Registration/Steps/fsa-action.ts +++ b/src/pages/Registration/Steps/fsa-action.ts @@ -9,13 +9,21 @@ export const fsaAction: ActionFunction = async ({ request, params }) => { const auth = await loadAuth(); if (!auth) return redirectToAuth(request); - const data: FsaPayload["signer"] = await request.json(); + const signer = await (async (r) => { + const r1 = r.clone(); + const form = await r1.formData(); + const signer_eid = form.get("signer_eid")?.toString(); + return ( + signer_eid || + ((await request.json()) as Exclude) + ); + })(request); const from = new URL(request.url); from.pathname = `register/${params.regId}/${regRoutes.sign_result}`; from.search = ""; const payload: FsaPayload = { - signer: data, + signer, redirectUrl: from.toString(), }; diff --git a/src/pages/Registration/index.tsx b/src/pages/Registration/index.tsx index 57946b26f8..cb09aa8bff 100644 --- a/src/pages/Registration/index.tsx +++ b/src/pages/Registration/index.tsx @@ -11,6 +11,7 @@ import { } from "react-router-dom"; import SigningResult from "./SigningResult"; import { route as stepsRoute } from "./Steps"; +import { fsaAction } from "./Steps/fsa-action"; import { regLoader } from "./data/step-loader"; function Layout() { @@ -51,7 +52,11 @@ export const route: RouteObject = { loader: regLoader, children: [ stepsRoute, - { path: regRoutes.sign_result, element: }, + { + path: regRoutes.sign_result, + element: , + action: fsaAction, + }, ], }, ], diff --git a/src/services/aws/aws.ts b/src/services/aws/aws.ts index 5e45d6e768..277e59c3b2 100644 --- a/src/services/aws/aws.ts +++ b/src/services/aws/aws.ts @@ -30,7 +30,6 @@ export const aws = createApi({ "application", "banking-applications", "banking-application", - "registration", "endow-admins", "donations", "user", diff --git a/src/services/aws/registration/index.ts b/src/services/aws/registration/index.ts deleted file mode 100644 index f2730abce1..0000000000 --- a/src/services/aws/registration/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./registration"; diff --git a/src/services/aws/registration/registration.ts b/src/services/aws/registration/registration.ts deleted file mode 100644 index 3bd198107e..0000000000 --- a/src/services/aws/registration/registration.ts +++ /dev/null @@ -1,105 +0,0 @@ -import type { FsaPayload } from "@better-giving/registration/fsa"; -import type { Submission } from "@better-giving/registration/models"; -import type { Reg, Step1 } from "@better-giving/registration/step"; -import type { NewReg, Update } from "@better-giving/registration/update"; -import { TEMP_JWT } from "constants/auth"; -import { EMAIL_SUPPORT } from "constants/env"; -import { logger } from "helpers"; -import { version as v } from "../../helpers"; -import { aws } from "../aws"; - -const registration_api = aws.injectEndpoints({ - endpoints: (builder) => ({ - newApplication: builder.query({ - query: (payload) => ({ - url: `${v(1)}/registrations`, - method: "POST", - body: payload, - headers: { authorization: TEMP_JWT }, - }), - }), - reg: builder.query({ - providesTags: ["registration"], - query: (uuid) => { - return { - url: `${v(1)}/registrations/${uuid}`, - params: { uuid }, - headers: { authorization: TEMP_JWT }, - }; - }, - transformResponse(res: Reg) { - return { ...res, reqId: 0 }; - }, - }), - fiscalSponsorshipAgreementSigningURL: builder.mutation< - { url: string }, - FsaPayload["signer"] - >({ - //no need to invalidate registration as latest would be fetched on redirect/success - query: (signer) => { - return { - url: `${v(1)}/registration-fsa`, - method: "POST", - body: { - signer, - redirectUrl: `${window.location.origin}/register/sign-result`, - } satisfies FsaPayload, - headers: { authorization: TEMP_JWT }, - }; - }, - }), - updateReg: builder.mutation({ - query: ({ id, ...payload }) => { - return { - url: `${v(1)}/registrations/${id}`, - method: "PATCH", - body: payload, - headers: { authorization: TEMP_JWT }, - }; - }, - transformErrorResponse(res, _meta, { type }) { - return { - status: res.status, - data: `Failed to update ${type}. Please try again later.`, - }; - }, - /** pessimistic manual cache update so not to GET fresh data */ - async onQueryStarted(args, { dispatch, queryFulfilled }) { - try { - const { id } = args; - const { data } = await queryFulfilled; - dispatch( - registration_api.util.updateQueryData("reg", id, () => data) - ); - } catch (err) { - logger.error(err); - } - }, - }), - submit: builder.mutation({ - invalidatesTags: ["registration"], - query: (referenceID) => ({ - url: `${v(1)}/registrations/${referenceID}/submit`, - method: "POST", - headers: { authorization: TEMP_JWT }, - }), - transformErrorResponse(err) { - return { - status: err.status, - data: `Registration submission failed. Contact ${EMAIL_SUPPORT}`, - }; - }, - }), - }), -}); -export const { - //queries - useRegQuery, - useLazyRegQuery, - useFiscalSponsorshipAgreementSigningURLMutation, - - //mutations - useUpdateRegMutation, - useNewApplicationQuery, - useSubmitMutation, -} = registration_api;