diff --git a/src/pages/CabinetCreation.jsx b/src/pages/CabinetCreation.jsx index 5245fbb..0a4d913 100644 --- a/src/pages/CabinetCreation.jsx +++ b/src/pages/CabinetCreation.jsx @@ -3,131 +3,109 @@ import { useTranslation } from "react-i18next"; import FormBox from "../components/FormBox"; import Button from "../components/Button"; import { useNavigate } from "react-router-dom"; -import { AnimatePresence, motion } from "motion/react"; -import clsx from "clsx"; +import FieldsWrapper from "../components/FieldsWrapper"; +import FormField from "../components/FormField"; +import FormInput from "../components/FormInput"; +import FormSelect from "../components/FormSelect"; function CabinetCreation() { const { t } = useTranslation(); const navigate = useNavigate(); - const [cabinetId, setCabinetId] = useState({ id: "", valid: true }); - const [cabinetKeyId, setCabinetKeyId] = useState({ id: "", valid: true }); - const [noOfLockers, setNoOfLockers] = useState({ number: 0, valid: true }); + const [cabinetDetails, setCabinetDetails] = useState({ + cabinetId: "", + cabinetIdValid: true, + cabinetKeyId: "", + cabinetKeyIdValid: true, + noOfLockers: 0, + noOfLockersValid: true, + }); const handleNext = (e) => { e.preventDefault(); - const idRegex = /^[A-Z]{2}[0-9]{4}$/; - if (!idRegex.test(cabinetId.id)) { - setCabinetId({ id: cabinetId.id, valid: false }); - return; - } else if (!idRegex.test(cabinetKeyId.id)) { - setCabinetKeyId({ id: cabinetKeyId.id, valid: false }); - return; - } else if (noOfLockers.number === 0) { - setNoOfLockers({ number: noOfLockers.number, valid: false }); + let isFormValid = true; + const newValues = {...cabinetDetails}; + formFields.forEach(field => { + if(field.validate) { + const isFieldValid = field.validate(cabinetDetails[field.name]) + newValues[`${field.name}Valid`] = isFieldValid; + if(!isFieldValid) isFormValid = false; + } + }); + + if(!isFormValid) { + setCabinetDetails(newValues); return; } + navigate("register-lockers", { state: { - cabinetId: cabinetId.id, - cabinetKeyId: cabinetKeyId.id, - noOfLockers: noOfLockers.number, + cabinetId: cabinetDetails.cabinetId, + cabinetKeyId: cabinetDetails.cabinetKeyId, + noOfLockers: parseInt(cabinetDetails.noOfLockers), }, }); }; + + const formFields = [ + { + name: "cabinetId", + label: t("cabinetId"), + type: "input", + maxLength: 6, + validate: v => /^\w{2}\d{4}$/.test(v) + }, + { + name: "cabinetKeyId", + label: t("cabinetKeyId"), + type: "input", + maxLength: 6, + validate: v => /^\w{2}\d{4}$/.test(v) + }, + { + name: "noOfLockers", + label: t("noOfLockers"), + type: "input", + subType: "number", + validate: v => parseInt(v) > 0 + }, + ]; + + const renderField = (field) => { + const commonProps = { + value: cabinetDetails[field.name], + onChange: (e) => { + const newCabinetDetails = { ...cabinetDetails }; + newCabinetDetails[field.name] = e.target.value.toUpperCase(); + newCabinetDetails[`${field.name}Valid`] = true; + setCabinetDetails(newCabinetDetails); + }, + maxLength: field.maxLength, + type: field.subType, + }; + const valid = cabinetDetails[`${field.name}Valid`]; + + return ( + + {field.type === "input" ? ( + + ) : ( + + )} + + ); + }; + return ( -
-
- -
- - setCabinetId({ - id: e.target.value.toUpperCase(), - valid: true, - }) - } - type="text" - maxLength={6} - /> - - {!cabinetId.valid && ( - - Invalid Cabinet Id - - )} - -
-
-
- -
- - setCabinetKeyId({ - id: e.target.value.toUpperCase(), - valid: true, - }) - } - type="text" - maxLength={6} - /> - {!cabinetKeyId.valid && ( -
Invalid Key Id
- )} -
-
-
- -
- - setNoOfLockers({ number: e.target.value, valid: true }) - } - type="number" - /> - {!noOfLockers.valid && ( -
- Invalid Number of Lockers -
- )} -
-
-
-