import React, { useEffect, useMemo, useRef } from "react"; import { useDispatch, useSelector } from "react-redux"; import { updatePartnerPreferences } from "../redux/registrationFormSlice"; import { Grid, FormControl, InputLabel, Select, MenuItem, Button, Checkbox, ListItemText, TextField, } from "@mui/material"; import { usePartnerPreferenceMasters } from "../hooks/useMasters"; import { useCityMasters, useSubCasteMasters } from "../hooks/useDependentMasters"; const PartnerPreferencesForm = ({ onSubmitStep, onSkipStep, errors, onFieldChange, }) => { const dispatch = useDispatch(); const data = useSelector((state) => state.registerform.partnerPreferences); const inputRef = useRef(null); const requiredMark = *; const { data: masters, isLoading: isPartnerMastersLoading } = usePartnerPreferenceMasters(); const subCasteQuery = useSubCasteMasters(data.castes); const cityQuery = useCityMasters(data.states); const ageOptions = useMemo(() => Array.from({ length: 53 }, (_, i) => i + 18), []); const heightOptions = useMemo(() => { const raw = masters; if (!raw) return []; return raw.heights || raw.height || []; }, [masters]); const maritalStatusOptions = useMemo(() => { const raw = masters; if (!raw) return []; return raw.maritalStatus || raw.marital_status || []; }, [masters]); const starOptions = useMemo(() => { const raw = masters; if (!raw) return []; return raw.stars || raw.star || []; }, [masters]); const employeeTypeOptions = useMemo(() => { const raw = masters; if (!raw) return []; return raw.employeeTypes || raw.employee_type || []; }, [masters]); const casteOptions = useMemo(() => { const raw = masters; if (!raw) return []; if (Array.isArray(raw)) return raw; return raw.caste || raw.castes || []; }, [masters]); const occupationOptions = useMemo(() => { const raw = masters; if (!raw) return []; if (Array.isArray(raw)) return raw; return raw.occupation || raw.occupations || []; }, [masters]); const educationOptions = useMemo(() => { const raw = masters; if (!raw) return []; if (Array.isArray(raw)) return raw; return raw.education || raw.educations || []; }, [masters]); const hobbyOptions = useMemo(() => { const raw = masters; if (!raw) return []; if (Array.isArray(raw)) return raw; return raw.hobbies || raw.hobby || []; }, [masters]); const annualIncomeOptions = useMemo(() => { const raw = masters; if (!raw) return []; if (Array.isArray(raw)) return raw; return raw.annual_income || raw.annualIncome || []; }, [masters]); const currencyOptions = useMemo(() => ["INR", "USD"], []); const stateOptions = useMemo(() => { const raw = masters; if (!raw) return []; if (Array.isArray(raw)) return raw; return raw.state || raw.states || []; }, [masters]); const subCasteOptions = useMemo(() => { const raw = subCasteQuery.data; if (!raw) return []; if (Array.isArray(raw)) { const merged = raw.flatMap((entry) => { if (!entry) return []; if (Array.isArray(entry)) return entry; return entry.sub_caste || entry.subCaste || entry.data || []; }); return merged; } return raw.sub_caste || raw.subCaste || raw.data || []; }, [subCasteQuery.data]); const cityOptions = useMemo(() => { const raw = cityQuery.data; if (!raw) return []; if (Array.isArray(raw)) { const merged = raw.flatMap((entry) => { if (!entry) return []; if (Array.isArray(entry)) return entry; return entry.district || entry.districts || entry.data || []; }); return merged; } return raw.district || raw.districts || raw.data || []; }, [cityQuery.data]); useEffect(() => { inputRef.current?.focus(); }, []); const handleChange = (field, value) => { const arrayFields = new Set([ "castes", "sub_castes", "occupations", "educations", "states", "districts", "marital_statuses", "birth_stars", "employee_types", "currencies", ]); const nextValue = arrayFields.has(field) && typeof value === "string" ? value.split(",").filter(Boolean) : value; const updates = { [field]: nextValue }; const fieldsToClear = [field]; if (field === "castes") { updates.sub_castes = []; fieldsToClear.push("sub_castes"); } if (field === "states") { updates.districts = []; fieldsToClear.push("districts"); } dispatch(updatePartnerPreferences(updates)); if (onFieldChange) onFieldChange(fieldsToClear); }; const handleSubmit = () => { console.log("Submitting partner preferences:", data); onSubmitStep(); }; const renderMultiSelect = ({ name, label, options, value, getLabel, getValue, disabled, }) => ( Select {label} {errors[name] && (

{errors[name]}

)}
); return (
{/* 1. Age Range */}
From Age {errors.age_from && (

{errors.age_from}

)}
To Age {errors.age_to && (

{errors.age_to}

)}
{/* 2. Height Range */}
From Height {errors.height_from && (

{errors.height_from}

)}
To Height {errors.height_to && (

{errors.height_to}

)}
{/* 3. Marital Status */}
{renderMultiSelect({ name: "marital_statuses", label: "Marital Status", options: maritalStatusOptions, value: data.marital_statuses, getLabel: (opt) => opt.marital_status_name || opt.name, getValue: (opt) => opt.id, disabled: isPartnerMastersLoading, })}
{/* 4. Birth Star */}
{renderMultiSelect({ name: "birth_stars", label: "Birth Star", options: starOptions, value: data.birth_stars, getLabel: (opt) => opt.star_name || opt.name, getValue: (opt) => opt.id, disabled: isPartnerMastersLoading, })}
{/* 5. Caste */}
{renderMultiSelect({ name: "castes", label: "Caste", options: casteOptions, value: data.castes, getLabel: (opt) => opt.caste_name || opt.name, getValue: (opt) => opt.id, disabled: isPartnerMastersLoading, })}
{/* 6. Sub-Sect */}
{renderMultiSelect({ name: "sub_castes", label: "Sub-Sect", options: subCasteOptions, value: data.sub_castes, getLabel: (opt) => opt.sub_caste_name || opt.name, getValue: (opt) => opt.id, disabled: data.castes.length === 0 || subCasteQuery.isLoading, })}
{/* 7. Qualification */}
{renderMultiSelect({ name: "educations", label: "Qualification", options: educationOptions, value: data.educations, getLabel: (opt) => opt.education_name || opt.name, getValue: (opt) => opt.id, disabled: isPartnerMastersLoading, })}
{/* 8. Occupation */}
{renderMultiSelect({ name: "occupations", label: "Occupation", options: occupationOptions, value: data.occupations, getLabel: (opt) => opt.occupation_name || opt.name, getValue: (opt) => opt.id, disabled: isPartnerMastersLoading, })}
{/* 9. Employee Type */}
{renderMultiSelect({ name: "employee_types", label: "Employee Type", options: employeeTypeOptions, value: data.employee_types, getLabel: (opt) => opt.employee_type_name || opt.name, getValue: (opt) => opt.id, disabled: isPartnerMastersLoading, })}
{/* 10. Currency Type */}
{renderMultiSelect({ name: "currencies", label: "Currency", options: currencyOptions, value: data.currencies, getLabel: (opt) => opt, getValue: (opt) => opt, disabled: isPartnerMastersLoading, })}
{/* 11. Income Range */} {data.currencies.includes("INR") && (
handleChange("inr_from", e.target.value)} /> handleChange("inr_to", e.target.value)} />
)} {data.currencies.includes("USD") && (
handleChange("usd_from", e.target.value)} /> handleChange("usd_to", e.target.value)} />
)} {/* 12. State */}
{renderMultiSelect({ name: "states", label: "State", options: stateOptions, value: data.states, getLabel: (opt) => opt.state_name || opt.name, getValue: (opt) => opt.id, disabled: isPartnerMastersLoading, })}
{/* 13. City */}
{renderMultiSelect({ name: "districts", label: "City", options: cityOptions, value: data.districts, getLabel: (opt) => opt.district_name || opt.city_name || opt.name, getValue: (opt) => opt.id, disabled: data.states.length === 0 || cityQuery.isLoading, })}
{onSkipStep && ( )}
); }; export default PartnerPreferencesForm;