"use client"; import { useEffect, useState } from "react"; import { LoadingCard, Skeleton } from "@/components/ui/loading-skeleton"; import { AlertBanner } from "@/components/common/AlertBanner"; import { MapPinIcon, PencilIcon, CheckIcon, XMarkIcon, UserIcon } from "@heroicons/react/24/outline"; import { useAuthStore } from "@/features/auth/services/auth.store"; import { accountService } from "@/features/account/services/account.service"; import { useProfileEdit } from "@/features/account/hooks/useProfileEdit"; import { AddressForm } from "@/features/catalog/components/base/AddressForm"; import { Button } from "@/components/ui/button"; import { useAddressEdit } from "@/features/account/hooks/useAddressEdit"; export default function ProfileContainer() { const { user } = useAuthStore(); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [editingProfile, setEditingProfile] = useState(false); const [editingAddress, setEditingAddress] = useState(false); const profile = useProfileEdit({ firstName: user?.firstName || "", lastName: user?.lastName || "", phone: user?.phone || "", }); const address = useAddressEdit({ street: "", streetLine2: "", city: "", state: "", postalCode: "", country: "", }); useEffect(() => { void (async () => { try { setLoading(true); const [addr, prof] = await Promise.all([ accountService.getAddress().catch(() => null), accountService.getProfile().catch(() => null), ]); if (addr) { address.setValues({ street: addr.street ?? "", streetLine2: addr.streetLine2 ?? "", city: addr.city ?? "", state: addr.state ?? "", postalCode: addr.postalCode ?? "", country: addr.country ?? "", }); } if (prof) { profile.setValues({ firstName: prof.firstName || "", lastName: prof.lastName || "", phone: prof.phone || "", }); useAuthStore.setState(state => ({ ...state, user: state.user ? { ...state.user, firstName: prof.firstName || state.user.firstName, lastName: prof.lastName || state.user.lastName, phone: prof.phone || state.user.phone, } : (prof as unknown as typeof state.user), })); } } catch (e) { setError(e instanceof Error ? e.message : "Failed to load profile data"); } finally { setLoading(false); } })(); }, [user?.id]); if (loading) { return (
{Array.from({ length: 4 }).map((_, i) => (
))}
); } return (
{error && ( {error} )}

Personal Information

{!editingProfile && ( )}
{editingProfile ? ( profile.setValue("firstName", e.target.value)} className="block w-full px-4 py-3 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-colors" /> ) : (

{user?.firstName || Not provided}

)}
{editingProfile ? ( profile.setValue("lastName", e.target.value)} className="block w-full px-4 py-3 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-colors" /> ) : (

{user?.lastName || Not provided}

)}

{user?.email}

Email cannot be changed from the portal.

{editingProfile ? ( profile.setValue("phone", e.target.value)} placeholder="+81 XX-XXXX-XXXX" className="block w-full px-4 py-3 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-colors" /> ) : (

{user?.phone || Not provided}

)}
{editingProfile && (
)}

Address Information

{!editingAddress && ( )}
{editingAddress ? (
address.setValues({ street: a.street, streetLine2: a.streetLine2, city: a.city, state: a.state, postalCode: a.postalCode, country: a.country, }) } title="Mailing Address" />
{address.submitError && ( {address.submitError} )}
) : (
{address.values.street || address.values.city ? (
{address.values.street &&

{address.values.street}

} {address.values.streetLine2 &&

{address.values.streetLine2}

}

{[address.values.city, address.values.state, address.values.postalCode] .filter(Boolean) .join(", ")}

{address.values.country}

) : (

No address on file

)}
)}
); }