'use client'; import { useEffect, useState } from 'react'; import { useAuth } from '@/lib/context/auth-context'; import { getOrganization, updateOrganization } from '@/lib/api/organizations'; import type { OrganizationResponse } from '@/types/api'; interface OrganizationForm { name: string; siren: string; eori: string; contact_phone: string; contact_email: string; address_street: string; address_city: string; address_postal_code: string; address_country: string; } type TabType = 'information' | 'address'; export default function OrganizationSettingsPage() { const { user } = useAuth(); const [activeTab, setActiveTab] = useState('information'); const [organization, setOrganization] = useState(null); const [formData, setFormData] = useState({ name: '', siren: '', eori: '', contact_phone: '', contact_email: '', address_street: '', address_city: '', address_postal_code: '', address_country: 'FR', }); const [isLoading, setIsLoading] = useState(true); const [isSaving, setIsSaving] = useState(false); const [error, setError] = useState(null); const [successMessage, setSuccessMessage] = useState(null); useEffect(() => { if (user?.organizationId) { loadOrganization(); } }, [user?.organizationId]); const loadOrganization = async () => { try { setIsLoading(true); setError(null); const org = await getOrganization(user!.organizationId); setOrganization(org); setFormData({ name: org.name || '', siren: org.siren || '', eori: org.eori || '', contact_phone: org.contact_phone || '', contact_email: org.contact_email || '', address_street: org.address?.street || '', address_city: org.address?.city || '', address_postal_code: org.address?.postalCode || '', address_country: org.address?.country || 'FR', }); } catch (err) { console.error('Failed to load organization:', err); setError(err instanceof Error ? err.message : 'Erreur lors du chargement'); } finally { setIsLoading(false); } }; const handleChange = (field: keyof OrganizationForm, value: string) => { setFormData(prev => ({ ...prev, [field]: value })); setSuccessMessage(null); }; const handleCancel = () => { if (organization) { setFormData({ name: organization.name || '', siren: organization.siren || '', eori: organization.eori || '', contact_phone: organization.contact_phone || '', contact_email: organization.contact_email || '', address_street: organization.address?.street || '', address_city: organization.address?.city || '', address_postal_code: organization.address?.postalCode || '', address_country: organization.address?.country || 'FR', }); setSuccessMessage(null); setError(null); } }; const handleSave = async () => { if (!user?.organizationId) return; try { setIsSaving(true); setError(null); setSuccessMessage(null); const updatedOrg = await updateOrganization(user.organizationId, { name: formData.name, siren: formData.siren, eori: formData.eori, contact_phone: formData.contact_phone, contact_email: formData.contact_email, address: { street: formData.address_street, city: formData.address_city, postalCode: formData.address_postal_code, country: formData.address_country, }, }); setOrganization(updatedOrg); setSuccessMessage('Informations sauvegardées avec succès'); } catch (err) { console.error('Failed to update organization:', err); setError(err instanceof Error ? err.message : 'Erreur lors de la sauvegarde'); } finally { setIsSaving(false); } }; if (isLoading) { return (

Chargement...

); } if (!organization) { return (

Erreur

{error || "Impossible de charger l'organisation"}

); } return (
{/* Header */}

Paramètres de l'organisation

Gérez les informations de votre organisation

{/* Success Message */} {successMessage && (

{successMessage}

)} {/* Error Message */} {error && (

{error}

)} {/* Tabs */}
{/* Tab Content */}
{activeTab === 'information' && (
{/* Nom de la société */}
handleChange('name', e.target.value)} className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="Xpeditis" required />
{/* SIREN */}
handleChange('siren', e.target.value.replace(/\D/g, '').slice(0, 9))} className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="123 456 789" maxLength={9} />

9 chiffres

{/* Numéro EORI */}
handleChange('eori', e.target.value.toUpperCase())} className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="FR123456789" maxLength={17} />

Code pays (2 lettres) + numéro unique (max 15 caractères)

{/* Téléphone */}
handleChange('contact_phone', e.target.value)} className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="+33 6 80 18 28 12" />
{/* Email */}
handleChange('contact_email', e.target.value)} className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="contact@xpeditis.com" />
)} {activeTab === 'address' && (
{/* Rue */}
handleChange('address_street', e.target.value)} className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="123 Rue de la Paix" required />
{/* Ville et Code postal */}
handleChange('address_postal_code', e.target.value)} className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="75001" required />
handleChange('address_city', e.target.value)} className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="Paris" required />
{/* Pays */}
)}
{/* Actions */}
); }