diff --git a/app/carnet/page.tsx b/app/carnet/page.tsx index e306b5c1..7ea7de07 100644 --- a/app/carnet/page.tsx +++ b/app/carnet/page.tsx @@ -200,7 +200,7 @@ export default function CarnetPage() { } }; - const fetchContacts = async (folder: string) => { + const fetchContacts = async (folder: string): Promise => { try { setIsLoadingContacts(true); @@ -210,10 +210,10 @@ export default function CarnetPage() { if (response.ok) { const { content } = await response.json(); const contacts = parseVCardContent(content); - setContacts(contacts.map(contact => ({ + return contacts.map(contact => ({ ...contact, group: folder.replace('.vcf', '') - }))); + })); } } else { // If not a VCF file, list all VCF files in the folder @@ -244,12 +244,13 @@ export default function CarnetPage() { ); // Flatten the array of contact arrays - setContacts(parsedContacts.flat().filter(Boolean)); + return parsedContacts.flat().filter(Boolean); } } + return []; } catch (error) { console.error('Error fetching contacts:', error); - setContacts([]); + return []; } finally { setIsLoadingContacts(false); } @@ -394,8 +395,10 @@ export default function CarnetPage() { const { content } = await response.json(); - // Split the content into individual vCards - const vcards = content.split('BEGIN:VCARD').filter(section => section.trim()); + // Split the content into individual vCards and clean up empty lines + const vcards = content.split('BEGIN:VCARD') + .filter(section => section.trim()) + .map(section => section.trim()); // Update or add the contact let updatedVcards: string[] = []; @@ -418,8 +421,8 @@ export default function CarnetPage() { updatedVcards.push(generateVCardContent(contact)); } - // Join all vCards back together - const vcfContent = updatedVcards.join('\n'); + // Join all vCards back together with proper spacing + const vcfContent = updatedVcards.join('\n\n'); // Save the updated VCF file const saveResponse = await fetch('/api/nextcloud/files', { @@ -440,8 +443,14 @@ export default function CarnetPage() { throw new Error('Failed to save contact'); } - // Refresh the contacts list - await fetchContacts(selectedFolder); + // Refresh only the contacts list without reloading the page + const updatedContacts = await fetchContacts(selectedFolder); + setContacts(updatedContacts); + + // Update the selected contact if it was edited + if (contactUpdated) { + setSelectedContact(contact); + } } catch (error) { console.error('Error saving contact:', error); } finally {