"use client"; import Image from "next/image"; import { useRouter } from "next/navigation"; import { useMemo, useState } from "react"; import { IoClose } from "react-icons/io5"; import QuestionCard from "@/components/questions/question-card"; import RequiredStepsCard from "@/components/questions/required-steps-card"; import Button from "@/components/ui/button"; import InformationSheet from "@/components/ui/information-sheet"; import NavigationButton from "@/components/ui/navigation-button"; import { PageBackground } from "@/components/utils/page-background"; import { getQuestionListItems, isQuestionListItemVisibleForProfile, isQuestionRequiredForProfile, isQuestionVisibleForProfile, type QuestionListItem, } from "@/data/question-data"; import { hasQuestionAnswerValue } from "@/components/questions/question-answer-storage"; import { clearMatchStartGrace, markMatchStarted, } from "@/lib/match-start-grace"; import { useStartMarriageMatchMutation } from "@/hooks/marriage/use-match-start"; import { useMarriageProfileQuery } from "@/hooks/marriage/use-profile-main"; import { useMarriageSectionsQuery } from "@/hooks/marriage/use-sections"; import { localizePath } from "@/translations/config"; import { useI18n } from "@/translations/provider"; import { toFrontendSlug } from "@/data/section-slug-map"; import SectionsRequest from "./sections-request"; import { getStoredAge, getLocalSectionProgress } from "@/components/questions/progress-helper"; export default function QuestionsListPage() { const { dictionary: t, locale } = useI18n(); const router = useRouter(); const { data: profile } = useMarriageProfileQuery(); const { data: sections } = useMarriageSectionsQuery({ refetchOnMount: "always", }); const startMatchMutation = useStartMarriageMatchMutation({ onSuccess: () => { markMatchStarted(); router.push(localizePath("/finding-match", locale)); }, onError: () => { // Never pretend the request went through – the user stays here and can // retry instead of being parked on the waiting screen forever. clearMatchStartGrace(); }, }); const [isOptionalInfoSheetOpen, setIsOptionalInfoSheetOpen] = useState(false); const [selectedSection, setSelectedSection] = useState(null); const questionListItems = useMemo( () => getQuestionListItems(locale).filter((item) => isQuestionListItemVisibleForProfile(item, { gender: profile?.gender, }), ), [locale, profile?.gender], ); const sectionProgressBySlug = useMemo(() => { const progressBySlug = new Map(); const age = getStoredAge(); sections?.forEach((section) => { const frontendSlug = toFrontendSlug(section.slug); const progress = Math.max(0, Math.min(100, Math.round(section.completion_percent))); progressBySlug.set(frontendSlug, progress); progressBySlug.set(section.slug, progress); }); questionListItems.forEach((item) => { const localProgress = getLocalSectionProgress(item, profile, age); if (localProgress !== null) { progressBySlug.set(item.slug, localProgress); } }); return progressBySlug; }, [sections, questionListItems, profile]); const requiredQuestionListItems = useMemo( () => questionListItems.filter((item) => Boolean(item.required)), [questionListItems], ); const allRequiredSectionsCompleted = useMemo(() => { if (requiredQuestionListItems.length === 0) { return false; } return requiredQuestionListItems.every((item) => { const progress = sectionProgressBySlug.get(item.slug) ?? 0; return progress >= 100; }); }, [requiredQuestionListItems, sectionProgressBySlug]); const profileStatus = profile?.status; const isProfileSuspended = profileStatus === "suspended"; const isProfileBlocked = profileStatus === "in_case" || profileStatus === "matched"; const canStartMatch = !isProfileSuspended && !isProfileBlocked && allRequiredSectionsCompleted; const isStartMatchDisabled = startMatchMutation.isPending || !canStartMatch; const hasIncompleteOptionalSections = useMemo(() => { return questionListItems.some( (item) => !item.required && (sectionProgressBySlug.get(item.slug) ?? 0) < 100, ); }, [questionListItems, sectionProgressBySlug]); const handleStartMatch = () => { if (isStartMatchDisabled) { return; } startMatchMutation.mutate(); }; return ( <> {isOptionalInfoSheetOpen ? ( {t.questions.optionalInfoPromptDescription}

} onClose={() => setIsOptionalInfoSheetOpen(false)} buttons={({ close }) => (
)} /> ) : null} {selectedSection ? ( ( {selectedSection.title} )} description={

{selectedSection.summary}

} onClose={() => setSelectedSection(null)} className="text-left" /> ) : null}

{t.questions.profileRegistration}

{questionListItems.map((item) => ( setSelectedSection(section)} /> ))}
{startMatchMutation.isError ? (

{t.questions.startMatchFailed}

) : null}
); }