diff --git a/src/app/api/generalApi.ts b/src/app/api/generalApi.ts new file mode 100644 index 0000000..70f5b6a --- /dev/null +++ b/src/app/api/generalApi.ts @@ -0,0 +1,54 @@ +import { createSupabaseClient } from "@/lib/supabase/clientComponentClient"; +import Swal from "sweetalert2"; + +export async function uploadFile(file: File, userID: string, bucketName: string, filePath: string) { + const supabase = createSupabaseClient(); + let errorMessages: string[] = []; + + // check if the folder exists + const { data: folderData, error: folderError } = await supabase.storage + .from(bucketName) + .list(`${userID}/`); + + if (folderError) { + errorMessages.push(`Error checking for folder: ${folderError.message}`); + } + + // if the folder exists, clear the folder + if (folderData && folderData.length > 0) { + // console.log("Folder exists. Clearing contents..."); + + for (const fileItem of folderData) { + const { error: removeError } = await supabase.storage + .from(bucketName) + .remove([`${userID}/${fileItem.name}`]); + + if (removeError) { + errorMessages.push( + `Error removing file (${fileItem.name}): ${removeError.message}` + ); + } + } + } + + // upload the new file to the folder (if no folderError) + if (errorMessages.length === 0) { + const { error: uploadError } = await supabase.storage + .from(bucketName) + .upload(filePath, file); + + if (uploadError) { + errorMessages.push(`Error uploading file: ${uploadError.message}`); + } + } + if (errorMessages.length > 0) { + Swal.fire({ + icon: "error", + title: "Errors occurred", + html: errorMessages.join("
"), + confirmButtonColor: "red", + }); + return false; + } + return true; +} diff --git a/src/app/business/apply/page.tsx b/src/app/business/apply/page.tsx index d269743..c7e90af 100644 --- a/src/app/business/apply/page.tsx +++ b/src/app/business/apply/page.tsx @@ -7,64 +7,12 @@ import BusinessForm from "@/components/BusinessForm"; import { businessFormSchema } from "@/types/schemas/application.schema"; import Swal from "sweetalert2"; import { getCurrentUserID } from "@/app/api/userApi"; +import { uploadFile } from "@/app/api/generalApi"; type businessSchema = z.infer; const BUCKET_PITCH_NAME = "business-pitches"; let supabase = createSupabaseClient(); -async function uploadFile(file: File, userID: string, bucketName: string) { - const folderPath = `${userID}/`; - const filePath = `${folderPath}${file.name}`; - let errorMessages: string[] = []; - - // check if the folder exists - const { data: folderData, error: folderError } = await supabase.storage - .from(bucketName) - .list(folderPath); - - if (folderError) { - errorMessages.push(`Error checking for folder: ${folderError.message}`); - } - - // if the folder exists, clear the folder - if (folderData && folderData.length > 0) { - // console.log("Folder exists. Clearing contents..."); - - for (const fileItem of folderData) { - const { error: removeError } = await supabase.storage - .from(bucketName) - .remove([`${folderPath}${fileItem.name}`]); - - if (removeError) { - errorMessages.push( - `Error removing file (${fileItem.name}): ${removeError.message}` - ); - } - } - } - - // upload the new file to the folder (if no folderError) - if (errorMessages.length === 0) { - const { error: uploadError } = await supabase.storage - .from(bucketName) - .upload(filePath, file); - - if (uploadError) { - errorMessages.push(`Error uploading file: ${uploadError.message}`); - } - } - if (errorMessages.length > 0) { - Swal.fire({ - icon: "error", - title: "Errors occurred", - html: errorMessages.join("
"), - confirmButtonColor: "red", - }); - return false; - } - return true; -} - export default function ApplyBusiness() { const [applyProject, setApplyProject] = useState(false); const alertShownRef = useRef(false); @@ -83,7 +31,8 @@ export default function ApplyBusiness() { const uploadSuccess = await uploadFile( recvData["businessPitchDeck"], user.id, - BUCKET_PITCH_NAME + BUCKET_PITCH_NAME, + `${user?.id}/${recvData["businessPitchDeck"].name}` ); if (!uploadSuccess) { @@ -196,7 +145,7 @@ export default function ApplyBusiness() { } }; - fetchUserData(); + // fetchUserData(); }, []); return ( diff --git a/src/types/schemas/application.schema.ts b/src/types/schemas/application.schema.ts index 509ab51..6e0c32b 100644 --- a/src/types/schemas/application.schema.ts +++ b/src/types/schemas/application.schema.ts @@ -151,7 +151,7 @@ const businessFormSchema = z.object({ .refine((file) => file.size < MAX_FILE_SIZE, { message: "File can't be bigger than 5MB.", }) - .refine((file) => file.name.endsWith(".md"), { + .refine((file) => file.name.toLowerCase().endsWith(".md"), { message: "File must be a markdown file (.md).", }), ]),