From b0cb20d0f4d36c8af3d1d9a2fd95a9c045ec4d39 Mon Sep 17 00:00:00 2001 From: chark1es Date: Tue, 11 Feb 2025 02:41:59 -0800 Subject: [PATCH] fix multiple files not showing --- .../dashboard/Officer_EventManagement.astro | 2674 +++++++++-------- src/components/pocketbase/FileManager.ts | 58 + 2 files changed, 1448 insertions(+), 1284 deletions(-) diff --git a/src/components/dashboard/Officer_EventManagement.astro b/src/components/dashboard/Officer_EventManagement.astro index 2c9401d..4ff7e9d 100644 --- a/src/components/dashboard/Officer_EventManagement.astro +++ b/src/components/dashboard/Officer_EventManagement.astro @@ -15,497 +15,6 @@ const sendLog = SendLog.getInstance(); // Interface for Event type interface Event { - id: string; - event_name: string; - event_description: string; - event_code: string; - location: string; - files: string[]; - points_to_reward: number; - start_date: string; - end_date: string; - published: boolean; - has_food: boolean; - attendees: AttendeeEntry[]; -} - -interface AttendeeEntry { - user_id: string; - time_checked_in: string; - food: string; -} - -interface ListResponse { - page: number; - perPage: number; - totalItems: number; - totalPages: number; - items: T[]; -} - -// Initialize variables -let eventResponse: ListResponse = { - page: 1, - perPage: 5, - totalItems: 0, - totalPages: 0, - items: [], -}; -let upcomingEvents: Event[] = []; - -// Fetch events -try { - if (auth.isAuthenticated()) { - eventResponse = await get.getList("events", 1, 5, "", "-start_date"); - upcomingEvents = eventResponse.items; - } -} catch (error) { - console.error("Failed to fetch events:", error); -} - -const totalEvents = eventResponse.totalItems; -const totalPages = eventResponse.totalPages; -const currentPage = eventResponse.page; - -// Add type declaration for window -declare global { - interface Window { - [key: string]: any; - openEditModal: (event?: any) => void; - deleteFile: (eventId: string, filename: string) => void; - previewFile: (url: string, filename: string) => void; - openDetailsModal: (event: Event) => void; - showFilePreview: (file: { - url: string; - type: string; - name: string; - }) => void; - backToFileList: () => void; - handlePreviewError: () => void; - showLoading: () => void; - hideLoading: () => void; - deleteEvent: (eventId: string, eventName: string) => Promise; - resetAndCloseModal: () => void; - } -} ---- - - - - - - - - - - - - - - - - diff --git a/src/components/pocketbase/FileManager.ts b/src/components/pocketbase/FileManager.ts index a73e55c..e3249be 100644 --- a/src/components/pocketbase/FileManager.ts +++ b/src/components/pocketbase/FileManager.ts @@ -89,6 +89,64 @@ export class FileManager { } } + /** + * Append multiple files to a record without overriding existing ones + * @param collectionName The name of the collection + * @param recordId The ID of the record to attach the files to + * @param field The field name for the files + * @param files Array of files to upload + * @returns The updated record + */ + public async appendFiles( + collectionName: string, + recordId: string, + field: string, + files: File[] + ): Promise { + if (!this.auth.isAuthenticated()) { + throw new Error("User must be authenticated to upload files"); + } + + try { + this.auth.setUpdating(true); + const pb = this.auth.getPocketBase(); + + // First, get the current record to check existing files + const record = await pb.collection(collectionName).getOne(recordId); + + // Create FormData with existing files + const formData = new FormData(); + + // Get existing files from the record + const existingFiles = (record as any)[field] || []; + + // For each existing file, we need to fetch it and add it to the FormData + for (const existingFile of existingFiles) { + try { + const response = await fetch(this.getFileUrl(collectionName, recordId, existingFile)); + const blob = await response.blob(); + const file = new File([blob], existingFile, { type: blob.type }); + formData.append(field, file); + } catch (error) { + console.warn(`Failed to fetch existing file ${existingFile}:`, error); + } + } + + // Append new files + files.forEach(file => { + formData.append(field, file); + }); + + const result = await pb.collection(collectionName).update(recordId, formData); + return result; + } catch (err) { + console.error(`Failed to append files to ${collectionName}:`, err); + throw err; + } finally { + this.auth.setUpdating(false); + } + } + /** * Get the URL for a file * @param collectionName The name of the collection