diff --git a/app/carnet/page.tsx b/app/carnet/page.tsx index 0c38dda5..54c7997d 100644 --- a/app/carnet/page.tsx +++ b/app/carnet/page.tsx @@ -31,8 +31,8 @@ interface Note { interface Contact { id: string; - fullName: string; - email: string; + fullName?: string; + email?: string; phone?: string; organization?: string; address?: string; @@ -132,51 +132,33 @@ export default function CarnetPage() { }, [isSmallScreen, isMediumScreen]); useEffect(() => { - const fetchNotes = async () => { - try { - setIsLoadingNotes(true); - const response = await fetch(`/api/nextcloud/files?folder=${selectedFolder}`); - if (!response.ok) { - throw new Error('Failed to fetch notes'); - } - const data = await response.json(); - if (selectedFolder === 'Contacts') { - // For contacts, parse the VCF files - const parsedContacts = await Promise.all( - data.map(async (file: any) => { - try { - // Use the full filename for fetching content - const contentResponse = await fetch(`/api/nextcloud/files/content?path=${encodeURIComponent(file.filename)}`); - if (contentResponse.ok) { - const content = await contentResponse.text(); - return parseVCard(content); - } - console.error('Failed to fetch VCF content:', contentResponse.status); - return null; - } catch (error) { - console.error('Error fetching VCF content:', error); - return null; - } - }) - ); - setContacts(parsedContacts.filter(Boolean)); - } else { + if (selectedFolder === 'Contacts') { + // When "Contacts" is selected, show all contacts + fetchContacts('Contacts'); + } else if (selectedFolder.endsWith('.vcf')) { + // When a specific VCF file is selected, show its contacts + fetchContacts(selectedFolder); + } else { + // For other folders (Notes, etc.), fetch notes + const fetchNotes = async () => { + try { + setIsLoadingNotes(true); + const response = await fetch(`/api/nextcloud/files?folder=${selectedFolder}`); + if (!response.ok) { + throw new Error('Failed to fetch notes'); + } + const data = await response.json(); setNotes(data); - } - } catch (error) { - console.error('Error fetching data:', error); - if (selectedFolder === 'Contacts') { - setContacts([]); - } else { + } catch (error) { + console.error('Error fetching notes:', error); setNotes([]); + } finally { + setIsLoadingNotes(false); } - } finally { - setIsLoadingNotes(false); - } - }; - - fetchNotes(); - }, [selectedFolder]); + }; + fetchNotes(); + } + }, [selectedFolder, session?.user?.id]); const parseVCard = (content: string): Contact[] => { try { @@ -277,39 +259,52 @@ export default function CarnetPage() { const fetchContacts = async (folder: string) => { try { setIsLoadingContacts(true); - const response = await fetch(`/api/nextcloud/files?folder=${folder}`); - if (response.ok) { - const files = await response.json(); - const vcfFiles = files.filter((file: any) => file.basename.endsWith('.vcf')); - - // Parse VCF files and extract contact information - const parsedContacts = await Promise.all( - vcfFiles.map(async (file: any) => { - try { - // Construct the full WebDAV path - const webdavPath = `/files/${file.filename}`; - const contentResponse = await fetch(`/api/nextcloud/files/content?path=${encodeURIComponent(webdavPath)}`); - if (contentResponse.ok) { - const content = await contentResponse.text(); - const contacts = parseVCard(content); - return contacts.map(contact => ({ - ...contact, - group: folder - })); + // First, check if we're looking at a specific VCF file + if (folder.endsWith('.vcf')) { + const response = await fetch(`/api/nextcloud/files/content?path=${encodeURIComponent(`/files/cube-${session?.user?.id}/Private/Contacts/${folder}`)}`); + if (response.ok) { + const { content } = await response.json(); + const contacts = parseVCard(content); + setContacts(contacts.map(contact => ({ + ...contact, + group: folder.replace('.vcf', '') + }))); + } + } else { + // If not a VCF file, list all VCF files in the folder + const response = await fetch(`/api/nextcloud/files?folder=${folder}`); + if (response.ok) { + const files = await response.json(); + const vcfFiles = files.filter((file: any) => file.basename.endsWith('.vcf')); + + // Parse VCF files and extract contact information + const parsedContacts = await Promise.all( + vcfFiles.map(async (file: any) => { + try { + const contentResponse = await fetch(`/api/nextcloud/files/content?path=${encodeURIComponent(file.filename)}`); + if (contentResponse.ok) { + const { content } = await contentResponse.json(); + const contacts = parseVCard(content); + return contacts.map(contact => ({ + ...contact, + group: file.basename.replace('.vcf', '') + })); + } + return []; + } catch (error) { + console.error('Error fetching VCF content:', error); + return []; } - return []; - } catch (error) { - console.error('Error fetching VCF content:', error); - return []; - } - }) - ); - - // Flatten the array of contact arrays - setContacts(parsedContacts.flat().filter(Boolean)); + }) + ); + + // Flatten the array of contact arrays + setContacts(parsedContacts.flat().filter(Boolean)); + } } } catch (error) { console.error('Error fetching contacts:', error); + setContacts([]); } finally { setIsLoadingContacts(false); } diff --git a/components/carnet/contacts-view.tsx b/components/carnet/contacts-view.tsx index 073b60bd..9cff5c76 100644 --- a/components/carnet/contacts-view.tsx +++ b/components/carnet/contacts-view.tsx @@ -5,8 +5,8 @@ import { Search, User, Mail, Phone, Building, MapPin, ChevronRight } from 'lucid interface Contact { id: string; - fullName: string; - email: string; + fullName?: string; + email?: string; phone?: string; organization?: string; address?: string; @@ -76,8 +76,12 @@ export const ContactsView: React.FC = ({
-
{contact.fullName}
-
{contact.email}
+
+ {contact.fullName || contact.email || 'Sans nom'} +
+ {contact.email && ( +
{contact.email}
+ )}