All files / react-app/src/features/forms/new-submission Medicaid.tsx

79.16% Statements 19/24
66.66% Branches 12/18
66.66% Functions 8/12
86.36% Lines 19/22

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                                                            4x 46x 46x   46x 1x 1x   1x   2x         1x 1x     46x                                                                                                                                                                                                               4x 2x 2x 2x 2x                                                                       32x                                         1x                                                                               44x                    
import { useEffect, useState } from "react";
import { Link } from "react-router";
import { isStateUser } from "shared-utils";
 
import { useGetUser } from "@/api";
import {
  ActionForm,
  DatePicker,
  FormArg,
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
  Input,
  RequiredIndicator,
  SpaIdFormattingDesc,
  UserPrompt as UserPromptType,
} from "@/components";
import { AttachmentFileFormatInstructions } from "@/components/ActionForm/actionForm.components";
import { FAQ_TAB } from "@/consts";
import { formSchemas } from "@/formSchemas";
import { useFeatureFlag } from "@/hooks/useFeatureFlag";
 
type MedSpaFooterProps = {
  form: FormArg<(typeof formSchemas)["new-medicaid-submission"]>;
  onSubmit: () => void;
  onCancel: (promptOverride?: Partial<Omit<UserPromptType, "onAccept">>) => void;
};
 
const MedSpaFooter = ({ form, onSubmit, onCancel }: MedSpaFooterProps) => {
  const [isFooterFixed, setIsFooterFixed] = useState(false);
  const watchedId = form.watch("id");
 
  useEffect(() => {
    const target = document.getElementById("footer-trigger");
    Iif (!target) return;
 
    const observer = new IntersectionObserver(
      ([entry]) => {
        setIsFooterFixed(!entry.isIntersecting);
      },
      { threshold: 0.2 },
    );
 
    observer.observe(target);
    return () => observer.disconnect();
  }, []);
 
  if (isFooterFixed) {
    return (
      <section
        className="fixed bottom-0 left-0 w-full z-40 border-t border-gray-300 bg-white px-6"
        id="form-actions"
        data-testid="medicaid-form-footer"
      >
        <div className="flex justify-between items-center w-full py-3">
          <button
            onClick={() =>
              onCancel({
                header: "Leave this page?",
                body: `Unsaved changes${
                  watchedId.trim() ? ` to ${watchedId}` : ""
                } will be discarded. Go back to save your changes.`,
                acceptButtonText: "Yes, leave",
                cancelButtonText: "Go back",
                areButtonsReversed: true,
                cancelVariant: "link",
              })
            }
            data-testid="cancel-action-form-footer"
            className="w-24 py-3 px-5 text-blue-700 font-semibold underline"
            type="button"
          >
            Cancel
          </button>
 
          <div className="flex gap-2.5">
            <button
              type="button"
              className="w-[128.36px] py-3 px-5 gap-2.5 rounded border-2 border-blue-700 text-blue-700 bg-white font-semibold text-sm"
            >
              Save
            </button>
            <button
              onClick={onSubmit}
              disabled={!form.formState.isValid}
              data-testid="submit-action-form"
              className={`w-[181.75px] py-3 px-5 gap-2.5 rounded font-semibold text-sm transition ${
                !form.formState.isValid
                  ? "bg-gray-300 text-white cursor-not-allowed"
                  : "bg-blue-700 text-white hover:bg-blue-800"
              }`}
            >
              Save & Submit
            </button>
          </div>
        </div>
      </section>
    );
  }
 
  return (
    <section
      className="flex flex-col md:flex-row justify-between items-center gap-4 p-4 w-full"
      data-testid="medicaid-form-footer"
    >
      <div className="flex justify-between items-center w-full py-3">
        <button
          onClick={() =>
            onCancel({
              header: "Leave this page?",
              body: `Unsaved changes${
                watchedId.trim() ? ` to ${watchedId}` : ""
              } will be discarded. Go back to save your changes.`,
              acceptButtonText: "Yes, leave",
              cancelButtonText: "Go back",
              areButtonsReversed: true,
              cancelVariant: "link",
            })
          }
          data-testid="cancel-action-form-footer"
          className="w-24 py-3 px-5 text-blue-700 font-semibold underline"
          type="button"
        >
          Cancel
        </button>
 
        <div className="flex gap-2.5">
          <button
            type="button"
            className="w-[128.36px] py-3 px-5 gap-2.5 rounded border-2 border-blue-700 text-blue-700 bg-white font-semibold text-sm"
          >
            Save
          </button>
          <button
            onClick={onSubmit}
            disabled={!form.formState.isValid}
            data-testid="submit-action-form"
            className={`w-[181.75px] py-3 px-5 gap-2.5 rounded font-semibold text-sm transition ${
              !form.formState.isValid
                ? "bg-gray-300 text-white cursor-not-allowed"
                : "bg-blue-700 text-white hover:bg-blue-800"
            }`}
          >
            Save & Submit
          </button>
        </div>
      </div>
    </section>
  );
};
 
