mirror of
https://github.com/Sosokker/B2D-Ventures.git
synced 2025-12-20 06:24:06 +01:00
Refactor ApplyBusiness component to upload files to Supabase storage
This commit is contained in:
parent
31f80b0af6
commit
121418ed25
@ -11,6 +11,7 @@ type businessSchema = z.infer<typeof businessFormSchema>;
|
|||||||
const BUCKET_NAME = "project-pitches";
|
const BUCKET_NAME = "project-pitches";
|
||||||
export default function ApplyBusiness() {
|
export default function ApplyBusiness() {
|
||||||
const [applyProject, setApplyProject] = useState(false);
|
const [applyProject, setApplyProject] = useState(false);
|
||||||
|
let supabase = createSupabaseClient();
|
||||||
|
|
||||||
const onSubmit: SubmitHandler<businessSchema> = async (data) => {
|
const onSubmit: SubmitHandler<businessSchema> = async (data) => {
|
||||||
const transformedData = await transformChoice(data);
|
const transformedData = await transformChoice(data);
|
||||||
@ -23,9 +24,20 @@ export default function ApplyBusiness() {
|
|||||||
const pitchType = typeof recvData["businessPitchDeck"];
|
const pitchType = typeof recvData["businessPitchDeck"];
|
||||||
if (pitchType === "object") {
|
if (pitchType === "object") {
|
||||||
if (user?.id) {
|
if (user?.id) {
|
||||||
uploadFile(recvData["businessPitchDeck"], user.id, BUCKET_NAME);
|
const uploadSuccess = await uploadFile(
|
||||||
|
recvData["businessPitchDeck"],
|
||||||
|
user.id,
|
||||||
|
BUCKET_NAME
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!uploadSuccess) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("file upload successful");
|
||||||
} else {
|
} else {
|
||||||
console.error("User ID is undefined. Cannot upload file.");
|
console.error("user ID is undefined.");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,9 +74,11 @@ export default function ApplyBusiness() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
async function uploadFile(file: File, userID: string, bucketName: string) {
|
async function uploadFile(file: File, userID: string, bucketName: string) {
|
||||||
const folderPath = `${userID}/`;
|
const folderPath = `${userID}/`;
|
||||||
const filePath = `${folderPath}${file.name}`;
|
const filePath = `${folderPath}${file.name}`;
|
||||||
|
let errorMessages: string[] = [];
|
||||||
|
|
||||||
// check if the folder exists
|
// check if the folder exists
|
||||||
const { data: folderData, error: folderError } = await supabase.storage
|
const { data: folderData, error: folderError } = await supabase.storage
|
||||||
@ -72,8 +86,7 @@ export default function ApplyBusiness() {
|
|||||||
.list(folderPath);
|
.list(folderPath);
|
||||||
|
|
||||||
if (folderError) {
|
if (folderError) {
|
||||||
console.error("Error checking for folder:", folderError.message);
|
errorMessages.push(`Error checking for folder: ${folderError.message}`);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the folder exists, clear the folder
|
// if the folder exists, clear the folder
|
||||||
@ -86,29 +99,35 @@ export default function ApplyBusiness() {
|
|||||||
.remove([`${folderPath}${fileItem.name}`]);
|
.remove([`${folderPath}${fileItem.name}`]);
|
||||||
|
|
||||||
if (removeError) {
|
if (removeError) {
|
||||||
console.error(
|
errorMessages.push(
|
||||||
`Error removing file (${fileItem.name}):`,
|
`Error removing file (${fileItem.name}): ${removeError.message}`
|
||||||
removeError.message
|
|
||||||
);
|
);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// upload new file to the folder
|
// upload the new file to the folder (if no folderError)
|
||||||
const { data: uploadData, error: uploadError } = await supabase.storage
|
if (errorMessages.length === 0) {
|
||||||
|
const { error: uploadError } = await supabase.storage
|
||||||
.from(bucketName)
|
.from(bucketName)
|
||||||
.upload(filePath, file);
|
.upload(filePath, file);
|
||||||
|
|
||||||
if (uploadError) {
|
if (uploadError) {
|
||||||
console.error("Error uploading file:", uploadError.message);
|
errorMessages.push(`Error uploading file: ${uploadError.message}`);
|
||||||
return;
|
}
|
||||||
|
}
|
||||||
|
if (errorMessages.length > 0) {
|
||||||
|
Swal.fire({
|
||||||
|
icon: "error",
|
||||||
|
title: "Errors occurred",
|
||||||
|
html: errorMessages.join("<br>"),
|
||||||
|
confirmButtonColor: "red",
|
||||||
|
});
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("File uploaded successfully:", uploadData);
|
|
||||||
}
|
|
||||||
|
|
||||||
let supabase = createSupabaseClient();
|
|
||||||
const transformChoice = (data: any) => {
|
const transformChoice = (data: any) => {
|
||||||
// convert any yes and no to true or false
|
// convert any yes and no to true or false
|
||||||
const transformedData = Object.entries(data).reduce(
|
const transformedData = Object.entries(data).reduce(
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user