/* global React, Icon */ const { useState, useEffect } = React; function ProfilePage({ navigate }) { const [profile, setProfile] = useState(null); const [addresses, setAddresses] = useState([]); const [tab, setTab] = useState('profile'); // profile | addresses const [form, setForm] = useState({ full_name: '', phone: '' }); const [loading, setLoading] = useState(false); const [saved, setSaved] = useState(false); const [error, setError] = useState(null); const [editingAddr, setEditingAddr] = useState(null); const [editAddrForm, setEditAddrForm] = useState({}); useEffect(() => { if (!window.IndieAuth.isLoggedIn()) { navigate('/gallery'); return; } const p = window.IndieAuth.getProfile(); setProfile(p); setForm({ full_name: p?.full_name || '', phone: p?.phone || '' }); window.IndieAuth.loadAddresses().then(setAddresses); }, []); const saveProfile = async () => { setLoading(true); setError(null); setSaved(false); try { const sb = window.supabase.createClient( 'https://indie.supabase.co', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Indsc3pqdnZveXltd2dwbXRnd2V4Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3NzQ1ODg5OTcsImV4cCI6MjA5MDE2NDk5N30.5emUfc2jlLxutLhtGZFD0Rq3MOUl4JAcU3Fd5RydjRU' ); const session = window.IndieAuth.getSession(); await sb.from('customer_profiles') .update({ full_name: form.full_name, phone: form.phone }) .eq('id', session.user.id); setSaved(true); setTimeout(() => setSaved(false), 3000); } catch(e) { setError(e.message); } finally { setLoading(false); } }; const deleteAddress = async (id) => { const sb = window.supabase.createClient( 'https://indie.supabase.co', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Indsc3pqdnZveXltd2dwbXRnd2V4Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3NzQ1ODg5OTcsImV4cCI6MjA5MDE2NDk5N30.5emUfc2jlLxutLhtGZFD0Rq3MOUl4JAcU3Fd5RydjRU' ); await sb.from('customer_addresses').delete().eq('id', id); setAddresses(a => a.filter(x => x.id !== id)); }; const saveEditAddr = async () => { if (!editingAddr) return; setLoading(true); try { await window.IndieAuth.saveAddress({ ...editAddrForm, id: editingAddr }); const updated = await window.IndieAuth.loadAddresses(); setAddresses(updated); setEditingAddr(null); } catch(e) { setError(e.message); } finally { setLoading(false); } }; if (!profile) return (
Memuat profil…
); return (
{[['profile','Profil saya'],['addresses','Alamat']].map(([id,label]) => ( ))}
{tab === 'profile' && (
setForm(f=>({...f,full_name:e.target.value}))} style={{width:'100%',boxSizing:'border-box',padding:'10px 12px',border:'1px solid var(--paper-card-edge)',fontFamily:'var(--font-sans)',fontSize:14,color:'var(--ink)',outline:'none'}} />
setForm(f=>({...f,phone:e.target.value}))} placeholder="+62 81..." style={{width:'100%',boxSizing:'border-box',padding:'10px 12px',border:'1px solid var(--paper-card-edge)',fontFamily:'var(--font-sans)',fontSize:14,color:'var(--ink)',outline:'none'}} />
{error &&
{error}
} {saved &&
Profil berhasil disimpan!
}
)} {tab === 'addresses' && (
{addresses.length === 0 ? (

Belum ada alamat tersimpan.

) : (
{addresses.map(addr => (
{editingAddr === addr.id ? (
{[ ['label','Label (contoh: Rumah, Kantor)'], ['street','Alamat lengkap'], ['city','Kota'], ['province','Provinsi'], ['postal_code','Kode pos'], ].map(([key, placeholder]) => ( setEditAddrForm(f => ({...f, [key]: e.target.value}))} placeholder={placeholder} style={{width:'100%',boxSizing:'border-box',padding:'8px 10px',border:'1px solid var(--paper-card-edge)',fontFamily:'var(--font-sans)',fontSize:13,outline:'none'}} /> ))}
) : (
{addr.label || 'Alamat'}
{addr.street}
{addr.city}{addr.province ? ', '+addr.province : ''} {addr.postal_code}
)}
))}
)}
)}
); } window.ProfilePage = ProfilePage;