"use client"; import Image from "next/image"; import { useRouter } from "next/navigation"; import { useEffect, useMemo, useState } from "react"; import Button from "@/components/ui/button"; import DismissReasonSheet from "@/components/ui/dismiss-reason-sheet"; import FemaleConsentSheet from "@/components/ui/female-consent-sheet"; import InformationSheet from "@/components/ui/information-sheet"; import NavigationButton from "@/components/ui/navigation-button"; import StickyHeader from "@/components/ui/sticky-header"; import { PageBackground } from "@/components/utils/page-background"; import type { MarriageCaseStatus, MarriageField, MarriageFieldValue, MarriageGender, MarriagePhoneFieldValue, } from "@/hooks/marriage/types"; import { useRespondToMarriageCaseMutation } from "@/hooks/marriage/use-case-respond"; import { useMarriageProfileQuery } from "@/hooks/marriage/use-profile-main"; import { getSubmitPath } from "@/lib/get-submit-path"; import { localizePath } from "@/translations/config"; import { useI18n } from "@/translations/provider"; function formatFieldValue(value: MarriageFieldValue) { if (value === null || value === "") { return null; } if (isMarriagePhoneFieldValue(value)) { return `+${value.countryCode}${value.phoneNumber}`; } if (typeof value === "boolean") { return value ? "Yes" : "No"; } return String(value); } function isMarriagePhoneFieldValue( value: unknown, ): value is MarriagePhoneFieldValue { if (!value || typeof value !== "object") { return false; } const phoneValue = value as Partial; return ( typeof phoneValue.countryCode === "string" && typeof phoneValue.phoneNumber === "string" ); } function titleFromKey(key: string) { return key .replace(/^q\d+[_-]?/i, "") .replace(/[_-]+/g, " ") .replace(/\s+/g, " ") .trim() .replace(/\b\w/g, (letter) => letter.toUpperCase()); } function isImageField(field: MarriageField) { return /(avatar|image|photo|picture|portrait|upload)/i.test( `${field.key} ${field.label}`, ); } function canAcceptProfile( gender: MarriageGender | null | undefined, status: MarriageCaseStatus | null | undefined, ) { if (!gender || !status) { return false; } if (gender === "female") { return status === "introduced" || status === "male_accepted"; } return status === "introduced"; } function MatchField({ field }: { field: MarriageField }) { const value = formatFieldValue(field.value); if (!value || isImageField(field)) { return null; } const label = field.label || titleFromKey(field.key); return (

{label}

{value}

); } function MatchPublicProfileFields({ publicInfo, }: { publicInfo: MarriageField[] | null | undefined; }) { const visibleFields = useMemo(() => { if (!publicInfo) return []; return publicInfo.filter((field) => { if (field.value === null || field.value === "" || isImageField(field)) { return false; } if ((field as any).private === true) { return false; } return true; }); }, [publicInfo]); if (!visibleFields.length) { return (

اطلاعات عمومی قابل نمایشی ثبت نشده است.

); } return (

اطلاعات عمومی و مشخصات فردی

{visibleFields.map((field) => ( ))}
); } export default function NewMatchProfilePage() { const { dictionary: t, locale } = useI18n(); const router = useRouter(); const [isRequestSheetOpen, setIsRequestSheetOpen] = useState(false); const [isFemaleConsentChecked, setIsFemaleConsentChecked] = useState(false); const [isRejectSheetOpen, setIsRejectSheetOpen] = useState(false); const [isDismissReasonSheetOpen, setIsDismissReasonSheetOpen] = useState(false); const { data: profile, refetch: refetchProfile } = useMarriageProfileQuery(); useEffect(() => { if (!profile) { return; } const targetPath = getSubmitPath(profile); if (targetPath !== "/new-match") { router.replace(localizePath(targetPath, locale)); } }, [profile, locale, router]); const caseId = profile?.active_case?.case_id; const caseStatus = profile?.active_case?.status; const isFemaleProfile = profile?.gender === "female"; const isMaleAccepted = caseStatus === "male_accepted"; const respondMutation = useRespondToMarriageCaseMutation(caseId ?? "", { onSuccess: async (_, variables) => { if (variables.action === "accept") { const { data: updatedProfile } = await refetchProfile(); const nextPath = getSubmitPath(updatedProfile); router.replace(localizePath(nextPath, locale)); return; } router.replace(localizePath("/finding-match", locale)); }, }); const candidateName = useMemo(() => { const publicInfo = profile?.match_summary?.public_info ?? []; const nameField = publicInfo.find( (f) => f.key === "q1_full_name" || f.key.toLowerCase().includes("name") || f.key.toLowerCase().includes("nam") || f.label.includes("نام"), ); const formatted = formatFieldValue(nameField?.value ?? null); if (formatted) return formatted; const firstVal = publicInfo.find((f) => Boolean(f.value))?.value ?? null; return formatFieldValue(firstVal) || "نامشخص"; }, [profile?.match_summary?.public_info]); const isSubmitting = respondMutation.isPending; const isAcceptProfileEnabled = Boolean(caseId) && !isSubmitting && canAcceptProfile(profile?.gender, caseStatus); return ( <> {isRequestSheetOpen ? ( isFemaleProfile ? ( (
)} onClose={() => { setIsFemaleConsentChecked(false); setIsRequestSheetOpen(false); }} /> ) : ( (
)} onClose={() => setIsRequestSheetOpen(false)} /> ) ) : null} {isRejectSheetOpen ? ( (
)} onClose={() => setIsRejectSheetOpen(false)} /> ) : null} {isDismissReasonSheetOpen ? ( setIsDismissReasonSheetOpen(false)} onSubmit={async (reason) => { if (!caseId) { return; } await respondMutation.mutateAsync({ action: "reject", custom_note: reason, }); }} /> ) : null}

{t.match.title}

{candidateName}

{candidateName}

); }