carnet panel contact

This commit is contained in:
alma 2025-04-20 22:21:06 +02:00
parent a10660a27f
commit 64b5fcd4e8

View File

@ -393,18 +393,33 @@ export default function CarnetPage() {
} }
const { content } = await response.json(); const { content } = await response.json();
const contacts = parseVCardContent(content);
// Split the content into individual vCards
const vcards = content.split('BEGIN:VCARD').filter(section => section.trim());
// Update or add the contact // Update or add the contact
const existingIndex = contacts.findIndex(c => c.id === contact.id); let updatedVcards: string[] = [];
if (existingIndex >= 0) { let contactUpdated = false;
contacts[existingIndex] = contact;
for (const section of vcards) {
const vcard = parseVCard('BEGIN:VCARD' + section);
if (vcard.uid?.[0]?.value === contact.id) {
// Replace the existing contact
updatedVcards.push(generateVCardContent(contact));
contactUpdated = true;
} else { } else {
contacts.push(contact); // Keep the existing contact
updatedVcards.push('BEGIN:VCARD' + section);
}
} }
// Generate new VCF content // If contact wasn't found, add it as new
const vcfContent = contacts.map(c => generateVCardContent(c)).join('\n'); if (!contactUpdated) {
updatedVcards.push(generateVCardContent(contact));
}
// Join all vCards back together
const vcfContent = updatedVcards.join('\n');
// Save the updated VCF file // Save the updated VCF file
const saveResponse = await fetch('/api/nextcloud/files', { const saveResponse = await fetch('/api/nextcloud/files', {