"use client"; import { useEffect, useState } from "react"; import { StepFooterButton } from "@/components/onboarding/StepFooterButton"; import { ErrorBanner } from "@/components/ui/ErrorBanner"; import { FullScreenLoader } from "@/components/ui/FullScreenLoader"; import { OptionPicker } from "@/components/ui/OptionPicker"; import { getOptions, submitEducation, Option, FullProfile } from "@/lib/api"; import { formatApiError } from "@/lib/hooks"; function findSelection(profile: FullProfile | null, key: string): number[] { return profile?.selections.find((s) => s.category_key === key)?.option_ids ?? []; } export function EducationStep({ initialProfile, onNext, }: { initialProfile: FullProfile | null; onNext: () => void; }) { const [levelOptions, setLevelOptions] = useState(null); const [fieldOptions, setFieldOptions] = useState(null); const [levelId, setLevelId] = useState( findSelection(initialProfile, "education_level") ); const [fieldId, setFieldId] = useState( findSelection(initialProfile, "field_of_study") ); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); useEffect(() => { Promise.all([getOptions("education_level"), getOptions("field_of_study")]) .then(([lvl, field]) => { setLevelOptions(lvl); setFieldOptions(field); }) .catch((err) => setError(formatApiError(err))); }, []); const isValid = levelId.length === 1 && fieldId.length === 1; async function handleSubmit() { if (!isValid) return; setLoading(true); setError(null); try { await submitEducation({ education_level_option_id: levelId[0], field_of_study_option_id: fieldId[0], }); onNext(); } catch (err) { setError(formatApiError(err)); } finally { setLoading(false); } } if (!levelOptions || !fieldOptions) return ; if (levelOptions.length === 0 || fieldOptions.length === 0) { return ( ); } return (
بریم مرحله بعد
); }