All files / src/apps/containers/components ContainerVersionForm.tsx

18.75% Statements 3/16
0% Branches 0/11
0% Functions 0/5
21.43% Lines 3/14

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                                                                                                                                                                                                                                                    
import React, { useEffect, useRef } from "react";
import {
  Button,
  ButtonGroup,
  DialogActions,
  DialogContent,
  DialogTitle,
  FormControl,
  InputLabel,
  MenuItem,
  Typography
} from "@mui/material";
import { usePrevious } from "../../../utils";
import { Field, Form, Formik, FormikProps, FormikValues } from "formik";
import * as Yup from "yup";
import { Select, TextField } from "formik-mui";
 
import { Version } from "../../../utils";
import { v4 as uuid } from "uuid";
 
interface Props {
  onSubmit: Function;
  loading: boolean;
  error?: { detail: string };
  handleClose: () => void;
}
 
const ContainerVersionSchema = Yup.object().shape<Version>({
  id: Yup.string().required("Version ID is required"),
  released: Yup.boolean().required(
    "Choose whether you would like to release this version"
  ),
  description: Yup.string().min(0),
  external_id: Yup.string().required()
});
 
const initialValues: Version = {
  id: "",
  released: false,
  description: "",
  external_id: uuid()
};
 
const ContainerVersionForm: React.FC<Props> = ({
  onSubmit,
  loading,
  error,
  handleClose
}) => {
  const formikRef = useRef<FormikProps<FormikValues & Version>>(null);
  const previouslyLoading = usePrevious(loading);
 
  useEffect(() => {
    const { current: currentRef } = formikRef;
    if (currentRef) {
      currentRef.setSubmitting(loading);
    }
  }, [loading]);
 
  useEffect(() => {
    if (!loading && previouslyLoading && !error) handleClose();
  }, [loading, previouslyLoading, error, handleClose]);
 
  return (
    <>
      <DialogTitle>Create new version</DialogTitle>
      <Formik
        innerRef={formikRef}
        initialValues={initialValues}
        validationSchema={ContainerVersionSchema}
        validateOnChange={false}
        onSubmit={(values: Version) => {
          if (onSubmit) onSubmit(values);
        }}
      >
        {({ isSubmitting }) => (
          <>
            <Form>
              <DialogContent>
                <Field
                  variant="standard"
                  fullWidth
                  autoComplete="off"
                  id="id"
                  name="id"
                  label="ID"
                  component={TextField}
                />
                <FormControl
                  variant="standard"
                  fullWidth
                  // required
                  margin="normal"
                >
                  <InputLabel shrink htmlFor="released">
                    Release
                  </InputLabel>
                  <Field
                    variant="standard"
                    fullWidth
                    id="released"
                    name="released"
                    component={Select}
                  >
                    <MenuItem
                      // @ts-ignore: some casting is done for us we don't need to worry about using booleans as values
                      value={0}
                    >
                      No
                    </MenuItem>
                    <MenuItem
                      // @ts-ignore
                      value={1}
                    >
                      Yes
                    </MenuItem>
                  </Field>
                </FormControl>
                <Field
                  variant="standard"
                  fullWidth
                  multiline
                  rowsMax={4}
                  id="description"
                  name="description"
                  label="Description"
                  component={TextField}
                />
                <br />
                <br />
              </DialogContent>
              {!error ? (
                <br />
              ) : (
                <Typography
                  align="center"
                  color="error"
                  variant="caption"
                  component="div"
                >
                  {error}
                </Typography>
              )}
              <DialogActions>
                <ButtonGroup
                  fullWidth
                  color="primary"
                  variant="text"
                  size="medium"
                >
                  <Button onClick={handleClose}>Cancel</Button>
                  <Button type="submit" disabled={isSubmitting}>
                    Submit
                  </Button>
                </ButtonGroup>
              </DialogActions>
            </Form>
          </>
        )}
      </Formik>
    </>
  );
};
 
export default ContainerVersionForm;