import Area from '@components/common/Area.js'; import { EmailField } from '@components/common/form/EmailField.js'; import { PasswordField } from '@components/common/form/PasswordField.js'; import { isValidEmail } from '@components/common/form/utils/isValidEmail.js'; import { Button } from '@components/common/ui/Button.js'; import { Card, CardContent, CardHeader, CardTitle } from '@components/common/ui/Card.js'; import { Item, ItemActions, ItemContent, ItemDescription, ItemTitle } from '@components/common/ui/Item.js'; import { toast } from '@components/common/ui/Sonner.js'; import { useCartDispatch, useCartState } from '@components/frontStore/cart/CartContext.js'; import { useCheckout, useCheckoutDispatch } from '@components/frontStore/checkout/CheckoutContext.js'; import { useCustomer, useCustomerDispatch } from '@components/frontStore/customer/CustomerContext.jsx'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import React, { useEffect, useRef, useState } from 'react'; const LoggedIn: React.FC<{ fullName: string; email: string; uuid: string; }> = ({ uuid, fullName, email }) => { const [isLoggingOut, setIsLoggingOut] = useState(false); const { logout } = useCustomerDispatch(); const { updateCheckoutData } = useCheckoutDispatch(); useEffect(() => { updateCheckoutData({ customer: { id: uuid, email: email, fullName: fullName } }); }, [fullName, email]); const handleLogout = async () => { if (isLoggingOut) return; try { setIsLoggingOut(true); await logout(); toast.success(_('Successfully logged out')); } catch (error) { const errorMessage = error instanceof Error ? error.message : _('Logout failed'); toast.error(errorMessage); } finally { setIsLoggingOut(false); } }; return (
{_('Logged in as')} {fullName}
{email}
); }; const Guest: React.FC<{ email: string; }> = ({ email }) => { const [showLogin, setShowLogin] = useState(false); const [isLogging, setIsLogging] = useState(false); const { login } = useCustomerDispatch(); const { form } = useCheckout(); const { updateCheckoutData } = useCheckoutDispatch(); const contactEmail = form.watch('contact.email', email); const handleLoginClick = (e: React.MouseEvent) => { e.preventDefault(); setShowLogin(true); }; useEffect(() => { updateCheckoutData({ customer: { email: contactEmail } }); }, [contactEmail]); // Save the email to the cart as soon as it is entered, not only with the // order, so the cart has it if the shopper leaves before ordering. On the // DOM `change` event, not React's onChange (an `input` event): the browser // fires it when the shopper leaves the field after changing it and when // autofill fills it, never on each key, so a half-typed address is not // saved. A failed save is not shown: the order sends the email anyway. const { addContactInfo } = useCartDispatch(); const emailFieldRef = useRef(null); const savedEmail = useRef(email); useEffect(() => { const wrapper = emailFieldRef.current; if (!wrapper) { return undefined; } const save = (event: Event) => { // An email input drops the spaces around its value itself const { value } = event.target as HTMLInputElement; if (!isValidEmail(value) || value === savedEmail.current) { return; } const previous = savedEmail.current; savedEmail.current = value; addContactInfo({ email: value }).catch(() => { // Let the next change try again if (savedEmail.current === value) { savedEmail.current = previous; } }); }; wrapper.addEventListener('change', save); return () => wrapper.removeEventListener('change', save); }, [addContactInfo]); const handleLogin = async () => { if (isLogging) return; try { setIsLogging(true); const isValid = await form.trigger(['contact.email', 'contact.password']); if (!isValid) { return; } const formData = form.getValues(); const loginEmail = formData?.contact?.email; const password = formData?.contact?.password; await login( { email: loginEmail, password: password }, window.location.href ); toast.success(_('Successfully logged in')); setShowLogin(false); } catch (error) { const errorMessage = error instanceof Error ? error.message : _('Login failed'); toast.error(errorMessage); } finally { setIsLogging(false); } }; const handleCancelLogin = () => { setShowLogin(false); // Clear password field form.setValue('contact.password', ''); }; return (
{showLogin && (
)} {!showLogin && (

{_('Already have an account?')}{' '}

)}
); }; export function ContactInformation() { const { customer } = useCustomer(); const { data: cart } = useCartState(); return ( <>
1 {_('Contact Information')}
{customer ? ( ) : ( )}
); }