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