carnet panel contact
This commit is contained in:
parent
be0853b867
commit
c15c615428
@ -381,29 +381,34 @@ export default function CarnetPage() {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
|
||||
// Determine the correct VCF file path
|
||||
// Always use Allemanique.vcf for new contacts
|
||||
const basePath = `/files/cube-${session.user.id}/Private/Contacts`;
|
||||
const vcfFile = contact.group ? `${contact.group}.vcf` : 'contacts.vcf';
|
||||
const vcfFile = 'Allemanique.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');
|
||||
let vcfContent = '';
|
||||
let existingContacts: string[] = [];
|
||||
|
||||
try {
|
||||
// Try to get existing contacts from the VCF file
|
||||
const response = await fetch(`/api/nextcloud/files/content?path=${encodeURIComponent(path)}`);
|
||||
if (response.ok) {
|
||||
const { content } = await response.json();
|
||||
// Split the content into individual vCards
|
||||
existingContacts = content.split('BEGIN:VCARD')
|
||||
.filter(section => section.trim())
|
||||
.map(section => 'BEGIN:VCARD' + section.trim());
|
||||
}
|
||||
} catch (error) {
|
||||
// If the file doesn't exist, we'll create it with just the new contact
|
||||
console.log('No existing VCF file found, will create a new one');
|
||||
}
|
||||
|
||||
const { content } = await response.json();
|
||||
|
||||
// Split the content into individual vCards
|
||||
const vcards = content.split('BEGIN:VCARD')
|
||||
.filter(section => section.trim())
|
||||
.map(section => 'BEGIN:VCARD' + section.trim());
|
||||
|
||||
// Update or add the contact
|
||||
let updatedVcards: string[] = [];
|
||||
let contactUpdated = false;
|
||||
|
||||
for (const vcard of vcards) {
|
||||
for (const vcard of existingContacts) {
|
||||
const parsed = parseVCard(vcard);
|
||||
if (parsed.uid?.[0]?.value === contact.id) {
|
||||
// Replace the existing contact
|
||||
@ -417,7 +422,6 @@ export default function CarnetPage() {
|
||||
...(contact.organization ? [`ORG:${contact.organization}`] : []),
|
||||
...(contact.address ? [`ADR:${contact.address}`] : []),
|
||||
...(contact.notes ? [`NOTE:${contact.notes}`] : []),
|
||||
...(contact.group ? [`CATEGORIES:${contact.group}`] : []),
|
||||
'END:VCARD'
|
||||
].join('\n');
|
||||
updatedVcards.push(newVcard);
|
||||
@ -440,14 +444,13 @@ export default function CarnetPage() {
|
||||
...(contact.organization ? [`ORG:${contact.organization}`] : []),
|
||||
...(contact.address ? [`ADR:${contact.address}`] : []),
|
||||
...(contact.notes ? [`NOTE:${contact.notes}`] : []),
|
||||
...(contact.group ? [`CATEGORIES:${contact.group}`] : []),
|
||||
'END:VCARD'
|
||||
].join('\n');
|
||||
updatedVcards.push(newVcard);
|
||||
}
|
||||
|
||||
// Join all vCards back together with proper spacing
|
||||
const vcfContent = updatedVcards.join('\n\n');
|
||||
vcfContent = updatedVcards.join('\n\n');
|
||||
|
||||
// Save the updated VCF file
|
||||
const saveResponse = await fetch('/api/nextcloud/files', {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user