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 { OAuthProfilePrefill } 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; prefill?: OAuthProfilePrefill; onClearPrefill?: () => void; } type OAuthAuthFlash = { status?: string | null; error?: string | null; }; const GoogleIcon: React.FC<{ className?: string }> = ({ className }) => ( ); const FacebookIcon: React.FC<{ className?: string }> = ({ className }) => ( ); export const AuthStep: React.FC = ({ privacyHtml, prefill, onClearPrefill }) => { const { t } = useTranslation('marketing'); const page = usePage<{ locale?: string }>(); const locale = page.props.locale ?? "de"; const googleAuth = useMemo(() => { const props = page.props as { googleAuth?: OAuthAuthFlash }; return props.googleAuth ?? {}; }, [page.props]); const facebookAuth = useMemo(() => { const props = page.props as { facebookAuth?: OAuthAuthFlash }; return props.facebookAuth ?? {}; }, [page.props]); const { isAuthenticated, authUser, setAuthUser, nextStep, selectedPackage } = useCheckoutWizard(); const [mode, setMode] = useState<'login' | 'register'>('register'); const [isRedirectingToGoogle, setIsRedirectingToGoogle] = useState(false); const [isRedirectingToFacebook, setIsRedirectingToFacebook] = useState(false); const [showGoogleHelper, setShowGoogleHelper] = useState(false); const showOauthControls = mode === 'register'; const authErrorTitle = facebookAuth?.error ? t('checkout.auth_step.facebook_error_title') : t('checkout.auth_step.google_error_title'); useEffect(() => { if (googleAuth?.status === 'signin') { toast.success(t('checkout.auth_step.google_success_toast')); onClearPrefill?.(); } }, [googleAuth?.status, onClearPrefill, t]); useEffect(() => { if (facebookAuth?.status === 'signin') { toast.success(t('checkout.auth_step.facebook_success_toast')); onClearPrefill?.(); } }, [facebookAuth?.status, onClearPrefill, t]); useEffect(() => { if (googleAuth?.error) { toast.error(googleAuth.error); } }, [googleAuth?.error]); useEffect(() => { if (facebookAuth?.error) { toast.error(facebookAuth.error); } }, [facebookAuth?.error]); useEffect(() => { if (mode !== 'login') { return; } if (showGoogleHelper) { setShowGoogleHelper(false); } }, [mode, showGoogleHelper]); 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), }); onClearPrefill?.(); 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), }); } onClearPrefill?.(); 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]); const handleFacebookLogin = useCallback(() => { if (!selectedPackage) { toast.error(t('checkout.auth_step.facebook_missing_package')); return; } setIsRedirectingToFacebook(true); const params = new URLSearchParams({ package_id: String(selectedPackage.id), locale, }); window.location.href = `/checkout/auth/facebook?${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 (
{showOauthControls && (
)}
{showOauthControls && ( {t('checkout.auth_step.google_helper')} )}
{(googleAuth?.error || facebookAuth?.error) && ( {authErrorTitle} {facebookAuth?.error ?? googleAuth?.error} )}
{mode === 'register' ? ( selectedPackage && ( ) ) : ( )}
); };