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 contacts = parseVCardContent(content);
// Split the content into individual vCards
const vcards = content.split('BEGIN:VCARD').filter(section => section.trim());
// Update or add the contact
const existingIndex = contacts.findIndex(c => c.id === contact.id);
if (existingIndex >= 0) {
contacts[existingIndex] = contact;
} else {
contacts.push(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) {
// Replace the existing contact
updatedVcards.push(generateVCardContent(contact));
contactUpdated = true;
} else {
// Keep the existing contact
updatedVcards.push('BEGIN:VCARD' + section);
}
}
// Generate new VCF content
const vcfContent = contacts.map(c => generateVCardContent(c)).join('\n');
// If contact wasn't found, add it as new
if (!contactUpdated) {
updatedVcards.push(generateVCardContent(contact));
}
// Join all vCards back together
const vcfContent = updatedVcards.join('\n');
// Save the updated VCF file
const saveResponse = await fetch('/api/nextcloud/files', {