carnet panel contact
This commit is contained in:
parent
dca1d5f204
commit
0291ec7465
@ -488,61 +488,36 @@ export default function CarnetPage() {
|
||||
};
|
||||
|
||||
const handleContactSave = async (contact: Contact) => {
|
||||
if (!session?.user?.id) return;
|
||||
|
||||
try {
|
||||
setIsLoading(true);
|
||||
if (!selectedFolder) return;
|
||||
const vcardContent = generateVCard(contact);
|
||||
const filename = `${contact.id}.vcf`;
|
||||
const path = `/files/cube-${session.user.id}/Private/Contacts/${selectedFolder}/${filename}`;
|
||||
|
||||
// Determine the correct path based on whether we're in the Contacts folder or a specific VCF file
|
||||
const basePath = `/files/cube-${session?.user?.id}/Private/Contacts`;
|
||||
const vcfFile = contact.group ? `${contact.group}.vcf` : selectedFolder;
|
||||
const path = `${basePath}/${vcfFile}`;
|
||||
|
||||
// Get existing contacts from the VCF file
|
||||
const response = await fetch(`/api/nextcloud/files/content?path=${encodeURIComponent(path)}`);
|
||||
let contacts = [];
|
||||
|
||||
if (response.ok) {
|
||||
const { content } = await response.json();
|
||||
contacts = parseVCard(content);
|
||||
|
||||
// Update or add the contact
|
||||
const existingIndex = contacts.findIndex(c => c.id === contact.id);
|
||||
if (existingIndex !== -1) {
|
||||
contacts[existingIndex] = contact;
|
||||
} else {
|
||||
contacts.push(contact);
|
||||
}
|
||||
} else {
|
||||
// If file doesn't exist yet, create new contacts array
|
||||
contacts = [contact];
|
||||
}
|
||||
|
||||
// Generate VCard content
|
||||
const vcfContent = contacts.map(c => generateVCard(c)).join('\n');
|
||||
|
||||
// Save the VCard file
|
||||
const saveResponse = await fetch('/api/nextcloud/files', {
|
||||
const response = await fetch('/api/nextcloud/files', {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
id: path,
|
||||
title: vcfFile,
|
||||
content: vcfContent,
|
||||
folder: 'Contacts'
|
||||
})
|
||||
title: filename,
|
||||
content: vcardContent,
|
||||
folder: `Contacts/${selectedFolder}`,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!saveResponse.ok) {
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to save contact');
|
||||
}
|
||||
|
||||
// Refresh contacts list
|
||||
// Refresh the contacts list
|
||||
await fetchContacts(selectedFolder);
|
||||
setIsLoading(false);
|
||||
} catch (error) {
|
||||
console.error('Error saving contact:', error);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user