Refactor ApplyProject component to update file upload path and add Lottie animation for loading

This commit is contained in:
Pattadon 2024-10-24 15:50:40 +07:00
parent 4b5be49874
commit f9aa1c586f

View File

@ -6,51 +6,58 @@ import { projectFormSchema } from "@/types/schemas/application.schema";
import { z } from "zod"; import { z } from "zod";
import { SubmitHandler } from "react-hook-form"; import { SubmitHandler } from "react-hook-form";
import Swal from "sweetalert2"; import Swal from "sweetalert2";
import { uploadFile } from "@/app/api/generalApi";
type projectSchema = z.infer<typeof projectFormSchema>; type projectSchema = z.infer<typeof projectFormSchema>;
let supabase = createSupabaseClient(); let supabase = createSupabaseClient();
const BUCKET_PITCH_NAME = "project-pitches"; const BUCKET_PITCH_APPLICATION_NAME = "project-pitches";
const BUCKET_LOGO_NAME = "project-logo";
const BUCKET_PHOTOS_NAME = "project-additional-photos";
export default function ApplyProject() { export default function ApplyProject() {
const onSubmit: SubmitHandler<projectSchema> = async (data) => { const onSubmit: SubmitHandler<projectSchema> = async (data) => {
alert("มาแน้ววว"); alert("มาแน้ววว");
await sendApplication(data);
console.table(data); // console.table(data);
console.log(typeof data["projectPhotos"], data["projectPhotos"]); // console.log(typeof data["projectPhotos"], data["projectPhotos"]);
}; };
const sendApplication = async (recvData: any) => { const sendApplication = async (recvData: any) => {
const { const {
data: { user }, data: { user },
} = await supabase.auth.getUser(); } = await supabase.auth.getUser();
let projectId = null;
const pitchType = typeof recvData["projectPitchDeck"]; const pitchType = typeof recvData["projectPitchDeck"];
// save the general application data to project_application table
const { data, error } = await supabase const { data: project_application, error } = await supabase
.from("project_application") .from("project_application")
.insert([ .insert([
{ {
user_id: user?.id, user_id: user?.id,
pitch_deck_url: pitch_deck_url:
pitchType === "string" ? recvData["businessPitchDeck"] : "", pitchType === "string" ? recvData["projectPitchDeck"] : "",
target_investment: recvData["targetInvest"], target_investment: recvData["targetInvest"],
deadline: recvData["deadline"], deadline: recvData["deadline"],
project_name: recvData["projectName"], project_name: recvData["projectName"],
project_type_id: recvData["projectType"],
short_description: recvData["shortDescription"],
min_investment: recvData["minInvest"],
}, },
]) ])
.select(); .select();
if (project_application) {
projectId = project_application[0].id;
}
// upload pitch file
if (pitchType === "object") { if (pitchType === "object") {
if (user?.id) { if (user?.id) {
// const uploadSuccess = await uploadFile( const uploadSuccess = await uploadFile(
// recvData["businessPitchDeck"], recvData["businessPitchDeck"],
// user.id, user.id,
// BUCKET_PITCH_NAME BUCKET_PITCH_APPLICATION_NAME,
// ); `${user?.id}/${projectId}/pitches/${recvData["projectPitchDeck"].name}`
);
// if (!uploadSuccess) { if (!uploadSuccess) {
// return; return;
// } }
console.log("file upload successful"); console.log("file upload successful");
} else { } else {
@ -67,7 +74,7 @@ export default function ApplyProject() {
confirmButtonColor: error == null ? "green" : "red", confirmButtonColor: error == null ? "green" : "red",
}).then((result) => { }).then((result) => {
if (result.isConfirmed) { if (result.isConfirmed) {
window.location.href = "/"; // window.location.href = "/";
} }
}); });
}; };