diff --git a/app/carnet/page.tsx b/app/carnet/page.tsx index dc89c751..1255b0c8 100644 --- a/app/carnet/page.tsx +++ b/app/carnet/page.tsx @@ -394,30 +394,56 @@ export default function CarnetPage() { const { content } = await response.json(); - // Split the content into individual vCards and clean up empty lines + // Split the content into individual vCards const vcards = content.split('BEGIN:VCARD') .filter(section => section.trim()) - .map(section => section.trim()); + .map(section => 'BEGIN:VCARD' + section.trim()); // Update or add the contact let updatedVcards: string[] = []; let contactUpdated = false; - for (const section of vcards) { - const vcard = parseVCard('BEGIN:VCARD' + section); - if (vcard.uid?.[0]?.value === contact.id) { + for (const vcard of vcards) { + const parsed = parseVCard(vcard); + if (parsed.uid?.[0]?.value === contact.id) { // Replace the existing contact - updatedVcards.push(generateVCardContent(contact)); + const newVcard = [ + 'BEGIN:VCARD', + 'VERSION:3.0', + `UID:${contact.id}`, + `FN:${contact.fullName || ''}`, + ...(contact.email ? [`EMAIL;TYPE=INTERNET:${contact.email}`] : []), + ...(contact.phone ? [`TEL;TYPE=CELL:${contact.phone}`] : []), + ...(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); contactUpdated = true; } else { // Keep the existing contact - updatedVcards.push('BEGIN:VCARD' + section); + updatedVcards.push(vcard); } } // If contact wasn't found, add it as new if (!contactUpdated) { - updatedVcards.push(generateVCardContent(contact)); + const newVcard = [ + 'BEGIN:VCARD', + 'VERSION:3.0', + `UID:${contact.id}`, + `FN:${contact.fullName || ''}`, + ...(contact.email ? [`EMAIL;TYPE=INTERNET:${contact.email}`] : []), + ...(contact.phone ? [`TEL;TYPE=CELL:${contact.phone}`] : []), + ...(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