All files / src/apps/organisations/components OrgForm.tsx

20% Statements 4/20
0% Branches 0/13
0% Functions 0/5
21.05% Lines 4/19

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                                                              1x                     1x                 1x                 1x                                                                                                                                                                                                                                                                                                            
import React, { useEffect, useRef } from "react";
import {
  Button,
  FormControl,
  InputLabel,
  MenuItem,
  Typography
} from "@mui/material";
import { CONTEXT, getPrettyError } from "../../../utils";
import { Organisation } from "../types";
import {
  ErrorMessage,
  Field,
  Form,
  Formik,
  FormikProps,
  FormikValues
} from "formik";
import * as Yup from "yup";
import { Select, TextField } from "formik-mui";
import { makeStyles } from "@mui/styles";
 
interface Props {
  onSubmit?: (values: Organisation) => void;
  loading: boolean;
  status?: string;
  errors?: {};
  context?: string;
  savedValues?: Organisation;
}
 
const OrganisationSchema = Yup.object().shape<Organisation>({
  id: Yup.string().required("Organisation id is required"),
  name: Yup.string().required("Organisation name is required"),
  location: Yup.string().notRequired(),
  company: Yup.string().notRequired(),
  website: Yup.string()
    .url()
    .notRequired(),
  public_access: Yup.string().notRequired()
});
 
const initialValues: Organisation = {
  id: "",
  name: "",
  company: "",
  website: "",
  location: "",
  public_access: "View"
};
 
const useStyles = makeStyles({
  organisationForm: {
    padding: "2vh 2vw"
  },
  submitButton: {
    textAlign: "center"
  }
});
 
const OrganisationForm: React.FC<Props> = ({
  onSubmit,
  context = CONTEXT.view,
  savedValues,
  errors,
  status,
  loading
}) => {
  const classes = useStyles();
  const editing = context === CONTEXT.edit;
  const error = getPrettyError(errors);
 
  const formikRef = useRef<FormikProps<FormikValues & Organisation>>(null);
 
  useEffect(() => {
    const { current: currentRef } = formikRef;
    if (currentRef) {
      currentRef.setSubmitting(loading);
    }
  }, [loading]);
 
  useEffect(() => {
    const { current: currentRef } = formikRef;
    if (currentRef) {
      currentRef.setStatus(status);
    }
  }, [status]);
 
  return (
    <div id="organisation-form" className={classes.organisationForm}>
      <Formik
        innerRef={formikRef}
        initialValues={savedValues || initialValues}
        validationSchema={OrganisationSchema}
        validateOnChange={false}
        onSubmit={(values: Organisation) => {
          if (onSubmit) onSubmit(values);
        }}
      >
        {({ isSubmitting }) => (
          <Form>
            <Field
              // required
              variant="standard"
              fullWidth
              autoComplete="off"
              disabled={editing || isSubmitting}
              id="id"
              name="id"
              label="Organisation ID"
              margin="normal"
              multiline
              rowsMax={4}
              component={TextField}
            />
            <Field
              // required
              variant="standard"
              fullWidth
              autoComplete="off"
              id="name"
              name="name"
              label="Organisation Name"
              margin="normal"
              multiline
              rowsMax={4}
              component={TextField}
            />
            <Field
              variant="standard"
              fullWidth
              disabled={isSubmitting}
              autoComplete="off"
              id="company"
              name="company"
              label="Company"
              margin="normal"
              multiline
              rowsMax={4}
              component={TextField}
            />
            <Field
              variant="standard"
              fullWidth
              multiline
              rowsMax={4}
              id="website"
              name="website"
              label="Website"
              margin="normal"
              component={TextField}
            />
            <Field
              variant="standard"
              fullWidth
              multiline
              rowsMax={4}
              id="location"
              name="location"
              label="Location"
              margin="normal"
              component={TextField}
            />
            <FormControl variant="standard" fullWidth margin="normal">
              <InputLabel shrink htmlFor="public_access">
                Public Access
              </InputLabel>
              <Field
                variant="standard"
                name="public_access"
                id="public_access"
                component={Select}
              >
                <MenuItem value="View">View</MenuItem>
                <MenuItem value="None">Edit</MenuItem>
                <MenuItem value="None">None</MenuItem>
              </Field>
              <Typography color="error" variant="caption" component="div">
                <ErrorMessage name="public_access" component="span" />
              </Typography>
            </FormControl>
 
            <br />
            <br />
            <div className={classes.submitButton}>
              {!error ? (
                <br />
              ) : (
                <Typography color="error" variant="caption" component="div">
                  {error}
                </Typography>
              )}
              <Button
                variant="outlined"
                color="primary"
                size="medium"
                type="submit"
                disabled={isSubmitting}
              >
                Submit
              </Button>
            </div>
          </Form>
        )}
      </Formik>
    </div>
  );
};
 
export default OrganisationForm;