import React, { useEffect, useMemo, useRef } from "react"; import { useDispatch, useSelector } from "react-redux"; import { updateFamilyDetails } from "../redux/registrationFormSlice"; import { Grid, TextField, FormControl, InputLabel, Select, MenuItem, Button, Box, } from "@mui/material"; import { useFamilyMasters } from "../hooks/useMasters"; const FamilyDetailsForm = ({ onSubmitStep, onSkipStep, errors, onFieldChange }) => { const dispatch = useDispatch(); const data = useSelector((state) => state.registerform.familyDetails); const inputRef = useRef(null); const requiredMark = *; const { data: familyMasters, isLoading: isFamilyMastersLoading } = useFamilyMasters(); const occupationOptions = useMemo(() => { const raw = familyMasters; if (!raw) return []; if (Array.isArray(raw)) return raw; return raw.occupation || raw.occupations || []; }, [familyMasters]); const maritalStatusOptions = useMemo(() => { const raw = familyMasters; if (!raw) return []; if (Array.isArray(raw)) return raw; return raw.maritalStatus || raw.marital_status || []; }, [familyMasters]); const familyStatusOptions = useMemo(() => { const raw = familyMasters; if (!raw) return []; if (Array.isArray(raw)) return []; return raw.familyStatus || raw.family_status || []; }, [familyMasters]); useEffect(() => { inputRef.current?.focus(); }, []); const createSibling = () => ({ name: "", occupation: "", maritalStatus: "", haveChildrens: "", }); const syncSiblingArray = (arr, count) => { const target = Math.max(0, Number(count) || 0); const next = [...(arr || [])]; while (next.length < target) { next.push(createSibling()); } if (next.length > target) { next.length = target; } return next; }; const handleChange = (field, value) => { const updates = { [field]: value }; const fieldsToClear = [field]; if (field === "brotherCount") { const count = Number(value) || 0; updates.brotherCount = count; updates.brothers = syncSiblingArray(data.brothers, count); fieldsToClear.push("brothers"); } if (field === "sisterCount") { const count = Number(value) || 0; updates.sisterCount = count; updates.sisters = syncSiblingArray(data.sisters, count); fieldsToClear.push("sisters"); } dispatch(updateFamilyDetails(updates)); if (onFieldChange) onFieldChange(fieldsToClear); }; const handleSiblingChange = (type, index, field, value) => { const list = [...(data[type] || [])]; if (!list[index]) list[index] = createSibling(); list[index] = { ...list[index], [field]: value }; dispatch(updateFamilyDetails({ [type]: list })); if (onFieldChange) onFieldChange(type); }; const handleSubmit = () => { console.log("Submitting family details:", data); onSubmitStep(); }; const countOptions = Array.from({ length: 11 }, (_, i) => i); const renderSiblingCard = (type, index) => { const sibling = (data[type] || [])[index] || createSibling(); const labelPrefix = type === "brothers" ? "Brother" : "Sister"; return (
{labelPrefix} {index + 1}
handleSiblingChange(type, index, "name", e.target.value) } variant="outlined" /> Occupation Marital Status Have Children
); }; return (
handleChange("fatherName", e.target.value)} error={Boolean(errors.fatherName)} helperText={errors.fatherName} placeholder="Enter Father Name" variant="outlined" />
handleChange("fatherOccupation", e.target.value)} error={Boolean(errors.fatherOccupation)} helperText={errors.fatherOccupation} placeholder="Enter Father Occupation" variant="outlined" />
handleChange("motherName", e.target.value)} error={Boolean(errors.motherName)} helperText={errors.motherName} placeholder="Enter Mother Name" variant="outlined" />
handleChange("motherOccupation", e.target.value)} error={Boolean(errors.motherOccupation)} helperText={errors.motherOccupation} placeholder="Enter Mother Occupation" variant="outlined" />
Select Brother Count
Select Sister Count
Select Family Status {errors.familyStatus && (

{errors.familyStatus}

)}
handleChange("nativePlace", e.target.value)} error={Boolean(errors.nativePlace)} helperText={errors.nativePlace} placeholder="Enter Native Place" variant="outlined" />
{Number(data.brotherCount) > 0 && (
Brother Details
{Array.from({ length: Number(data.brotherCount) }).map( (_, index) => renderSiblingCard("brothers", index) )}
)} {Number(data.sisterCount) > 0 && (
Sister Details
{Array.from({ length: Number(data.sisterCount) }).map( (_, index) => renderSiblingCard("sisters", index) )}
)} {onSkipStep && ( )}
); }; export default FamilyDetailsForm;