completed project's apply

This commit is contained in:
THIS ONE IS A LITTLE BIT TRICKY KRUB 2024-10-27 21:20:40 +07:00
parent fa40037682
commit a0030e39c0

View File

@ -6,12 +6,16 @@ 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"; import { uploadFile } from "@/app/api/generalApi";
import { Loader } from "@/components/loading/loader";
import { useState } from "react";
import { errors } from "@playwright/test";
type projectSchema = z.infer<typeof projectFormSchema>; type projectSchema = z.infer<typeof projectFormSchema>;
let supabase = createSupabaseClient(); let supabase = createSupabaseClient();
const BUCKET_PITCH_APPLICATION_NAME = "project-application"; const BUCKET_PITCH_APPLICATION_NAME = "project-application";
export default function ApplyProject() { export default function ApplyProject() {
const [isSuccess, setIsSuccess] = useState(true);
const onSubmit: SubmitHandler<projectSchema> = async (data) => { const onSubmit: SubmitHandler<projectSchema> = async (data) => {
alert("มาแน้ววว"); alert("มาแน้ววว");
await sendApplication(data); await sendApplication(data);
@ -139,6 +143,7 @@ export default function ApplyProject() {
}; };
const sendApplication = async (recvData: any) => { const sendApplication = async (recvData: any) => {
setIsSuccess(false);
const { const {
data: { user }, data: { user },
} = await supabase.auth.getUser(); } = await supabase.auth.getUser();
@ -155,7 +160,7 @@ export default function ApplyProject() {
displayAlert(error); displayAlert(error);
return; return;
} }
const tagError = saveTags(recvData["tag"], projectId); const tagError = await saveTags(recvData["tag"], projectId);
// if (tagError) { // if (tagError) {
// displayAlert(tagError); // displayAlert(tagError);
// return; // return;
@ -186,48 +191,74 @@ export default function ApplyProject() {
if (!success) { if (!success) {
console.error("Error uploading media files."); console.error("Error uploading media files.");
} }
const folderPath = photos[0].data.path;
const lastSlashIndex = folderPath.lastIndexOf("/");
const photosPath = folderPath.substring(0, lastSlashIndex);
// Log for debugging // console.log("Bucket Name:", BUCKET_PITCH_APPLICATION_NAME);
console.log("Bucket Name:", BUCKET_PITCH_APPLICATION_NAME); // console.log("Logo Path:", logo.data.path);
console.log("Logo Path:", logo.data.path); // console.table(photos);
console.log("Photos Path:", photosPath);
const logoURL = await getPublicURL( const logoURL = await getPrivateURL(
logo.data.path, logo.data.path,
BUCKET_PITCH_APPLICATION_NAME BUCKET_PITCH_APPLICATION_NAME
); );
const photosURL = await getPublicURL( let photoURLsArray: string[] = [];
photosPath, const photoURLPromises = photos.map(
async (item: {
success: boolean;
errors: typeof errors;
data: { path: string };
}) => {
const photoURL = await getPrivateURL(
item.data.path,
BUCKET_PITCH_APPLICATION_NAME BUCKET_PITCH_APPLICATION_NAME
); );
if (photoURL?.signedUrl) {
photoURLsArray.push(photoURL.signedUrl);
} else {
console.error("Signed URL for photo is undefined.");
}
}
);
await Promise.all(photoURLPromises);
// console.log(logoURL.publicUrl, projectId, logo.data.path); // console.log(logoURL.publicUrl, projectId, logo.data.path);
console.log(photosURL, projectId, logo.data.path); // console.log(logoURL?.signedUrl, projectId);
updateImageURL(logoURL.publicUrl, "project_logo", projectId); // console.log(photoURLsArray[0], photoURLsArray[1]);
if (logoURL?.signedUrl) {
await updateImageURL(logoURL.signedUrl, "project_logo", projectId);
} else {
console.error("Signed URL for logo is undefined.");
}
await updateImageURL(photoURLsArray, "project_photos", projectId);
// console.log(logoURL, photosUrl); // console.log(logoURL, photosUrl);
setIsSuccess(true);
displayAlert(error); displayAlert(error);
}; };
const updateImageURL = async ( const updateImageURL = async (
url: string, url: string | string[],
columnName: string, columnName: string,
projectId: number projectId: number
) => { ) => {
const { data, error } = await supabase const { error } = await supabase
.from("project_application") .from("project_application")
.update({ [columnName]: url }) .update({ [columnName]: url })
.eq("id", projectId) .eq("id", projectId);
.select(); // console.log(
console.table(data); // `Updating ${columnName} with URL: ${url} for project ID: ${projectId}`
// );
if (error) {
console.error(error);
}
}; };
const getPublicURL = async (path: string, bucketName: string) => { const getPrivateURL = async (path: string, bucketName: string) => {
const { data } = await supabase.storage.from(bucketName).getPublicUrl(path); const { data } = await supabase.storage
.from(bucketName)
.createSignedUrl(path, 9999999999999999999999999999);
// console.table(data); // console.table(data);
return data; return data;
}; };
return ( return (
<div> <div>
<Loader isSuccess={isSuccess} />
<div className="grid grid-flow-row auto-rows-max w-full h-52 md:h-92 bg-gray-2s00 dark:bg-gray-800 p-5"> <div className="grid grid-flow-row auto-rows-max w-full h-52 md:h-92 bg-gray-2s00 dark:bg-gray-800 p-5">
<h1 className="text-2xl md:text-5xl font-medium md:font-bold justify-self-center md:mt-8"> <h1 className="text-2xl md:text-5xl font-medium md:font-bold justify-self-center md:mt-8">
Apply to raise on B2DVentures Apply to raise on B2DVentures