missions s3
This commit is contained in:
parent
dddfd5024b
commit
9f9280fbe8
@ -189,6 +189,7 @@ export function AttachmentsList({
|
|||||||
type="attachment"
|
type="attachment"
|
||||||
missionId={missionId}
|
missionId={missionId}
|
||||||
onUploadComplete={handleAttachmentUploaded}
|
onUploadComplete={handleAttachmentUploaded}
|
||||||
|
isNewMission={false}
|
||||||
/>
|
/>
|
||||||
<div className="mt-2 flex justify-end">
|
<div className="mt-2 flex justify-end">
|
||||||
<Button
|
<Button
|
||||||
|
|||||||
@ -8,18 +8,22 @@ import { toast } from '@/components/ui/use-toast';
|
|||||||
|
|
||||||
interface FileUploadProps {
|
interface FileUploadProps {
|
||||||
type: 'logo' | 'attachment';
|
type: 'logo' | 'attachment';
|
||||||
missionId: string;
|
missionId?: string; // Make missionId optional
|
||||||
onUploadComplete?: (data: any) => void;
|
onUploadComplete?: (data: any) => void;
|
||||||
|
onFileSelect?: (file: File) => void; // New callback for when file is selected but not uploaded yet
|
||||||
maxSize?: number; // in bytes, default 5MB
|
maxSize?: number; // in bytes, default 5MB
|
||||||
acceptedFileTypes?: string;
|
acceptedFileTypes?: string;
|
||||||
|
isNewMission?: boolean; // Flag to indicate if this is a new mission being created
|
||||||
}
|
}
|
||||||
|
|
||||||
export function FileUpload({
|
export function FileUpload({
|
||||||
type,
|
type,
|
||||||
missionId,
|
missionId,
|
||||||
onUploadComplete,
|
onUploadComplete,
|
||||||
|
onFileSelect,
|
||||||
maxSize = 5 * 1024 * 1024, // 5MB
|
maxSize = 5 * 1024 * 1024, // 5MB
|
||||||
acceptedFileTypes = type === 'logo' ? 'image/*' : '.pdf,.doc,.docx,.xls,.xlsx,.jpg,.jpeg,.png'
|
acceptedFileTypes = type === 'logo' ? 'image/*' : '.pdf,.doc,.docx,.xls,.xlsx,.jpg,.jpeg,.png',
|
||||||
|
isNewMission = false
|
||||||
}: FileUploadProps) {
|
}: FileUploadProps) {
|
||||||
// Log props on init
|
// Log props on init
|
||||||
console.log('FileUpload component initialized with props:', {
|
console.log('FileUpload component initialized with props:', {
|
||||||
@ -109,6 +113,10 @@ export function FileUpload({
|
|||||||
const droppedFile = e.dataTransfer.files[0];
|
const droppedFile = e.dataTransfer.files[0];
|
||||||
if (validateFile(droppedFile)) {
|
if (validateFile(droppedFile)) {
|
||||||
setFile(droppedFile);
|
setFile(droppedFile);
|
||||||
|
// If this is a new mission, call onFileSelect instead of waiting for upload
|
||||||
|
if (isNewMission && onFileSelect) {
|
||||||
|
onFileSelect(droppedFile);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -118,20 +126,15 @@ export function FileUpload({
|
|||||||
const selectedFile = e.target.files[0];
|
const selectedFile = e.target.files[0];
|
||||||
if (validateFile(selectedFile)) {
|
if (validateFile(selectedFile)) {
|
||||||
setFile(selectedFile);
|
setFile(selectedFile);
|
||||||
|
// If this is a new mission, call onFileSelect instead of waiting for upload
|
||||||
|
if (isNewMission && onFileSelect) {
|
||||||
|
onFileSelect(selectedFile);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleUpload = async () => {
|
const handleUpload = async () => {
|
||||||
// Log all values to see what's missing
|
|
||||||
console.log('Debug upload parameters:', {
|
|
||||||
file,
|
|
||||||
sessionUser: session?.user,
|
|
||||||
userId: session?.user?.id,
|
|
||||||
missionId
|
|
||||||
});
|
|
||||||
|
|
||||||
// Check for missing parameters and provide specific feedback
|
|
||||||
if (!file) {
|
if (!file) {
|
||||||
console.error('Upload failed: No file selected');
|
console.error('Upload failed: No file selected');
|
||||||
toast({
|
toast({
|
||||||
@ -152,6 +155,20 @@ export function FileUpload({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For new missions, just notify through the callback and don't try to upload
|
||||||
|
if (isNewMission) {
|
||||||
|
if (onFileSelect) {
|
||||||
|
onFileSelect(file);
|
||||||
|
}
|
||||||
|
toast({
|
||||||
|
title: 'File selected',
|
||||||
|
description: 'The file will be uploaded when you save the mission.',
|
||||||
|
variant: 'default',
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// For existing missions, we need a missionId
|
||||||
if (!missionId) {
|
if (!missionId) {
|
||||||
console.error('Upload failed: Missing mission ID');
|
console.error('Upload failed: Missing mission ID');
|
||||||
toast({
|
toast({
|
||||||
@ -331,15 +348,18 @@ export function FileUpload({
|
|||||||
>
|
>
|
||||||
<X className="h-4 w-4" />
|
<X className="h-4 w-4" />
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
|
||||||
variant="default"
|
{!isNewMission && (
|
||||||
size="sm"
|
<Button
|
||||||
className="bg-blue-600 hover:bg-blue-700 text-white"
|
variant="default"
|
||||||
onClick={handleUpload}
|
size="sm"
|
||||||
>
|
className="bg-blue-600 hover:bg-blue-700 text-white"
|
||||||
<Check className="h-4 w-4 mr-1" />
|
onClick={handleUpload}
|
||||||
Upload
|
>
|
||||||
</Button>
|
<Check className="h-4 w-4 mr-1" />
|
||||||
|
Upload
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -64,6 +64,7 @@ export function MissionsAdminPanel() {
|
|||||||
const [missionId, setMissionId] = useState<string>("");
|
const [missionId, setMissionId] = useState<string>("");
|
||||||
const [activeTab, setActiveTab] = useState<string>("general");
|
const [activeTab, setActiveTab] = useState<string>("general");
|
||||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||||
|
const [selectedLogoFile, setSelectedLogoFile] = useState<File | null>(null);
|
||||||
const [missionData, setMissionData] = useState<{
|
const [missionData, setMissionData] = useState<{
|
||||||
name?: string;
|
name?: string;
|
||||||
logo?: string;
|
logo?: string;
|
||||||
@ -415,6 +416,28 @@ export function MissionsAdminPanel() {
|
|||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
||||||
|
// If we have a selected logo file and this is a new mission, upload it now
|
||||||
|
if (selectedLogoFile && data.mission && data.mission.id) {
|
||||||
|
console.log('Uploading logo for new mission:', data.mission.id);
|
||||||
|
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append('file', selectedLogoFile);
|
||||||
|
formData.append('missionId', data.mission.id);
|
||||||
|
formData.append('type', 'logo');
|
||||||
|
|
||||||
|
const logoResponse = await fetch('/api/missions/upload', {
|
||||||
|
method: 'POST',
|
||||||
|
body: formData
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!logoResponse.ok) {
|
||||||
|
console.error('Failed to upload logo:', await logoResponse.json());
|
||||||
|
// Continue with mission creation even if logo upload fails
|
||||||
|
} else {
|
||||||
|
console.log('Logo uploaded successfully');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
toast({
|
toast({
|
||||||
title: "Mission créée avec succès",
|
title: "Mission créée avec succès",
|
||||||
description: "Tous les gardiens ont été assignés et la mission a été enregistrée.",
|
description: "Tous les gardiens ont été assignés et la mission a été enregistrée.",
|
||||||
@ -473,6 +496,12 @@ export function MissionsAdminPanel() {
|
|||||||
<FileUpload
|
<FileUpload
|
||||||
type="logo"
|
type="logo"
|
||||||
missionId={missionId || ""}
|
missionId={missionId || ""}
|
||||||
|
isNewMission={!missionId}
|
||||||
|
onFileSelect={(file) => {
|
||||||
|
// Store the selected file in state for later upload
|
||||||
|
setSelectedLogoFile(file);
|
||||||
|
console.log('Logo file selected for later upload:', file.name);
|
||||||
|
}}
|
||||||
onUploadComplete={(data) => {
|
onUploadComplete={(data) => {
|
||||||
// Handle logo upload complete
|
// Handle logo upload complete
|
||||||
if (data?.filePath) {
|
if (data?.filePath) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user