carnet panel contact
This commit is contained in:
parent
7ab64dce3c
commit
3a35d473bb
@ -431,6 +431,64 @@ export default function CarnetPage() {
|
||||
}
|
||||
};
|
||||
|
||||
const handleContactSave = async (contact: Contact) => {
|
||||
if (!session?.user?.id) return;
|
||||
|
||||
try {
|
||||
setIsLoading(true);
|
||||
|
||||
// Determine the correct VCF file path
|
||||
const basePath = `/files/cube-${session.user.id}/Private/Contacts`;
|
||||
const vcfFile = contact.group ? `${contact.group}.vcf` : 'contacts.vcf';
|
||||
const path = `${basePath}/${vcfFile}`;
|
||||
|
||||
// Get existing contacts from the VCF file
|
||||
const response = await fetch(`/api/nextcloud/files/content?path=${encodeURIComponent(path)}`);
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch contacts');
|
||||
}
|
||||
|
||||
const { content } = await response.json();
|
||||
const contacts = parseVCard(content);
|
||||
|
||||
// Update or add the contact
|
||||
const existingIndex = contacts.findIndex(c => c.id === contact.id);
|
||||
if (existingIndex >= 0) {
|
||||
contacts[existingIndex] = contact;
|
||||
} else {
|
||||
contacts.push(contact);
|
||||
}
|
||||
|
||||
// Generate new VCF content
|
||||
const vcfContent = contacts.map(c => generateVCard(c)).join('\n');
|
||||
|
||||
// Save the updated VCF file
|
||||
const saveResponse = await fetch('/api/nextcloud/files', {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
id: path,
|
||||
title: vcfFile,
|
||||
content: vcfContent,
|
||||
folder: 'Contacts'
|
||||
}),
|
||||
});
|
||||
|
||||
if (!saveResponse.ok) {
|
||||
throw new Error('Failed to save contact');
|
||||
}
|
||||
|
||||
// Refresh the contacts list
|
||||
await fetchContacts(selectedFolder);
|
||||
} catch (error) {
|
||||
console.error('Error saving contact:', error);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleContactDelete = async (contact: Contact) => {
|
||||
if (!confirm('Êtes-vous sûr de vouloir supprimer ce contact ?')) {
|
||||
return;
|
||||
@ -439,12 +497,12 @@ export default function CarnetPage() {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
|
||||
// Determine the correct path based on whether we're in the Contacts folder or a specific VCF file
|
||||
// Determine the correct VCF file path
|
||||
const basePath = `/files/cube-${session?.user?.id}/Private/Contacts`;
|
||||
const vcfFile = contact.group ? `${contact.group}.vcf` : selectedFolder;
|
||||
const vcfFile = contact.group ? `${contact.group}.vcf` : 'contacts.vcf';
|
||||
const path = `${basePath}/${vcfFile}`;
|
||||
|
||||
// Get all contacts from the VCF file
|
||||
// Get existing contacts from the VCF file
|
||||
const response = await fetch(`/api/nextcloud/files/content?path=${encodeURIComponent(path)}`);
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch contacts');
|
||||
@ -480,46 +538,8 @@ export default function CarnetPage() {
|
||||
// Clear selected contact and refresh list
|
||||
setSelectedContact(null);
|
||||
await fetchContacts(selectedFolder);
|
||||
setIsLoading(false);
|
||||
} catch (error) {
|
||||
console.error('Error deleting contact:', error);
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleContactSave = async (contact: Contact) => {
|
||||
if (!session?.user?.id) return;
|
||||
|
||||
try {
|
||||
setIsLoading(true);
|
||||
const vcardContent = generateVCard(contact);
|
||||
const filename = `${contact.id}.vcf`;
|
||||
|
||||
// If we're in a specific group folder, use that as the path
|
||||
const folderPath = selectedFolder === 'Contacts' ? 'Contacts' : `Contacts/${selectedFolder}`;
|
||||
const path = `/files/cube-${session.user.id}/Private/${folderPath}/${filename}`;
|
||||
|
||||
const response = await fetch('/api/nextcloud/files', {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
id: path,
|
||||
title: filename,
|
||||
content: vcardContent,
|
||||
folder: folderPath
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to save contact');
|
||||
}
|
||||
|
||||
// Refresh the contacts list
|
||||
await fetchContacts(selectedFolder);
|
||||
} catch (error) {
|
||||
console.error('Error saving contact:', error);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user