From b33ee113274a24a7ed25bdb1ecfec1e08d887b4d Mon Sep 17 00:00:00 2001 From: alma Date: Sat, 24 May 2025 19:59:08 +0200 Subject: [PATCH] W n8n attention vm --- components/missions/file-upload.tsx | 27 +++--- components/missions/missions-admin-panel.tsx | 91 +++++++------------- 2 files changed, 45 insertions(+), 73 deletions(-) diff --git a/components/missions/file-upload.tsx b/components/missions/file-upload.tsx index 20be863e..4312a209 100644 --- a/components/missions/file-upload.tsx +++ b/components/missions/file-upload.tsx @@ -126,16 +126,15 @@ export function FileUpload({ const selectedFile = e.target.files[0]; if (validateFile(selectedFile)) { setFile(selectedFile); - // If this is a new mission, call onFileSelect instead of waiting for upload - if (isNewMission && onFileSelect) { - onFileSelect(selectedFile); - } + // Immediately upload the file + handleUpload(selectedFile); } } }; - const handleUpload = async () => { - if (!file) { + const handleUpload = async (uploadFile?: File) => { + const fileToUpload = uploadFile || file; + if (!fileToUpload) { console.error('Upload failed: No file selected'); toast({ title: 'Upload failed', @@ -158,7 +157,7 @@ export function FileUpload({ // For new missions, just notify through the callback and don't try to upload if (isNewMission) { if (onFileSelect) { - onFileSelect(file); + onFileSelect(fileToUpload); } toast({ title: 'File selected', @@ -180,9 +179,9 @@ export function FileUpload({ } console.log('Starting upload process...', { - fileName: file.name, - fileSize: file.size, - fileType: file.type, + fileName: fileToUpload.name, + fileSize: fileToUpload.size, + fileType: fileToUpload.type, missionId, userId: session.user.id, uploadType: type @@ -194,7 +193,7 @@ export function FileUpload({ try { // Create form data const formData = new FormData(); - formData.append('file', file); + formData.append('file', fileToUpload); formData.append('missionId', missionId); formData.append('type', type); @@ -240,7 +239,7 @@ export function FileUpload({ toast({ title: 'File uploaded successfully', - description: type === 'logo' ? 'Logo has been updated' : `${file.name} has been added to attachments`, + description: type === 'logo' ? 'Logo has been updated' : `${fileToUpload.name} has been added to attachments`, variant: 'default', }); }, 1000); @@ -354,7 +353,7 @@ export function FileUpload({ variant="default" size="sm" className="bg-blue-600 hover:bg-blue-700 text-white" - onClick={handleUpload} + onClick={() => handleUpload()} > Upload @@ -377,4 +376,4 @@ export function FileUpload({ )} ); -} \ No newline at end of file +} \ No newline at end of file diff --git a/components/missions/missions-admin-panel.tsx b/components/missions/missions-admin-panel.tsx index 56a55ece..6ca1d8da 100644 --- a/components/missions/missions-admin-panel.tsx +++ b/components/missions/missions-admin-panel.tsx @@ -88,6 +88,20 @@ export function MissionsAdminPanel() { // Check if mission is valid (has all required guardiens) const isMissionValid = gardienDuTemps !== null && gardienDeLaParole !== null && gardienDeLaMemoire !== null; + // Add a draft mission ID for uploads + const [draftMissionId, setDraftMissionId] = useState(""); + const [uploadedLogoPath, setUploadedLogoPath] = useState(null); + const [uploadedAttachmentPaths, setUploadedAttachmentPaths] = useState([]); + + // Generate a draft mission ID on mount + useEffect(() => { + if (!draftMissionId) { + const userId = typeof window !== 'undefined' && window.localStorage.getItem('userId'); + const id = `draft-${userId || 'nouser'}-${Date.now()}`; + setDraftMissionId(id); + } + }, [draftMissionId]); + // Fetch users and groups on component mount useEffect(() => { const fetchData = async () => { @@ -384,16 +398,11 @@ export function MissionsAdminPanel() { setIsSubmitting(true); try { - // Format the data before sending const formattedData = { - name: missionData.name, + ...missionData, + logo: uploadedLogoPath || null, + attachments: uploadedAttachmentPaths.map(path => ({ filePath: path })), oddScope: Array.isArray(missionData.oddScope) ? missionData.oddScope : [missionData.oddScope], - niveau: missionData.niveau, - intention: missionData.intention, - missionType: missionData.missionType, - donneurDOrdre: missionData.donneurDOrdre, - projection: missionData.projection, - participation: missionData.participation, services: selectedServices, profils: selectedProfils, guardians: { @@ -402,10 +411,6 @@ export function MissionsAdminPanel() { 'gardien-memoire': gardienDeLaMemoire }, volunteers: volontaires, - // Only include logo if it exists and has a path - logo: missionData.logo || null, - // Don't send attachments directly - they should be handled separately - attachments: undefined }; console.log('Submitting mission data:', JSON.stringify(formattedData, null, 2)); @@ -426,40 +431,6 @@ export function MissionsAdminPanel() { throw new Error(data.error || data.details || 'Failed to create mission'); } - // Handle file uploads after mission creation if needed - if (data?.mission?.id && (selectedLogoFile || selectedAttachments.length > 0)) { - // Upload logo - if (selectedLogoFile) { - const result = await import('@/lib/s3').then(mod => mod.uploadMissionFile({ - missionId: data.mission.id, - file: selectedLogoFile, - type: 'logo', - })); - if (!result.success) { - toast({ - title: "Erreur", - description: result.error || "Erreur lors de l'upload du logo", - variant: "destructive", - }); - } - } - // Upload attachments - for (const file of selectedAttachments) { - const result = await import('@/lib/s3').then(mod => mod.uploadMissionFile({ - missionId: data.mission.id, - file, - type: 'attachment', - })); - if (!result.success) { - toast({ - title: "Erreur", - description: result.error || `Erreur lors de l'upload du fichier ${file.name}`, - variant: "destructive", - }); - } - } - } - toast({ title: "Mission créée avec succès", description: "La mission a été créée et les intégrations sont en cours.", @@ -515,22 +486,15 @@ export function MissionsAdminPanel() { { - // Store the selected file in state for later upload - setSelectedLogoFile(file); - console.log('Logo file selected for later upload:', file.name); - }} + missionId={draftMissionId} + isNewMission={false} onUploadComplete={(data) => { - // Handle logo upload complete if (data?.filePath) { - setMissionData(prev => ({ - ...prev, - logo: data.filePath - })); + setUploadedLogoPath(data.filePath); + setMissionData(prev => ({ ...prev, logo: data.filePath })); } }} + onFileSelect={undefined} /> @@ -906,7 +870,16 @@ export function MissionsAdminPanel() { onChange={(e) => { if (e.target.files && e.target.files.length > 0) { const file = e.target.files[0]; - setSelectedAttachments(prev => [...prev, file]); + // Immediately upload the file + import('@/lib/s3').then(mod => mod.uploadMissionFile({ + missionId: draftMissionId, + file, + type: 'attachment', + })).then(result => { + if (result.success && result.data?.filePath) { + setUploadedAttachmentPaths(prev => [...prev, result.data.filePath]); + } + }); } }} />