Files
ota-admin-portal/src/components/Certificates/Add/CreateForm.jsx
John Wu aa585bebc3 CEC-3119 Magna Create Certs (#236)
* CEC-3119 FiskerQA
Give Magna access to create certs

* Comment
2022-11-15 12:19:43 -08:00

105 lines
2.7 KiB
JavaScript

import {
Button,
FormControlLabel,
FormLabel,
Radio,
RadioGroup,
TextField
} from "@material-ui/core";
import React, { useRef, useState } from "react";
import { CertTypeData, CertTypes } from "../../../utils/certificates";
import { Providers } from "../../../utils/roles";
import { useUserContext } from "../../Contexts/UserContext";
import useStyles from "../../useStyles";
const getCertTypeLabel = (certtype) => {
const item = CertTypeData.find((item) => certtype === item.value);
if (item !== null) return item.inputlabel;
return "ID";
};
const getCertsTypes = (providers) => {
if (providers.length === 0) return [];
if (providers[0] === Providers.MAGNA) {
return CertTypeData.filter((item) => {
return (item.label === CertTypes.TBOX || item.label === CertTypes.ICC);
})
}
return CertTypeData;
}
const CreateForm = ({ onCreate, busy }) => {
const classes = useStyles();
const commonnameEl = useRef(null);
const {providers} = useUserContext();
const [certType, setCertType] = useState(CertTypes.TBOX);
const onSubmit = async (event) => {
event.preventDefault();
if (onCreate)
onCreate({
common_name: commonnameEl.current.value,
type: certType,
});
};
const onCertTypeChange = (event) => {
setCertType(event.target.value);
};
return (
<div className={classes.paper}>
<form className={classes.form} noValidate action="{onSubmit}">
<TextField
id="common_name"
name="common_name"
label={`Common Name (${getCertTypeLabel(certType)})`}
variant="outlined"
margin="normal"
inputProps={{
maxLength: "17",
}}
required
fullWidth
inputRef={commonnameEl}
/>
<FormLabel id="cert-type-group-label">Type</FormLabel>
<RadioGroup
row
aria-labelledby="cert-type-group-label"
name="cert-type"
value={certType}
onChange={onCertTypeChange}
margin="normal"
>
{getCertsTypes(providers).map((item, i) => {
return (
<FormControlLabel
key={i}
value={item.value}
label={item.label}
control={<Radio />}
/>
);
})}
</RadioGroup>
<Button
type="submit"
disabled={busy}
fullWidth
variant="contained"
color="primary"
className={classes.submit}
onClick={onSubmit}
>
{busy ? "Submitting..." : "Submit"}
</Button>
</form>
</div>
);
};
export default CreateForm;