All files / react-app/src/components/ActionForm index.tsx

74.13% Statements 43/58
70.83% Branches 34/48
50% Functions 8/16
73.68% Lines 42/57

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391                                                                                                                                                                                92x                                                                       368x         368x   368x 368x   368x   368x               368x   6x           368x   368x 6x 6x 6x   1x     5x     5x   5x 5x 5x   2x     2x   2x           2x 2x   2x 2x 2x 2x     2x   3x 3x           3x       368x   368x       334x 334x     334x                                       6x     6x                                                                                                                                                                                                                                               1x                     1x                                            
import { zodResolver } from "@hookform/resolvers/zod";
import { useMutation } from "@tanstack/react-query";
import { API } from "aws-amplify";
import { ReactNode, useMemo } from "react";
import { DefaultValues, FieldPath, useForm, UseFormReturn } from "react-hook-form";
import { Navigate, useLocation, useNavigate, useParams } from "react-router";
import { Authority, CognitoUserAttributes } from "shared-types";
import { isStateUser } from "shared-utils";
import { z } from "zod";
 
import { useGetUser } from "@/api";
import { MedSpaFooter } from "@/components";
import {
  ActionFormDescription,
  Banner,
  banner,
  BreadCrumbs,
  Button,
  FAQFooter,
  Form,
  FormField,
  LoadingSpinner,
  optionCrumbsFromPath,
  PreSubmissionMessage,
  RequiredFieldDescription,
  RequiredIndicator,
  SectionCard,
  SimplePageContainer,
  UserPrompt,
  userPrompt,
} from "@/components";
import { getFormOrigin, queryClient } from "@/utils";
import { CheckDocumentFunction, documentPoller } from "@/utils/Poller/documentPoller";
import { sendGAEvent } from "@/utils/ReactGA/sendGAEvent";
 
import { getAttachments } from "./actionForm.utilities";
import { ActionFormAttachments, AttachmentsOptions } from "./ActionFormAttachments";
import { AdditionalInformation } from "./AdditionalInformation";
 
type EnforceSchemaProps<Shape extends z.ZodRawShape> = z.ZodObject<
  Shape & {
    attachments?: z.ZodObject<{
      [Key in keyof Shape]: z.ZodObject<{
        label: z.ZodDefault<z.ZodString>;
        files: z.ZodArray<z.ZodTypeAny, "many"> | z.ZodOptional<z.ZodArray<z.ZodTypeAny, "many">>;
      }>;
    }>;
  },
  "strip",
  z.ZodTypeAny
>;
 
export type SchemaWithEnforcableProps<Shape extends z.ZodRawShape = z.ZodRawShape> =
  | z.ZodEffects<EnforceSchemaProps<Shape>>
  | EnforceSchemaProps<Shape>;
 
// Utility type to handle Zod schema with or without a transform
type InferUntransformedSchema<T> = T extends z.ZodEffects<infer U> ? U : T;
 
type ActionFormProps<Schema extends SchemaWithEnforcableProps> = {
  schema: Schema;
  defaultValues?: DefaultValues<z.infer<InferUntransformedSchema<Schema>>>;
  title: string;
  fields: (form: UseFormReturn<z.infer<InferUntransformedSchema<Schema>>>) => ReactNode;
  bannerPostSubmission?: Omit<Banner, "pathnameToDisplayOn">;
  promptPreSubmission?: Omit<UserPrompt, "onAccept">;
  promptOnLeavingForm?: Omit<UserPrompt, "onAccept">;
  attachments?: AttachmentsOptions;
  additionalInformation?:
    | {
        required: boolean;
        title: string;
        label: string;
      }
    | false;
  documentPollerArgs: {
    property: (keyof z.TypeOf<Schema> & string) | ((values: z.TypeOf<Schema>) => string);
    documentChecker: CheckDocumentFunction;
  };
  conditionsDeterminingUserAccess?: ((user: CognitoUserAttributes | null) => boolean)[];
  breadcrumbText: string;
  formDescription?: string;
  preSubmissionMessage?: string;
  showPreSubmissionMessage?: boolean;
  areFieldsRequired?: boolean;
  showCustomFooter?: boolean;
};
 
