All files / src/components/ManageMoney/TransactionTable/TableActions MakeRecurring.tsx

66.66% Statements 10/15
33.33% Branches 2/6
50% Functions 2/4
66.66% Lines 10/15

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                                                          1x 5x 5x 5x   5x       5x         5x         5x                 5x                 1x                                                                                                                                                                  
import * as React from "react";
import { palette } from "@palette";
// form
import * as Yup from "yup";
import { yupResolver } from "@hookform/resolvers/yup";
import { VALIDATION_MESSAGES } from "@validation/messages";
import { useForm, FormProvider, SubmitHandler } from "react-hook-form";
// @mui
import Box from "@mui/material/Box";
import Grid from "@mui/material/Grid";
import Stack from "@mui/material/Stack";
import { useTheme } from "@mui/material/styles";
import useMediaQuery from "@mui/material/useMediaQuery";
// components
import { Text } from "@common/Text";
import { Modal } from "@common/Modal";
import { Button } from "@common/Button";
import { RHFInput } from "@common/Input";
import { RHFSelect } from "@common/Select";
import { showMessage } from "@common/Toast/ShowToast";
// assets
import { RefreshIcon } from "@assets/icons";
import NiceModal, { muiDialogV5, useModal } from "@ebay/nice-modal-react";
 
type IFormInputs = {
  recurrence: string;
  maxNumber: string;
};
 
const MakeRecuring = NiceModal.create(() => {
  const modal = useModal();
  const theme = useTheme();
  const isDesktop = useMediaQuery(theme.breakpoints.up("sm"));
 
  const schema = Yup.object({
    recurrence: Yup.string().required(VALIDATION_MESSAGES.REQUIRED),
  });
 
  const defaultValues = {
    maxNumber: "",
    recurrence: "",
  };
 
  const methods = useForm<IFormInputs>({
    resolver: yupResolver(schema),
    defaultValues,
  });
 
  const onSubmit: SubmitHandler<IFormInputs> = (data) => {
    if (isDesktop) {
      showMessage("Success", "Purchase is now recurring");
    } else {
      showMessage("Success", "Purchase is now recurring", false);
    }
    modal.hide();
  };
 
  return (
    <Modal
      {...muiDialogV5(modal)}
      title="Make Recuring Transaction"
      onClose={() => modal.hide()}
      titleIcon={<RefreshIcon />}
      closeAfterTransition={true}
      actions={
        <>
          <Button variant="text" onClick={() => modal.hide()}>
            Cancel
          </Button>
          <Button type="submit" variant="primary" form="make-recurring">
            Confirm
          </Button>
        </>
      }
    >
      <Stack
        mt={1}
        gap={2}
        direction={isDesktop ? "row" : "column"}
        alignItems={isDesktop ? "center" : "flex-start"}
      >
        <Box
          component="img"
          src="/settings-recurringmodify.png"
          alt="settings-recurringmodify"
        />
        <Box>
          <Text variant="h5" fontWeight="semibold" color={palette.neutral[800]}>
            Antonio Silvestre, antonio@gmail.com, VISA 1111
          </Text>
          <Text color={palette.neutral[600]}>
            Fill the fields below and click “Confirm”
          </Text>
        </Box>
      </Stack>
      <FormProvider {...methods}>
        <Grid
          mt={2}
          mb={1}
          container
          spacing={2}
          component="form"
          id="make-recurring"
          onSubmit={methods.handleSubmit(onSubmit)}
        >
          <Grid item xs={12} sm={6}>
            <RHFSelect
              name="recurrence"
              placeholder="Select an option"
              label="Recurrence"
              options={[
                {
                  value: "Daily",
                  label: "Daily",
                },
                {
                  value: "Monthly",
                  label: "Monthly",
                },
                {
                  value: "Quarterly",
                  label: "Quarterly",
                },
                {
                  value: "Yearly",
                  label: "Yearly",
                },
              ]}
            />
          </Grid>
          <Grid item xs={12} sm={6}>
            <RHFInput
              name="maxNumber"
              fullWidth
              placeholder="Enter a number"
              type="number"
              label="Max # number of payments"
              helperText="Leave blank to have no limit"
            />
          </Grid>
        </Grid>
      </FormProvider>
    </Modal>
  );
});
 
export default MakeRecuring;