export const MedicaidForm = () => {
  const { data: user } = useGetUser();
  const isStateUserFlag = useFeatureFlag("MED_SPA_FOOTER");
  const isState = isStateUser(user?.user);
  const isMedSpaFooterShown = isState && isStateUserFlag;
 
  return (
    <ActionForm
      schema={formSchemas["new-medicaid-submission"]}
      title="Medicaid SPA Details"
      breadcrumbText="Submit new Medicaid SPA"
      fields={({ control }) => (
        <>
          <FormField
            control={control}
            name="id"
            render={({ field }) => {
              return (
                <FormItem>
                  <div className="flex gap-4">
                    <FormLabel htmlFor="spa-id" className="font-semibold" data-testid="spaid-label">
                      SPA ID <RequiredIndicator />
                    </FormLabel>
                    <Link
                      to="/faq/spa-id-format"
                      target={FAQ_TAB}
                      rel="noopener noreferrer"
                      className="text-blue-900 underline"
                    >
                      What is my SPA ID?
                    </Link>
                  </div>
                  <SpaIdFormattingDesc />
                  <FormControl>
                    <Input
                      id="spa-id"
                      className="max-w-sm"
                      ref={field.ref}
                      value={field.value}
                      aria-describedby="spa-id-formatting-desc"
                      onChange={(e) => field.onChange(e.currentTarget.value.toUpperCase())}
                    />
                  </FormControl>
                  <FormMessage announceOn={field.value} />
                </FormItem>
              );
            }}
          />
          <FormField
            control={control}
            name="proposedEffectiveDate"
            render={({ field }) => (
              <FormItem className="max-w-md">
                <FormLabel
                  className="text-lg font-semibold block"
                  data-testid="proposedEffectiveDate-label"
                >
                  Proposed Effective Date of Medicaid SPA <RequiredIndicator />
                </FormLabel>
                <FormControl>
                  <DatePicker
                    onChange={(date) => field.onChange(date.getTime())}
                    date={field.value ? new Date(field.value) : undefined}
                    dataTestId="proposedEffectiveDate"
                  />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
        </>
      )}
      defaultValues={{ id: "" }}
      attachments={{
        instructions: [
          <p data-testid="attachments-instructions">
            Maximum file size of 80 MB per attachment.{" "}
            <span className="font-bold">
              You can add multiple files per attachment type except for the CMS-179 Form.
            </span>{" "}
            Read the description for each of the attachment types on the{" "}
            <Link
              to="/faq/medicaid-spa-attachments"
              target={FAQ_TAB}
              rel="noopener noreferrer"
              className="text-blue-900 underline"
            >
              FAQ Page
            </Link>
            .
          </p>,
          <AttachmentFileFormatInstructions />,
        ],
      }}
      documentPollerArgs={{
        property: "id",
        documentChecker: (check) => check.recordExists,
      }}
      footer={
        isMedSpaFooterShown
          ? ({ form, onSubmit, onCancel }) => (
              <section>
                <div id="footer-trigger" />
                <MedSpaFooter form={form} onSubmit={onSubmit} onCancel={onCancel} />
              </section>
            )
          : undefined
      }
    />
  );
};