All files / react-app/src/features/forms/waiver/temporary-extension index.tsx

88.23% Statements 15/17
95.65% Branches 22/23
77.77% Functions 7/9
88.23% Lines 15/17

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                                                  4x         4x 9x 8x   8x   8x   8x       8x     8x                                                       2x 2x                                                                 72x                                   72x                             63x                                                   63x                                                                                                      
import { useState } from "react";
import { Link, useParams } from "react-router";
 
import { useGetItem } from "@/api";
import {
  ActionForm,
  FormControl,
  FormDescription,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
  Input,
  RequiredIndicator,
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components";
import { FAQ_TAB } from "@/consts";
import { formSchemas } from "@/formSchemas";
 
import { getFAQLinkForAttachments } from "../../faqLinks";
 
const actionTypeMap = {
  New: "Initial Waiver",
  Renew: "Waiver Renewal",
};
 
export const TemporaryExtensionForm = () => {
  const { id: waiverId } = useParams<{ id: string }>();
  const { data: submission } = useGetItem(waiverId, { enabled: waiverId !== undefined });
 
  const [temporaryExtensionType, setTemporaryExtensionType] = useState<string>("");
 
  const actionType = submission?._source?.actionType;
  const actionTypeLabel =
    actionType && actionType in actionTypeMap
      ? actionTypeMap[actionType as keyof typeof actionTypeMap]
      : undefined;
  const type =
    submission?._source && actionTypeLabel
      ? `${submission._source.authority} ${actionTypeLabel}`
      : null;
  const attachmentAuthority = submission?._source?.authority ?? temporaryExtensionType;
 
  return (
    <ActionForm
      schema={formSchemas["temporary-extension"]}
      title="Temporary Extension Request Details"
      breadcrumbText={`Request ${
        submission ? submission._source.authority : "1915(b) or 1915(c)"
      } Temporary Extension`}
      fields={(form) => (
        <>
          {waiverId && submission ? (
            <div>
              <p>Temporary Extension Type</p>
              <p className="text-xl">{submission._source.authority}</p>
            </div>
          ) : (
            <FormField
              name="ids.validAuthority.authority"
              control={form.control}
              render={({ field }) => (
                <FormItem className="max-w-xs">
                  <FormLabel>
                    <strong className="font-bold">Temporary Extension Type</strong>{" "}
                    <RequiredIndicator />
                  </FormLabel>
                  <Select
                    onValueChange={(value) => {
                      field.onChange(value);
                      setTemporaryExtensionType(value);
                    }}
                    value={field.value ?? ""}
                  >
                    <FormControl>
                      <SelectTrigger>
                        <SelectValue placeholder="-- select a temporary extension type --" />
                      </SelectTrigger>
                    </FormControl>
                    <SelectContent>
                      <SelectItem value="1915(b)">1915(b)</SelectItem>
                      <SelectItem value="1915(c)">1915(c)</SelectItem>
                    </SelectContent>
                  </Select>
                  <FormMessage />
                </FormItem>
              )}
            />
          )}
          {submission ? (
            <div>
              <p>Approved Initial or Renewal Waiver Number</p>
              <p className="text-xl">{waiverId}</p>
            </div>
          ) : (
            <FormField
              name="ids.validAuthority.waiverNumber"
              control={form.control}
              render={({ field }) => {
                return (
                  <FormItem
                    className="max-w-md"
                    onChange={async () => {
                      await form.trigger("ids.validAuthority.authority");
                    }}
                  >
                    <FormLabel data-testid="waiverNumber-label">
                      <strong className="font-bold">
                        Approved Initial or Renewal Waiver Number
                      </strong>{" "}
                      <RequiredIndicator />
                    </FormLabel>
                    <FormDescription>
                      Enter the existing waiver number in the format it was approved, using a dash
                      after the two character state abbreviation.
                    </FormDescription>
                    <FormControl>
                      <Input
                        className="max-w-sm"
                        ref={field.ref}
                        value={field.value}
                        onChange={(e) => field.onChange(e.currentTarget.value.toUpperCase())}
                      />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                );
              }}
            />
          )}
          <FormField
            control={form.control}
            name="ids.id"
            render={({ field }) => (
              <FormItem
                onChange={async () => {
                  await form.trigger("ids.validAuthority.authority");
                }}
              >
                <FormLabel data-testid="requestNumber-label">
                  <strong className="font-bold">
                    Temporary Extension Request Number
                    <RequiredIndicator />
                  </strong>
                  <Link
                    className="text-blue-600 cursor-pointer hover:underline px-4"
                    to={"/faq/waiver-extension-id-format"}
                    target={FAQ_TAB}
                    rel="noopener noreferrer"
                  >
                    What is my Temporary Extension Request Number?
                  </Link>
                </FormLabel>
                <FormDescription className="max-w-md">
                  Must use a waiver extension request number with the format SS-####.R##.TE## or
                  SS-#####.R##.TE##
                </FormDescription>
                <FormControl>
                  <Input
                    className="max-w-sm"
                    ref={field.ref}
                    value={field.value}
                    onChange={(e) => field.onChange(e.currentTarget.value.toUpperCase())}
                  />
                </FormControl>
 
                <FormMessage />
              </FormItem>
            )}
          />
          {type && (
            <div>
              <p>Type</p>
              <p className="text-xl">{type}</p>
            </div>
          )}
        </>
      )}
      defaultValues={{
        ids: {
          validAuthority: {
            authority: submission?._source?.authority ?? "",
            waiverNumber: waiverId ?? "",
          },
        },
      }}
      attachments={{
        faqLink: getFAQLinkForAttachments(`temporary-extension-${attachmentAuthority}`),
      }}
      documentPollerArgs={{
        property: (data) => data.id,
        documentChecker: (check) => check.recordExists,
      }}
      bannerPostSubmission={{
        header: "Temporary extension request submitted",
        body: "Your submission has been received.",
        variant: "success",
      }}
      draftOptions={{
        enabled: true,
        event: "temporary-extension",
        idPath: "ids.id",
        authorityPath: "ids.validAuthority.authority",
        requiredSaveFields: [
          {
            path: "ids.validAuthority.authority",
            message: "Please select a Temporary Extension Type before saving.",
          },
        ],
      }}
    />
  );
};