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) => {
|
const handleContactSave = async (contact: Contact) => {
|
||||||
|
if (!session?.user?.id) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
setIsLoading(true);
|
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 response = await fetch('/api/nextcloud/files', {
|
||||||
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', {
|
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
id: path,
|
id: path,
|
||||||
title: vcfFile,
|
title: filename,
|
||||||
content: vcfContent,
|
content: vcardContent,
|
||||||
folder: 'Contacts'
|
folder: `Contacts/${selectedFolder}`,
|
||||||
})
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!saveResponse.ok) {
|
if (!response.ok) {
|
||||||
throw new Error('Failed to save contact');
|
throw new Error('Failed to save contact');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Refresh contacts list
|
// Refresh the contacts list
|
||||||
await fetchContacts(selectedFolder);
|
await fetchContacts(selectedFolder);
|
||||||
setIsLoading(false);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error saving contact:', error);
|
console.error('Error saving contact:', error);
|
||||||
|
} finally {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user