"use client"; import { useRouter } from "next/navigation"; import type { TouchEvent } from "react"; import { useEffect, useState } from "react"; import { useUpdateMarriageProfileBasicMutation } from "@/hooks/marriage/use-profile-basic"; import { localizePath } from "@/translations/config"; import { useI18n } from "@/translations/provider"; import Button from "./button"; import NavigationButton from "./navigation-button"; import type { GenderAnswer, RegistrationAnswer } from "./slider-slide"; import { SliderSlideFive } from "./slider-slide-five"; import { SliderSlideFour } from "./slider-slide-four"; import { SliderSlideOne, SliderSlideOneActions } from "./slider-slide-one"; import { SliderSlideThree } from "./slider-slide-three"; import { SliderSlideTwo } from "./slider-slide-two"; const FINAL_SLIDE_COUNT = 5; const SWIPE_THRESHOLD = 40; const SLIDER_ANSWERS_STORAGE_KEY = "marriage-slider-answers"; export default function SliderPage() { const router = useRouter(); const { locale } = useI18n(); const [activeSlide, setActiveSlide] = useState(0); const [selectedGender, setSelectedGender] = useState("woman"); const [selectedRegistration, setSelectedRegistration] = useState("self"); const [hasReadRules, setHasReadRules] = useState(false); const [touchStartX, setTouchStartX] = useState(null); const updateProfileBasicMutation = useUpdateMarriageProfileBasicMutation(); const hasFinalNotice = true; const maxSlideIndex = FINAL_SLIDE_COUNT - 1; const displaySlideCount = FINAL_SLIDE_COUNT; const navDotCount = displaySlideCount; useEffect(() => { // Clear any previous slider answers on mount so abandoned onboarding starts clean if (typeof window !== "undefined") { window.localStorage.removeItem(SLIDER_ANSWERS_STORAGE_KEY); } }, []); const goToSlide = (index: number) => { setActiveSlide(Math.max(0, Math.min(index, maxSlideIndex))); }; const goToPreviousSlide = () => { goToSlide(activeSlide - 1); }; const goToNextSlide = () => { goToSlide(activeSlide + 1); }; const completeSlider = async () => { const navigateAway = () => { if (typeof window !== "undefined") { window.localStorage.removeItem(SLIDER_ANSWERS_STORAGE_KEY); // Use hard navigation instead of router.push() to ensure the // destination page loads with a fresh auth state. SPA (soft) // navigation sometimes leaves stale query-cache / token state // that causes the profile query to return 401 and the page to // stay stuck on the loading spinner. window.location.href = localizePath( "/questions-list/personal_info", locale, ); return; } router.push(localizePath("/questions-list/personal_info", locale)); }; // Safety timeout — navigate after 5 seconds even if the request hangs const timeout = setTimeout(navigateAway, 5000); try { await updateProfileBasicMutation.mutateAsync({ gender: selectedGender === "man" ? "male" : "female", is_registering_for_self: selectedRegistration === "self", }); } catch (error) { console.warn("Failed to update profile basic details:", error); } finally { clearTimeout(timeout); navigateAway(); } }; const handleTouchStart = (event: TouchEvent) => { setTouchStartX(event.touches[0]?.clientX ?? null); }; const handleTouchEnd = (event: TouchEvent) => { if (touchStartX === null) { return; } const touchEndX = event.changedTouches[0]?.clientX ?? touchStartX; const deltaX = touchStartX - touchEndX; if (Math.abs(deltaX) >= SWIPE_THRESHOLD) { if (deltaX > 0) { goToNextSlide(); } else { goToPreviousSlide(); } } setTouchStartX(null); }; return (
router.back()} />
setHasReadRules(true)} /> {hasFinalNotice ? : null}
{activeSlide === 0 ? ( ) : activeSlide === maxSlideIndex ? ( ) : ( )}
); } type SliderStepActionsProps = { onBack: () => void; onNext: () => void; }; function SliderStepActions({ onBack, onNext }: SliderStepActionsProps) { return (
); } type SliderFinalActionsProps = { isFinishing?: boolean; onBack: () => void; onFinish: () => void | Promise; }; function SliderFinalActions({ isFinishing = false, onBack, onFinish, }: SliderFinalActionsProps) { return (
); }