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, } 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 ageRangeOptions = useMemo(() => { const raw = masters; if (!raw) return []; if (Array.isArray(raw)) return raw; return raw.ageRange || raw.age_range || []; }, [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 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)) return raw; return raw.subCaste || raw.district || raw.data || []; }, [cityQuery.data]); useEffect(() => { inputRef.current?.focus(); }, []); const handleChange = (field, value) => { const arrayFields = new Set([ "castes", "subCastes", "occupations", "educations", "hobbies", "states", "districts", ]); const nextValue = arrayFields.has(field) && typeof value === "string" ? value.split(",").filter(Boolean) : value; const updates = { [field]: nextValue }; const fieldsToClear = [field]; if (field === "castes") { updates.subCastes = []; fieldsToClear.push("subCastes"); } 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 (
{/* Age Range */}
Select Age Range {errors.ageRange && (

{errors.ageRange}

)}
{/* Caste */}
{renderMultiSelect({ name: "castes", label: "Caste", options: casteOptions, value: data.castes, getLabel: (opt) => opt.caste_name || opt.name || String(opt), getValue: (opt) => opt.id ?? opt, disabled: isPartnerMastersLoading, })}
{/* Sub Caste */}
{renderMultiSelect({ name: "subCastes", label: "Sub Caste", options: subCasteOptions, value: data.subCastes, getLabel: (opt) => opt.sub_caste_name || opt.subCaste_name || opt.name || String(opt), getValue: (opt) => opt.id ?? opt, disabled: data.castes.length === 0 || subCasteQuery.isLoading, })}
{/* Occupation */}
{renderMultiSelect({ name: "occupations", label: "Occupation", options: occupationOptions, value: data.occupations, getLabel: (opt) => opt.occupation_name || opt.name || String(opt), getValue: (opt) => opt.id ?? opt, disabled: isPartnerMastersLoading, })}
{/* Qualification */}
{renderMultiSelect({ name: "educations", label: "Qualification", options: educationOptions, value: data.educations, getLabel: (opt) => opt.education_name || opt.name || String(opt), getValue: (opt) => opt.id ?? opt, disabled: isPartnerMastersLoading, })}
{/* Lifestyle and Hobbies */}
{renderMultiSelect({ name: "hobbies", label: "Lifestyle & Hobbies", options: hobbyOptions, value: data.hobbies, getLabel: (opt) => opt.hobby_name || opt.name || String(opt), getValue: (opt) => opt.id ?? opt, disabled: isPartnerMastersLoading, })}
{/* Annual Income */}
Select Annual Income {errors.annualIncome && (

{errors.annualIncome}

)}
{/* State */}
{renderMultiSelect({ name: "states", label: "State", options: stateOptions, value: data.states, getLabel: (opt) => opt.state_name || opt.name || String(opt), getValue: (opt) => opt.id ?? opt, disabled: isPartnerMastersLoading, })}
{/* City */}
{renderMultiSelect({ name: "districts", label: "City", options: cityOptions, value: data.districts, getLabel: (opt) => opt.district_name || opt.city_name || opt.name || String(opt), getValue: (opt) => opt.id ?? opt, disabled: data.states.length === 0 || cityQuery.isLoading, })}
); }; export default PartnerPreferencesForm;