Compare commits

...

3 Commits

3 changed files with 125 additions and 152 deletions

View File

@ -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 (
<FormField key={field.name} label={field.label}>
{field.type === "input" ? (
<FormInput props={commonProps} valid={valid} />
) : (
<FormSelect
props={commonProps}
valid={valid}
options={field.options}
/>
)}
</FormField>
);
};
return (
<FormBox title={t("cabinetCreation")}>
<div className="p-2 pt-7 flex flex-col gap-4">
<div className="flex">
<label className="mr-4 text-lg text-black dark:text-primary-dark w-[15%]">
{t("cabinetId")}
</label>
<div className="w-full">
<input
value={cabinetId.id}
className={clsx(
"w-1/5 h-10 px-2 rounded-full dark:bg-white dark:text-grey border-2 border-grey text-grey focus:outline-grey",
!cabinetId.valid && "border-error"
)}
onChange={(e) =>
setCabinetId({
id: e.target.value.toUpperCase(),
valid: true,
})
}
type="text"
maxLength={6}
/>
<AnimatePresence initial={false}>
{!cabinetId.valid && (
<motion.div
className="w-1/5 text-sm text-error ml-3 pt-1"
initial={{ opacity: 0, scale: 0 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0 }}
key="cabinetIdError"
>
Invalid Cabinet Id
</motion.div>
)}
</AnimatePresence>
</div>
</div>
<div className="flex">
<label className="mr-4 text-lg text-black dark:text-primary-dark w-[15%]">
{t("cabinetKeyId")}
</label>
<div className="w-full">
<input
value={cabinetKeyId.id}
className={clsx(
"w-1/5 h-10 px-2 rounded-full dark:bg-white dark:text-grey border-2 border-grey text-grey focus:outline-grey",
!cabinetKeyId.valid && "border-error"
)}
onChange={(e) =>
setCabinetKeyId({
id: e.target.value.toUpperCase(),
valid: true,
})
}
type="text"
maxLength={6}
/>
{!cabinetKeyId.valid && (
<div className="text-sm text-error ml-3 pt-1">Invalid Key Id</div>
)}
</div>
</div>
<div className="flex">
<label className="mr-4 text-lg text-black dark:text-primary-dark w-[15%]">
{t("noOfLockers")}
</label>
<div className="w-full">
<input
value={noOfLockers.number}
className={clsx(
"w-1/5 h-10 px-2 rounded-full dark:bg-white dark:text-grey border-2 border-grey text-grey focus:outline-grey",
!noOfLockers.valid && "border-error"
)}
onChange={(e) =>
setNoOfLockers({ number: e.target.value, valid: true })
}
type="number"
/>
{!noOfLockers.valid && (
<div className="text-sm text-error ml-3 pt-1">
Invalid Number of Lockers
</div>
)}
</div>
</div>
</div>
<Button
text={t("next")}
onClick={(e) => {
handleNext(e);
}}
/>
<FieldsWrapper>
{formFields.map(renderField)}
</FieldsWrapper>
<Button text={t('next')} onClick={handleNext} />
</FormBox>
);
}

View File

@ -3,9 +3,10 @@ import { useNavigate } from "react-router-dom";
import { useTranslation } from "react-i18next";
import FormBox from "../components/FormBox";
import Button from "../components/Button";
import { motion } from "motion/react";
import clsx from "clsx";
import { AnimatePresence } from "motion/react";
import FormInput from "../components/FormInput";
import FormSelect from "../components/FormSelect";
import FieldsWrapper from "../components/FieldsWrapper";
import FormField from "../components/FormField";
function CabinetMaintenace() {
const navigate = useNavigate();
@ -20,47 +21,41 @@ function CabinetMaintenace() {
navigate(operation.value);
};
const formFields = [
{
name: "value",
label: t("operation"),
options: [{ label: t("create"), value: "create" }],
type: "select",
},
];
const renderField = (field) => {
const commonProps = {
value: operation[field.name],
onChange: (e) => {
const newValues = { ...operation };
newValues[field.name] = e.target.value;
newValues[`${field.name}Valid`] = true;
setOperation(newValues);
},
};
const options = field.options;
return (
<FormField label={field.label}>
{field.type === "input" ? (
<FormInput props={commonProps} />
) : (
<FormSelect props={commonProps} options={options} />
)}
</FormField>
);
};
return (
<div>
<FormBox title={t("cabinetMaintenance")}>
<div className="p-2 pt-7">
<div className="flex">
<label className="mr-4 text-lg text-black dark:text-primary-dark w-[10%]">
{t("operation")}
</label>
<div className="w-full">
<select
className={clsx(
"w-1/5 h-10 px-2 rounded-full dark:bg-grey dark:text-primary-dark border-2 focus:outline-grey border-grey",
!operation.valid && "border-error"
)}
onChange={(e) =>
setOperation({ value: e.target.value, valid: true })
}
defaultValue={operation.value}
value={operation.value}
>
<option value="" disabled>
{t("select")}
</option>
<option value="create">{t("create")}</option>
</select>
<AnimatePresence>
{!operation.valid && (
<motion.div
className="w-1/5 text-sm text-error ml-3 pt-1"
initial={{ y: 15, opacity: 0 }}
animate={{ y: 0, opacity: 1 }}
exit={{ y: 15, opacity: 0 }}
key="cabinetIdError"
>
Invalid Operation
</motion.div>
)}
</AnimatePresence>
</div>
</div>
</div>
<FieldsWrapper>{formFields.map(renderField)}</FieldsWrapper>
<Button text={t("next")} onClick={(e) => handleNext(e)} />
</FormBox>
</div>

View File

@ -20,7 +20,7 @@ function LockersRegistration() {
const noOfLockers = location.state?.noOfLockers;
const cabinetId = location.state?.cabinetId;
const initLockers = Array(noOfLockers ? parseInt(noOfLockers) : 0)
const initLockers = Array(noOfLockers)
.fill()
.map(() => ({
id: "",
@ -163,9 +163,9 @@ function LockersRegistration() {
const valid = lockerValues[`${field.name}Valid`];
return field.type === "input" ? (
<FormInput props={commonProps} valid={valid} />
<FormInput key={field.name} props={commonProps} valid={valid} />
) : (
<FormSelect props={commonProps} valid={valid} options={field.options} />
<FormSelect key={field.name} props={commonProps} valid={valid} options={field.options} />
);
};