From c15c6154282d88e096403685fc0d5039c787f861 Mon Sep 17 00:00:00 2001 From: alma Date: Sun, 20 Apr 2025 22:40:31 +0200 Subject: [PATCH] carnet panel contact --- app/carnet/page.tsx | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/app/carnet/page.tsx b/app/carnet/page.tsx index 1255b0c8..a20a97dc 100644 --- a/app/carnet/page.tsx +++ b/app/carnet/page.tsx @@ -381,29 +381,34 @@ export default function CarnetPage() { try { setIsLoading(true); - // Determine the correct VCF file path + // Always use Allemanique.vcf for new contacts const basePath = `/files/cube-${session.user.id}/Private/Contacts`; - const vcfFile = contact.group ? `${contact.group}.vcf` : 'contacts.vcf'; + const vcfFile = 'Allemanique.vcf'; const path = `${basePath}/${vcfFile}`; - // Get existing contacts from the VCF file - const response = await fetch(`/api/nextcloud/files/content?path=${encodeURIComponent(path)}`); - if (!response.ok) { - throw new Error('Failed to fetch contacts'); + let vcfContent = ''; + let existingContacts: string[] = []; + + try { + // Try to get existing contacts from the VCF file + const response = await fetch(`/api/nextcloud/files/content?path=${encodeURIComponent(path)}`); + if (response.ok) { + const { content } = await response.json(); + // Split the content into individual vCards + existingContacts = content.split('BEGIN:VCARD') + .filter(section => section.trim()) + .map(section => 'BEGIN:VCARD' + section.trim()); + } + } catch (error) { + // If the file doesn't exist, we'll create it with just the new contact + console.log('No existing VCF file found, will create a new one'); } - const { content } = await response.json(); - - // Split the content into individual vCards - const vcards = content.split('BEGIN:VCARD') - .filter(section => section.trim()) - .map(section => 'BEGIN:VCARD' + section.trim()); - // Update or add the contact let updatedVcards: string[] = []; let contactUpdated = false; - for (const vcard of vcards) { + for (const vcard of existingContacts) { const parsed = parseVCard(vcard); if (parsed.uid?.[0]?.value === contact.id) { // Replace the existing contact @@ -417,7 +422,6 @@ export default function CarnetPage() { ...(contact.organization ? [`ORG:${contact.organization}`] : []), ...(contact.address ? [`ADR:${contact.address}`] : []), ...(contact.notes ? [`NOTE:${contact.notes}`] : []), - ...(contact.group ? [`CATEGORIES:${contact.group}`] : []), 'END:VCARD' ].join('\n'); updatedVcards.push(newVcard); @@ -440,14 +444,13 @@ export default function CarnetPage() { ...(contact.organization ? [`ORG:${contact.organization}`] : []), ...(contact.address ? [`ADR:${contact.address}`] : []), ...(contact.notes ? [`NOTE:${contact.notes}`] : []), - ...(contact.group ? [`CATEGORIES:${contact.group}`] : []), 'END:VCARD' ].join('\n'); updatedVcards.push(newVcard); } // Join all vCards back together with proper spacing - const vcfContent = updatedVcards.join('\n\n'); + vcfContent = updatedVcards.join('\n\n'); // Save the updated VCF file const saveResponse = await fetch('/api/nextcloud/files', {