export const ActionForm = <Schema extends SchemaWithEnforcableProps>({
  schema,
  defaultValues = {} as DefaultValues<z.TypeOf<InferUntransformedSchema<Schema>>>,
  title,
  fields: Fields,
  bannerPostSubmission = {
    header: "Package submitted",
    body: "Your submission has been received.",
    variant: "success",
  },
  promptOnLeavingForm = {
    header: "Stop form submission?",
    body: "All information you've entered on this form will be lost if you leave this page.",
    acceptButtonText: "Yes, leave form",
    cancelButtonText: "Return to form",
    areButtonsReversed: true,
  },
  promptPreSubmission,
  documentPollerArgs,
  attachments,
  conditionsDeterminingUserAccess = [isStateUser],
  breadcrumbText,
  formDescription = `Once you submit this form, a confirmation email is sent to you and to CMS.
      CMS will use this content to review your package, and you will not be able
      to edit this form. If CMS needs any additional information, they will
      follow up by email.`,
  preSubmissionMessage,
  additionalInformation = {
    required: false,
    label: "",
    title: "Additional Information",
  },
  showPreSubmissionMessage = true,
  areFieldsRequired = true,
  showCustomFooter = false,
}: ActionFormProps<Schema>) => {
  const { id, authority } = useParams<{
    id: string;
    authority: Authority;
    type: string;
  }>();
  const { pathname } = useLocation();
 
  const navigate = useNavigate();
  const { data: userObj, isLoading: isUserLoading } = useGetUser();
 
  const breadcrumbs = optionCrumbsFromPath(pathname, authority, id);
 
  const form = useForm<z.TypeOf<Schema>>({
    resolver: zodResolver(schema),
    mode: "onChange",
    defaultValues: {
      ...defaultValues,
    },
  });
 
  const { mutateAsync } = useMutation({
    mutationFn: (formData: z.TypeOf<Schema>) =>
      API.post("os", "/submit", {
        body: formData,
      }),
  });
 
  const shouldShowMedSpaFooter =
    showCustomFooter && pathname.startsWith("/new-submission/spa/medicaid");
 
  const onSubmit = form.handleSubmit(async (formData) => {
    try {
      try {
        await mutateAsync(formData);
      } catch (error) {
        throw Error(`Error submitting form: ${error?.message || error}`);
      }
 
      const { documentChecker, property } = documentPollerArgs;
 
      const documentPollerId =
        typeof property === "function" ? property(formData) : formData[property];
 
      try {
        const poller = documentPoller(documentPollerId, documentChecker);
        await poller.startPollingData();
      } catch (error) {
        throw Error(`${error?.message || error}`);
      }
 
      const formOrigins = getFormOrigin({ authority, id });
 
      banner({
        ...bannerPostSubmission,
        pathnameToDisplayOn: formOrigins.pathname,
      });
 
      // Prevent stale data from displaying on formOrigins page
      await queryClient.invalidateQueries({ queryKey: ["record"] });
      navigate(formOrigins);
 
      const customUserRoles = userObj?.user?.["custom:cms-roles"];
      const customisMemberOf = userObj?.user?.["custom:ismemberof"];
      const userRoles = customUserRoles || customisMemberOf || "";
      const eventState = formData.id?.substring(0, 2);
 
      // send package action event
      sendGAEvent(formData.event, userRoles, eventState);
    } catch (error) {
      console.error(error);
      banner({
        header: "An unexpected error has occurred:",
        body: error instanceof Error ? error.message : String(error),
        variant: "destructive",
        pathnameToDisplayOn: window.location.pathname,
      });
      window.scrollTo(0, 0);
    }
  });
 
  const attachmentsFromSchema = useMemo(() => getAttachments(schema), [schema]);
 
  if (isUserLoading === true) {
    return <LoadingSpinner />;
  }
 
  const doesUserHaveAccessToForm = conditionsDeterminingUserAccess.some((condition) =>
    condition(userObj?.user || null),
  );
 
  if (!userObj || doesUserHaveAccessToForm === false) {
    return <Navigate to="/" replace />;
  }
 
  return (
    <SimplePageContainer>
      <BreadCrumbs
        options={[
          ...breadcrumbs,
          {
            to: pathname,
            displayText: breadcrumbText,
            order: breadcrumbs.length,
          },
        ]}
      />
      {form.formState.isSubmitting && <LoadingSpinner />}
      <Form {...form}>
        <form
          onSubmit={(e) => {
            Iif (shouldShowMedSpaFooter) {
              e.preventDefault(); // Avoid duplicate submission for MedSpa
            } else {
              onSubmit(e); // Normal submission for other forms
            }
          }}
          className="my-6 space-y-8 mx-auto justify-center flex flex-col"
        >
          <SectionCard testId="detail-section" title={title}>
            <div>
              {areFieldsRequired && <RequiredFieldDescription />}
              <ActionFormDescription boldReminder={areFieldsRequired}>
                {formDescription}
              </ActionFormDescription>
            </div>
            <Fields {...form} />
          </SectionCard>
          {attachmentsFromSchema.length > 0 && (
            <ActionFormAttachments attachmentsFromSchema={attachmentsFromSchema} {...attachments} />
          )}
          {additionalInformation && (
            <SectionCard
              testId="additional-info"
              title={
                <>
                  {additionalInformation.title}{" "}
                  {additionalInformation.required && <RequiredIndicator />}
                </>
              }
            >
              <FormField
                control={form.control}
                name={"additionalInformation" as FieldPath<z.TypeOf<Schema>>}
                render={({ field }) => (
                  <AdditionalInformation label={additionalInformation.label} field={field} />
                )}
              />
            </SectionCard>
          )}
          {showPreSubmissionMessage && (
            <PreSubmissionMessage
              hasProgressLossReminder={areFieldsRequired}
              preSubmissionMessage={preSubmissionMessage}
            />
          )}
          {shouldShowMedSpaFooter && (
            <MedSpaFooter
              onCancel={() =>
                userPrompt({
                  ...promptOnLeavingForm,
                  onAccept: () => {
                    const origin = getFormOrigin({ id, authority });
                    navigate(origin);
                  },
                })
              }
              onSubmit={() => {
                if (promptPreSubmission) {
                  userPrompt({ ...promptPreSubmission, onAccept: onSubmit });
                } else {
                  onSubmit();
                }
              }}
              disabled={!form.formState.isValid}
            />
          )}
 
          {shouldShowMedSpaFooter ? (
            <section
              id="form-actions"
              className="flex flex-col md:flex-row justify-between items-center gap-4 p-4 w-full"
            >
              <div className="w-full md:w-auto text-center md:text-left">
                <Button
                  type="reset"
                  onClick={() =>
                    userPrompt({
                      ...promptOnLeavingForm,
                      onAccept: () => {
                        const origin = getFormOrigin({ id, authority });
                        navigate(origin);
                      },
                    })
                  }
                  variant="outline"
                  data-testid="cancel-action-form"
                  className="text-blue-700 font-semibold underline px-0 py-0 bg-transparent shadow-none border-none hover:bg-transparent"
                >
                  Cancel
                </Button>
              </div>
              <div className="flex flex-col md:flex-row gap-3 w-full md:w-auto justify-center md:justify-end">
                <Button
                  type="button"
                  onClick={() => {}}
                  className="bg-white w-[113px] text-blue-700 border border-blue-700 font-semibold text-sm px-5 py-2 rounded-md hover:bg-white hover:text-blue-700 hover:border-blue-700"
                >
                  Save
                </Button>
                <Button
                  type="button"
                  onClick={() => {
                    if (promptPreSubmission) {
                      userPrompt({ ...promptPreSubmission, onAccept: onSubmit });
                    } else {
                      onSubmit(); // manually call submit handler
                    }
                  }}
                  disabled={!form.formState.isValid}
                  data-testid="submit-action-form"
                  className="bg-blue-700 text-white font-semibold text-sm px-5 py-2 rounded-md"
                >
                  Save & Submit
                </Button>
              </div>
            </section>
          ) : (
            <section className="flex justify-end gap-2 p-4 ml-auto">
              <Button
                className="px-12"
                type={promptPreSubmission ? "button" : "submit"}
                onClick={
                  promptPreSubmission
                    ? () => userPrompt({ ...promptPreSubmission, onAccept: onSubmit })
                    : undefined
                }
                disabled={!form.formState.isValid}
                data-testid="submit-action-form"
              >
                Submit
              </Button>
              <Button
                className="px-12"
                onClick={() =>
                  userPrompt({
                    ...promptOnLeavingForm,
                    onAccept: () => {
                      const origin = getFormOrigin({ id, authority });
                      navigate(origin);
                    },
                  })
                }
                variant="outline"
                type="reset"
                data-testid="cancel-action-form"
              >
                Cancel
              </Button>
            </section>
          )}
        </form>
      </Form>
      <FAQFooter />
    </SimplePageContainer>
  );
};