Refactor ApplyBusiness component to upload files to Supabase storage and update file upload path

This commit is contained in:
Pattadon 2024-10-22 11:13:14 +07:00
parent 24f452ae70
commit a225189a24
3 changed files with 59 additions and 56 deletions

54
src/app/api/generalApi.ts Normal file
View File

@ -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("<br>"),
confirmButtonColor: "red",
});
return false;
}
return true;
}

View File

@ -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<typeof businessFormSchema>;
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("<br>"),
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 (

View File

@ -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).",
}),
]),