All files / app/components/ContributorForm StandaloneContributorForm.tsx

0% Statements 0/20
0% Branches 0/10
0% Functions 0/3
0% Lines 0/20

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                                                                                                                     
import { Typography } from "@material-ui/core";
import Alert from "components/Alert";
import Button from "components/Button";
import CircularProgress from "components/CircularProgress";
import ContributorForm from "components/ContributorForm";
import { contributorFormInfoQueryKey } from "features/queryKeys";
import { GetContributorFormInfoRes } from "proto/account_pb";
import { ContributorForm as ContributorFormPb } from "proto/auth_pb";
import { useState } from "react";
import { useQuery, useQueryClient } from "react-query";
import { service } from "service";
 
import { ALREADY_FILLED_IN, FILL_IN_AGAIN, SUCCESS_MSG } from "./constants";
 
export default function StandaloneContributorForm() {
  const queryClient = useQueryClient();
 
  const [fillState, setFillState] = useState<
    undefined | "success" | "fillAgain"
  >(undefined);
 
  const {
    data,
    isLoading: queryLoading,
    error: queryError,
  } = useQuery<GetContributorFormInfoRes.AsObject, Error>(
    contributorFormInfoQueryKey,
    service.account.getContributorFormInfo
  );
 
  const handleSubmit = async (form: ContributorFormPb.AsObject) => {
    await service.account.fillContributorForm(form);
    queryClient.invalidateQueries(contributorFormInfoQueryKey);
    setFillState("success");
  };
 
  return queryLoading ? (
    <CircularProgress />
  ) : (
    <>
      {queryError && <Alert severity="error">{queryError?.message}</Alert>}
      {data?.filledContributorForm && fillState !== "fillAgain" ? (
        <>
          <Typography variant="body1" paragraph>
            {ALREADY_FILLED_IN}
          </Typography>
          <Button onClick={() => setFillState("fillAgain")}>
            {FILL_IN_AGAIN}
          </Button>
        </>
      ) : fillState === "success" ? (
        <Typography variant="body1">{SUCCESS_MSG}</Typography>
      ) : (
        <ContributorForm processForm={handleSubmit} autofocus />
      )}
    </>
  );
}