All files / src/features/Merchants/MerchantSidePanel/Modals CreateTaskModal.tsx

78.26% Statements 18/23
86.66% Branches 13/15
50% Functions 3/6
78.26% Lines 18/23

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                                              1x         1x 1x   1x   8x 8x     8x                   8x 1x 1x     8x                                           8x       8x 2x           8x   8x   8x                                                                                                                                             1x          
import { Stack } from "@mui/material";
import { FormProvider, SubmitHandler, useForm } from "react-hook-form";
import useNiceModal from "@common/Modal/ModalFactory/hooks/useNiceModal";
import NiceModal from "@ebay/nice-modal-react";
import * as Yup from "yup";
import { yupResolver } from "@hookform/resolvers/yup";
import GiveButton from "@shared/Button/GiveButton";
import GiveBaseModal from "@shared/modals/GiveBaseModal";
import ButtonWithSpinner from "@shared/Button/ButtonWithSpinner";
import { HFGiveInput } from "@shared/HFInputs/HFGiveInput/HFGiveInput";
import { styled } from "@theme/v2/Provider";
import { isEmpty } from "lodash";
import { useEDDTaskMutations } from "../ApprovalFlowPanel/hooks/useEDDTaskMutations";
import { useEffect } from "react";
import { formatCharCountWithLimit } from "@utils/index";
 
export type TaskType = {
  merchantId: number;
  id: number;
  name: string;
  description?: string;
};
 
const schema = Yup.object().shape({
  name: Yup.string().required("Task Name is required"),
  description: Yup.string(),
});
 
const MAX_LENGTH_DESCRIPTION = 500;
const MAX_LENGTH_NAME = 100;
 
const CreateTaskModal = NiceModal.create(
  ({ merchantId, id, name, description }: TaskType) => {
    const { open, onClose } = useNiceModal();
    const { upsertTask } = useEDDTaskMutations({
      merchantId,
    });
    const methods = useForm<TaskType>({
      mode: "onChange",
      reValidateMode: "onChange",
      defaultValues: {
        name: "",
        description: "",
      },
      resolver: yupResolver(schema),
    });
 
    const handleClose = () => {
      methods.reset();
      onClose();
    };
 
    const onCreate: SubmitHandler<TaskType> = async (data) => {
      upsertTask.mutate(
        { ...data, id },
        {
          onSuccess: () => {
            handleClose();
          },
          onError: (err: any) => {
            const message = err?.response?.data?.message;
 
            if (message.includes("already exists")) {
              methods.setError("name", {
                type: "manual",
                message: "That task name is already in use. Pick another name.",
              });
            }
          },
        },
      );
    };
 
    const isValid =
      methods.formState.isValid &&
      isEmpty(methods.formState.errors) &&
      !upsertTask.isLoading;
 
    useEffect(() => {
      methods.reset({
        name: name || "",
        description: description || "",
      });
    }, [name, description]);
 
    const isEdit = Boolean(id);
 
    const values = methods.watch();
 
    return (
      <GiveBaseModal
        open={open}
        title={isEdit ? "Edit Task" : "Add New Task"}
        width="560px"
        height="57%"
        onClose={handleClose}
        buttons={
          <Stack gap="12px" flexDirection="row">
            <GiveButton
              onClick={handleClose}
              label="Cancel"
              variant="ghost"
              size="large"
            />
            <ButtonWithSpinner isLoading={upsertTask.isLoading}>
              <GiveButton
                label={isEdit ? "Save" : "Create"}
                color="primary"
                variant="filled"
                size="large"
                sx={{
                  border: "none",
                }}
                onClick={methods.handleSubmit(onCreate)}
                disabled={!isValid}
              />
            </ButtonWithSpinner>
          </Stack>
        }
      >
        <FormProvider {...methods}>
          <Stack gap="24px">
            <StyledInput
              name="name"
              label="Task Name"
              fullWidth
              inputProps={{
                maxLength: MAX_LENGTH_NAME,
              }}
              {...(!methods.formState?.errors?.name && {
                helperText: formatCharCountWithLimit(
                  values.name,
                  MAX_LENGTH_NAME,
                ),
              })}
            />
            <StyledInput
              name="description"
              label="Task Description"
              placeholder=" " //if we use `hidePlaceholder` prop, the border color won't be correct
              multiline
              rows={6}
              fullWidth
              inputProps={{
                maxLength: `${MAX_LENGTH_DESCRIPTION}`,
              }}
              helperText={formatCharCountWithLimit(
                values.description,
                MAX_LENGTH_DESCRIPTION,
              )}
            />
          </Stack>
        </FormProvider>
      </GiveBaseModal>
    );
  },
);
 
export default CreateTaskModal;
 
const StyledInput = styled(HFGiveInput)({
  label: {
    marginBottom: "0 !important",
  },
});