import { useEffect, useState } from 'react' import { Link } from 'react-router-dom' import { fetchWhatsappParentContactsByClass, updateWhatsappMembership, type ParentContactsByClassParentRow, type ParentContactsByClassSection, } from '../../api/whatsapp' function digitsOnly(phone: string): string { return phone.replace(/\D+/g, '') } function memClass(v: string): string { if (v === '1') return 'mem-yes' if (v === '0') return 'mem-no' return 'mem-none' } export function WhatsappParentContactsByClassPage() { const [sections, setSections] = useState([]) const [openIndex, setOpenIndex] = useState(0) const [error, setError] = useState(null) const [statusByKey, setStatusByKey] = useState>({}) useEffect(() => { let cancelled = false ;(async () => { try { const data = await fetchWhatsappParentContactsByClass() if (cancelled) return setSections(data.classSections ?? []) setError(null) } catch (e) { if (!cancelled) setError(e instanceof Error ? e.message : 'Unable to load data.') } })() return () => { cancelled = true } }, []) async function saveRow( section: ParentContactsByClassSection, p: ParentContactsByClassParentRow, primaryVal: string, secondVal: string, rowKey: string, ) { const class_section_id = Number(p.class_section_id ?? section.class_section_id ?? 0) try { const res = await updateWhatsappMembership({ class_section_id, school_year: section.school_year, semester: section.semester, primary_id: Number(p.primary_id ?? 0), second_id: Number(p.second_id ?? 0), primary_member: primaryVal, second_member: secondVal, }) const ok = res.status === 'ok' setStatusByKey((prev) => ({ ...prev, [rowKey]: ok ? 'Saved' : res.message ?? 'Nothing to save', })) } catch { setStatusByKey((prev) => ({ ...prev, [rowKey]: 'Save failed' })) } } return (

Parent Contacts by Class

WhatsApp Group Links {error ?
{error}
: null} {sections.length === 0 && !error ?

No class sections available.

: null}
{sections.map((classSection, index) => (

{openIndex === index ? (
{(classSection.parents ?? []).length === 0 ? (

No parents found for this class section.

) : (
{(classSection.parents ?? []).map((p, rIdx) => { const rowKey = `${index}-${rIdx}` const pm = (p.primary_member ?? '') as string const sm = (p.second_member ?? '') as string return ( ) })}
Primary Parent Phone In Group (Primary) Second Parent Phone In Group (Second) Status
{p.primary_name} {p.primary_phone ? ( {p.primary_phone} ) : ( )} {p.second_name} {p.second_phone ? ( {p.second_phone} ) : ( )} {p.second_id ? ( ) : ( )} {statusByKey[rowKey] ?? ''}
)}
) : null}
))}
) }