mirror of
https://github.com/Sosokker/B2D-Ventures.git
synced 2025-12-19 14:04:06 +01:00
Refactor ApplyBusiness component to upload files to Supabase storage and update file upload path
This commit is contained in:
parent
24f452ae70
commit
a225189a24
54
src/app/api/generalApi.ts
Normal file
54
src/app/api/generalApi.ts
Normal 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;
|
||||||
|
}
|
||||||
@ -7,64 +7,12 @@ import BusinessForm from "@/components/BusinessForm";
|
|||||||
import { businessFormSchema } from "@/types/schemas/application.schema";
|
import { businessFormSchema } from "@/types/schemas/application.schema";
|
||||||
import Swal from "sweetalert2";
|
import Swal from "sweetalert2";
|
||||||
import { getCurrentUserID } from "@/app/api/userApi";
|
import { getCurrentUserID } from "@/app/api/userApi";
|
||||||
|
import { uploadFile } from "@/app/api/generalApi";
|
||||||
|
|
||||||
type businessSchema = z.infer<typeof businessFormSchema>;
|
type businessSchema = z.infer<typeof businessFormSchema>;
|
||||||
const BUCKET_PITCH_NAME = "business-pitches";
|
const BUCKET_PITCH_NAME = "business-pitches";
|
||||||
let supabase = createSupabaseClient();
|
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() {
|
export default function ApplyBusiness() {
|
||||||
const [applyProject, setApplyProject] = useState(false);
|
const [applyProject, setApplyProject] = useState(false);
|
||||||
const alertShownRef = useRef(false);
|
const alertShownRef = useRef(false);
|
||||||
@ -83,7 +31,8 @@ export default function ApplyBusiness() {
|
|||||||
const uploadSuccess = await uploadFile(
|
const uploadSuccess = await uploadFile(
|
||||||
recvData["businessPitchDeck"],
|
recvData["businessPitchDeck"],
|
||||||
user.id,
|
user.id,
|
||||||
BUCKET_PITCH_NAME
|
BUCKET_PITCH_NAME,
|
||||||
|
`${user?.id}/${recvData["businessPitchDeck"].name}`
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!uploadSuccess) {
|
if (!uploadSuccess) {
|
||||||
@ -196,7 +145,7 @@ export default function ApplyBusiness() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
fetchUserData();
|
// fetchUserData();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@ -151,7 +151,7 @@ const businessFormSchema = z.object({
|
|||||||
.refine((file) => file.size < MAX_FILE_SIZE, {
|
.refine((file) => file.size < MAX_FILE_SIZE, {
|
||||||
message: "File can't be bigger than 5MB.",
|
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).",
|
message: "File must be a markdown file (.md).",
|
||||||
}),
|
}),
|
||||||
]),
|
]),
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user