diff --git a/app/carnet/page.tsx b/app/carnet/page.tsx index a20a97dc..c7a72b7b 100644 --- a/app/carnet/page.tsx +++ b/app/carnet/page.tsx @@ -73,33 +73,15 @@ export default function CarnetPage() { useEffect(() => { const fetchNextcloudFolders = async () => { - // Check cache first - if (foldersCache.current) { - const cacheAge = Date.now() - foldersCache.current.timestamp; - if (cacheAge < 5 * 60 * 1000) { // 5 minutes cache - setNextcloudFolders(foldersCache.current.folders); - return; - } - } - try { - const response = await fetch('/api/nextcloud/status'); + const response = await fetch('/api/nextcloud/files?folder=Contacts'); if (!response.ok) { - throw new Error('Failed to fetch Nextcloud folders'); + throw new Error('Failed to fetch folders'); } const data = await response.json(); - const folders = data.folders || []; - - // Update cache - foldersCache.current = { - folders, - timestamp: Date.now() - }; - - setNextcloudFolders(folders); - } catch (err) { - console.error('Error fetching Nextcloud folders:', err); - setNextcloudFolders([]); + setNextcloudFolders(data); + } catch (error) { + console.error('Error fetching folders:', error); } }; @@ -561,6 +543,45 @@ export default function CarnetPage() { return formatVCard(vcard); }; + const handleCreateGroup = async (groupName: string) => { + if (!session?.user?.id) return; + + try { + setIsLoading(true); + + // Create a new empty VCF file for the group + const basePath = `/files/cube-${session.user.id}/Private/Contacts`; + const vcfFile = `${groupName}.vcf`; + const path = `${basePath}/${vcfFile}`; + + // Create an empty VCF file + const saveResponse = await fetch('/api/nextcloud/files', { + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + id: path, + title: vcfFile, + content: '', // Empty VCF file + folder: 'Contacts', + mime: 'text/vcard' + }), + }); + + if (!saveResponse.ok) { + throw new Error('Failed to create group'); + } + + // Refresh the folders list to show the new group + await fetchNextcloudFolders(); + } catch (error) { + console.error('Error creating group:', error); + } finally { + setIsLoading(false); + } + }; + if (isLoading) { return (