carnet panel contact
This commit is contained in:
parent
64b5fcd4e8
commit
15ca9e1fd5
@ -200,7 +200,7 @@ export default function CarnetPage() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const fetchContacts = async (folder: string) => {
|
const fetchContacts = async (folder: string): Promise<Contact[]> => {
|
||||||
try {
|
try {
|
||||||
setIsLoadingContacts(true);
|
setIsLoadingContacts(true);
|
||||||
|
|
||||||
@ -210,10 +210,10 @@ export default function CarnetPage() {
|
|||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
const { content } = await response.json();
|
const { content } = await response.json();
|
||||||
const contacts = parseVCardContent(content);
|
const contacts = parseVCardContent(content);
|
||||||
setContacts(contacts.map(contact => ({
|
return contacts.map(contact => ({
|
||||||
...contact,
|
...contact,
|
||||||
group: folder.replace('.vcf', '')
|
group: folder.replace('.vcf', '')
|
||||||
})));
|
}));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// If not a VCF file, list all VCF files in the folder
|
// 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
|
// Flatten the array of contact arrays
|
||||||
setContacts(parsedContacts.flat().filter(Boolean));
|
return parsedContacts.flat().filter(Boolean);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return [];
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching contacts:', error);
|
console.error('Error fetching contacts:', error);
|
||||||
setContacts([]);
|
return [];
|
||||||
} finally {
|
} finally {
|
||||||
setIsLoadingContacts(false);
|
setIsLoadingContacts(false);
|
||||||
}
|
}
|
||||||
@ -394,8 +395,10 @@ export default function CarnetPage() {
|
|||||||
|
|
||||||
const { content } = await response.json();
|
const { content } = await response.json();
|
||||||
|
|
||||||
// Split the content into individual vCards
|
// Split the content into individual vCards and clean up empty lines
|
||||||
const vcards = content.split('BEGIN:VCARD').filter(section => section.trim());
|
const vcards = content.split('BEGIN:VCARD')
|
||||||
|
.filter(section => section.trim())
|
||||||
|
.map(section => section.trim());
|
||||||
|
|
||||||
// Update or add the contact
|
// Update or add the contact
|
||||||
let updatedVcards: string[] = [];
|
let updatedVcards: string[] = [];
|
||||||
@ -418,8 +421,8 @@ export default function CarnetPage() {
|
|||||||
updatedVcards.push(generateVCardContent(contact));
|
updatedVcards.push(generateVCardContent(contact));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Join all vCards back together
|
// Join all vCards back together with proper spacing
|
||||||
const vcfContent = updatedVcards.join('\n');
|
const vcfContent = updatedVcards.join('\n\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', {
|
||||||
@ -440,8 +443,14 @@ export default function CarnetPage() {
|
|||||||
throw new Error('Failed to save contact');
|
throw new Error('Failed to save contact');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Refresh the contacts list
|
// Refresh only the contacts list without reloading the page
|
||||||
await fetchContacts(selectedFolder);
|
const updatedContacts = await fetchContacts(selectedFolder);
|
||||||
|
setContacts(updatedContacts);
|
||||||
|
|
||||||
|
// Update the selected contact if it was edited
|
||||||
|
if (contactUpdated) {
|
||||||
|
setSelectedContact(contact);
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error saving contact:', error);
|
console.error('Error saving contact:', error);
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user