import React, { useCallback, useEffect, useMemo, useState } from "react"; import { usePage } from "@inertiajs/react"; import { Button } from "@/components/ui/button"; import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; import { useCheckoutWizard } from "../WizardContext"; import type { GoogleProfilePrefill } from '../types'; import LoginForm, { AuthUserPayload } from "../../../auth/LoginForm"; import RegisterForm, { RegisterSuccessPayload } from "../../../auth/RegisterForm"; import { Trans, useTranslation } from 'react-i18next'; import toast from 'react-hot-toast'; import { ChevronDown, LoaderCircle } from "lucide-react"; import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@/components/ui/collapsible"; import { cn } from "@/lib/utils"; interface AuthStepProps { privacyHtml: string; googleProfile?: GoogleProfilePrefill; onClearGoogleProfile?: () => void; } type GoogleAuthFlash = { status?: string | null; error?: string | null; }; const GoogleIcon: React.FC<{ className?: string }> = ({ className }) => ( ); export const AuthStep: React.FC = ({ privacyHtml, googleProfile, onClearGoogleProfile }) => { const { t } = useTranslation('marketing'); const page = usePage<{ locale?: string }>(); const locale = page.props.locale ?? "de"; const googleAuth = useMemo(() => { const props = page.props as Record; return props.googleAuth ?? {}; }, [page.props]); const { isAuthenticated, authUser, setAuthUser, nextStep, selectedPackage } = useCheckoutWizard(); const [mode, setMode] = useState<'login' | 'register'>('register'); const [isRedirectingToGoogle, setIsRedirectingToGoogle] = useState(false); const [showGoogleHelper, setShowGoogleHelper] = useState(false); useEffect(() => { if (googleAuth?.status === 'signin') { toast.success(t('checkout.auth_step.google_success_toast')); onClearGoogleProfile?.(); } }, [googleAuth?.status, onClearGoogleProfile, t]); useEffect(() => { if (googleAuth?.error) { toast.error(googleAuth.error); } }, [googleAuth?.error]); const handleLoginSuccess = (payload: AuthUserPayload | null) => { if (!payload) { return; } setAuthUser({ id: payload.id ?? 0, email: payload.email ?? "", name: payload.name ?? undefined, pending_purchase: Boolean(payload.pending_purchase), }); onClearGoogleProfile?.(); nextStep(); }; const handleRegisterSuccess = (result: RegisterSuccessPayload) => { const nextUser = result?.user ?? null; if (nextUser) { setAuthUser({ id: nextUser.id ?? 0, email: nextUser.email ?? "", name: nextUser.name ?? undefined, pending_purchase: Boolean(result?.pending_purchase ?? nextUser.pending_purchase), }); } onClearGoogleProfile?.(); nextStep(); }; const handleGoogleLogin = useCallback(() => { if (!selectedPackage) { toast.error(t('checkout.auth_step.google_missing_package')); return; } setIsRedirectingToGoogle(true); const params = new URLSearchParams({ package_id: String(selectedPackage.id), locale, }); window.location.href = `/checkout/auth/google?${params.toString()}`; }, [locale, selectedPackage, t]); if (isAuthenticated && authUser) { return (
{t('checkout.auth_step.already_logged_in_title')} }} values={{ email: authUser?.email || '' }} />

{t('checkout.auth_step.already_logged_in_hint')}

); } return (
{t('checkout.auth_step.google_helper')}
{googleAuth?.error && ( {t('checkout.auth_step.google_error_title')} {googleAuth.error} )}
{mode === 'register' ? ( selectedPackage && ( ) ) : ( )}
); };