diff --git a/app/carnet/page.tsx b/app/carnet/page.tsx index 23f080e9..0b65bd7f 100644 --- a/app/carnet/page.tsx +++ b/app/carnet/page.tsx @@ -431,6 +431,78 @@ export default function CarnetPage() { } }; + const handleContactSave = async (contact: Contact) => { + try { + const vcfContent = generateVCard(contact); + const response = await fetch('/api/nextcloud/files', { + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + path: `/files/cube-${session?.user?.id}/Private/Contacts/${selectedFolder}`, + content: vcfContent + }), + }); + + if (!response.ok) { + throw new Error('Failed to save contact'); + } + + // Refresh contacts list + fetchContacts(selectedFolder); + } catch (error) { + console.error('Error saving contact:', error); + } + }; + + const handleContactDelete = async (contact: Contact) => { + if (!confirm('Êtes-vous sûr de vouloir supprimer ce contact ?')) { + return; + } + + try { + const response = await fetch('/api/nextcloud/files', { + method: 'DELETE', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + path: `/files/cube-${session?.user?.id}/Private/Contacts/${selectedFolder}` + }), + }); + + if (!response.ok) { + throw new Error('Failed to delete contact'); + } + + // Clear selected contact and refresh list + setSelectedContact(null); + fetchContacts('Contacts'); + } catch (error) { + console.error('Error deleting contact:', error); + } + }; + + const generateVCard = (contact: Contact): string => { + const lines = [ + 'BEGIN:VCARD', + 'VERSION:3.0', + `UID:${contact.id}`, + 'PRODID:-//Infomaniak//Workspace//pim', + contact.fullName ? `FN:${contact.fullName}` : '', + contact.fullName ? `N:${contact.fullName.split(' ').reverse().join(';')};` : '', + contact.organization ? `ORG:${contact.organization}` : '', + contact.email ? `EMAIL;TYPE=WORK:${contact.email}` : '', + contact.phone ? `TEL;TYPE=WORK:${contact.phone}` : '', + contact.address ? `ADR;TYPE=WORK:;;${contact.address};;;;` : '', + contact.notes ? `NOTE:${contact.notes}` : '', + 'END:VCARD' + ].filter(Boolean); + + return lines.join('\n'); + }; + if (isLoading) { return (
{label}
+ {isEditing ? ( + setEditedContact(prev => prev ? {...prev, [field]: e.target.value} : null)} + className="w-full text-sm text-carnet-text-primary bg-transparent border-b border-primary focus:outline-none" + /> + ) : ( +{value}
+ )} +- {contact.organization} -
+ {isEditing ? ( + setEditedContact(prev => prev ? {...prev, fullName: e.target.value} : null)} + className="text-xl font-semibold text-carnet-text-primary bg-transparent border-b border-primary focus:outline-none" + placeholder="Nom complet" + /> + ) : ( +{contact.email}
-Téléphone
-{contact.phone}
-Organisation
-{contact.organization}
-Adresse
-{contact.address}
-{contact.notes}
-