carnet panel contact
This commit is contained in:
parent
cb44599a4f
commit
543c78f7d5
@ -431,76 +431,129 @@ export default function CarnetPage() {
|
||||
}
|
||||
};
|
||||
|
||||
const handleContactSave = async (contact: Contact) => {
|
||||
try {
|
||||
const vcfContent = generateVCard(contact);
|
||||
const response = await fetch('/api/nextcloud/files', {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
path: `/files/cube-${session?.user?.id}/Private/Contacts/${selectedFolder}`,
|
||||
content: vcfContent
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to save contact');
|
||||
}
|
||||
|
||||
// Refresh contacts list
|
||||
fetchContacts(selectedFolder);
|
||||
} catch (error) {
|
||||
console.error('Error saving contact:', error);
|
||||
}
|
||||
};
|
||||
|
||||
const handleContactDelete = async (contact: Contact) => {
|
||||
if (!confirm('Êtes-vous sûr de vouloir supprimer ce contact ?')) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/nextcloud/files', {
|
||||
method: 'DELETE',
|
||||
setIsLoading(true);
|
||||
const path = `/files/cube-${session?.user?.id}/Private/Contacts/${selectedFolder}`;
|
||||
|
||||
// Get all contacts from the current 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);
|
||||
|
||||
// Remove the contact
|
||||
const updatedContacts = contacts.filter(c => c.id !== contact.id);
|
||||
|
||||
// Generate new VCF content
|
||||
const vcfContent = updatedContacts.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({
|
||||
path: `/files/cube-${session?.user?.id}/Private/Contacts/${selectedFolder}`
|
||||
path,
|
||||
content: vcfContent,
|
||||
mime: 'text/vcard'
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
if (!saveResponse.ok) {
|
||||
throw new Error('Failed to delete contact');
|
||||
}
|
||||
|
||||
// Clear selected contact and refresh list
|
||||
setSelectedContact(null);
|
||||
fetchContacts('Contacts');
|
||||
await fetchContacts(selectedFolder);
|
||||
setIsLoading(false);
|
||||
} catch (error) {
|
||||
console.error('Error deleting contact:', error);
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleContactSave = async (contact: Contact) => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
if (!selectedFolder) return;
|
||||
|
||||
const path = `/files/cube-${session?.user?.id}/Private/Contacts/${selectedFolder}`;
|
||||
|
||||
// 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',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
path,
|
||||
content: vcfContent,
|
||||
mime: 'text/vcard'
|
||||
})
|
||||
});
|
||||
|
||||
if (!saveResponse.ok) {
|
||||
throw new Error('Failed to save contact');
|
||||
}
|
||||
|
||||
// Refresh contacts list
|
||||
await fetchContacts(selectedFolder);
|
||||
setIsLoading(false);
|
||||
} catch (error) {
|
||||
console.error('Error saving contact:', error);
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const generateVCard = (contact: Contact): string => {
|
||||
const lines = [
|
||||
const vcard = [
|
||||
'BEGIN:VCARD',
|
||||
'VERSION:3.0',
|
||||
`UID:${contact.id}`,
|
||||
'PRODID:-//Infomaniak//Workspace//pim',
|
||||
contact.fullName ? `FN:${contact.fullName}` : '',
|
||||
contact.fullName ? `N:${contact.fullName.split(' ').reverse().join(';')};` : '',
|
||||
`FN:${contact.fullName}`,
|
||||
contact.email ? `EMAIL;TYPE=INTERNET:${contact.email}` : '',
|
||||
contact.phone ? `TEL;TYPE=CELL:${contact.phone}` : '',
|
||||
contact.organization ? `ORG:${contact.organization}` : '',
|
||||
contact.email ? `EMAIL;TYPE=WORK:${contact.email}` : '',
|
||||
contact.phone ? `TEL;TYPE=WORK:${contact.phone}` : '',
|
||||
contact.address ? `ADR;TYPE=WORK:;;${contact.address};;;;` : '',
|
||||
contact.address ? `ADR;TYPE=HOME:;;${contact.address}` : '',
|
||||
contact.notes ? `NOTE:${contact.notes}` : '',
|
||||
contact.group ? `CATEGORIES:${contact.group}` : '',
|
||||
'END:VCARD'
|
||||
].filter(Boolean);
|
||||
].filter(line => line !== '').join('\r\n');
|
||||
|
||||
return lines.join('\n');
|
||||
return vcard;
|
||||
};
|
||||
|
||||
if (isLoading) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user