import { AnimatePresence } from "motion/react"; import clsx from "clsx"; import { PackageSearch, Copy, UserSearch } from "lucide-react"; import { useState } from "react"; import { motion } from "motion/react"; import FormBox from "../components/FormBox"; import { useTranslation } from "react-i18next"; import FormField from "../components/FormField"; import FormInput from "../components/FormInput"; import FormSelect from "../components/FormSelect"; import Button from "../components/Button"; import { useToast } from "../hooks/useToast"; import productInfo from "../util/productList"; import ProductListTable from "../components/ProductListTable"; function AccountCreation() { const { t } = useTranslation(); const showToast = useToast(); const [notification] = useState({ visible: false, message: "", type: "", }); const [showProductModal, setShowProductModal] = useState(false); const [submitting] = useState(false); const [accountDetails, setAccountDetails] = useState({ productCode: "", interestCategory: "", segmentCode: "", accountHolderType: "", primaryCifNumber: "", nomineeCifNumber: "", activityCode: "", customerType: "", collateralFDAccount: "", rentPayAccount: "", productCodeValid: true, interestCategoryValid: true, segmentCodeValid: true, accountHolderTypeValid: true, primaryCifNumberValid: true, nomineeCifNumberValid: true, activityCodeValid: true, customerTypeValid: true, collateralFDAccountValid: true, rentPayAccountValid: true, }); const handleProductSelect = (product) => { const newAccountDetails = { ...accountDetails }; newAccountDetails.productCode = product.productCode; newAccountDetails.interestCategory = product.interestCategory; setAccountDetails(newAccountDetails); setShowProductModal(false); }; const accountDetailsFields = [ { label: t("productCode"), name: "productCode", type: "input", subType: "number", readOnly: true, validate: (value) => value !== "", icon: { icon: , onClick: () => setShowProductModal(true), }, }, { label: t("interestCategory"), name: "interestCategory", type: "input", subType: "number", readOnly: true, validate: (value) => value !== "", }, { label: t("segmentCode"), name: "segmentCode", type: "select", options: [ { value: "0706", label: "0706: Individual" }, { value: "0306", label: "0306: Staff" }, { value: "5003", label: "5003: Senior Citizen" }, { value: "5010", label: "5010: SHG" }, { value: "5000", label: "5000: Bank" }, { value: "5009", label: "5009: Institutions" }, { value: "5050", label: "5050: Others" }, { value: "5007", label: "5007: Society" }, ], validate: (value) => value !== "", }, { label: t("accountHolderType"), name: "accountHolderType", type: "select", options: [ { value: "1", label: "Single" }, { value: "2", label: "Joint" }, ], validate: (value) => value === "1" || value === "2", }, { label: t("primaryCifNumber"), name: "primaryCifNumber", type: "input", subType: "number", maxLength: 17, validate: (value) => /^[0-9]{17}$/.test(value), icon: { icon: }, }, { label: t("nomineeCifNumber"), name: "nomineeCifNumber", type: "input", subType: "number", maxLength: 17, validate: (value) => /^[0-9]{17}$/.test(value), }, ]; const additionalDetailsFields = [ { label: t("activityCode"), name: "activityCode", type: "select", options: [ { value: "0701", label: "Direct Agriculture" }, { value: "0702", label: "Indirect Agriculture" }, { value: "0703", label: "Agricultural Services Unit" }, { value: "0704", label: "Farm Irrigation" }, { value: "0705", label: "Fruits & Vegetables" }, { value: "0706", label: "Non-Agriculture" }, ], validate: (value) => value !== "", }, { label: t("customerType"), name: "customerType", type: "select", options: [ { value: "0709", label: "Individual" }, { value: "0701", label: "Corporate" }, ], validate: (value) => value === "0709" || value === "0701", }, { label: t("collateralFDAccount"), name: "collateralFDAccount", type: "input", subType: "number", maxLength: 17, validate: (value) => /^[0-9]{17}$/.test(value), }, { label: t("rentPayAccount"), name: "rentPayAccount", type: "input", subType: "number", maxLength: 17, validate: (value) => /^[0-9]{17}$/.test(value), }, ]; const handleSubmit = (e) => { e.preventDefault(); let isValid = true; const newValidationState = { ...accountDetails }; // Validate account details fields [...accountDetailsFields, ...additionalDetailsFields].forEach((field) => { if (field.validate) { const fieldIsValid = field.validate(accountDetails[field.name]); newValidationState[`${field.name}Valid`] = fieldIsValid; if (!fieldIsValid) isValid = false; } }); setAccountDetails(newValidationState); if (!isValid) { showToast("Highlighted fields are invalid", "error"); return; } console.log("Form is valid", accountDetails); }; const renderProductModal = () => { return ( {showProductModal && (

Select Product

)}
); }; const renerField = (field) => { const commonProps = { value: accountDetails[field.name], onChange: (e) => { const newAccountDetails = { ...accountDetails }; newAccountDetails[field.name] = e.target.value; newAccountDetails[`${field.name}Valid`] = true; setAccountDetails(newAccountDetails); }, maxLength: field.maxLength, className: clsx(!accountDetails[`${field.name}Valid`] && "border-error"), }; return ( {field.type === "input" ? ( ) : ( )} ); }; return (
{notification.visible && ( {notification.message.split(":").map((msg, index) => { return index === 1 ? ( {msg} ) : ( {msg} ); })} )} {renderProductModal()}

Account Details

{accountDetailsFields.map(renerField)}

Additional Details

{additionalDetailsFields.map(renerField)}
); } export default AccountCreation;