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 and add user application check
This commit is contained in:
parent
121418ed25
commit
729da7112f
@ -1,17 +1,19 @@
|
|||||||
"use client";
|
"use client";
|
||||||
import { createSupabaseClient } from "@/lib/supabase/clientComponentClient";
|
import { createSupabaseClient } from "@/lib/supabase/clientComponentClient";
|
||||||
import { useState, useEffect } from "react";
|
import { useState, useEffect, useRef } from "react";
|
||||||
import { SubmitHandler } from "react-hook-form";
|
import { SubmitHandler } from "react-hook-form";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import BusinessForm from "@/components/BusinessForm";
|
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";
|
||||||
|
|
||||||
type businessSchema = z.infer<typeof businessFormSchema>;
|
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();
|
let supabase = createSupabaseClient();
|
||||||
|
const alertShownRef = useRef(false);
|
||||||
|
|
||||||
const onSubmit: SubmitHandler<businessSchema> = async (data) => {
|
const onSubmit: SubmitHandler<businessSchema> = async (data) => {
|
||||||
const transformedData = await transformChoice(data);
|
const transformedData = await transformChoice(data);
|
||||||
@ -127,7 +129,17 @@ export default function ApplyBusiness() {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
const hasUserApplied = async (userID: string) => {
|
||||||
|
let { data: business, error } = await supabase
|
||||||
|
.from("business")
|
||||||
|
.select("*")
|
||||||
|
.eq("user_id", userID);
|
||||||
|
console.table(business);
|
||||||
|
if (business) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
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(
|
||||||
@ -150,9 +162,37 @@ export default function ApplyBusiness() {
|
|||||||
);
|
);
|
||||||
return transformedData;
|
return transformedData;
|
||||||
};
|
};
|
||||||
// useEffect(() => {
|
useEffect(() => {
|
||||||
// uploadFile();
|
const fetchUserData = async () => {
|
||||||
// }, []);
|
try {
|
||||||
|
const userID = await getCurrentUserID();
|
||||||
|
if (userID) {
|
||||||
|
const hasApplied = await hasUserApplied(userID);
|
||||||
|
if (hasApplied && !alertShownRef.current) {
|
||||||
|
alertShownRef.current = true;
|
||||||
|
Swal.fire({
|
||||||
|
icon: "info",
|
||||||
|
title: "You Already Have an Account",
|
||||||
|
text: "You have already submitted your business application.",
|
||||||
|
confirmButtonText: "OK",
|
||||||
|
allowOutsideClick: false,
|
||||||
|
allowEscapeKey: false,
|
||||||
|
}).then((result) => {
|
||||||
|
if (result.isConfirmed) {
|
||||||
|
window.location.href = "/";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.error("User ID is undefined.");
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching user ID:", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fetchUserData();
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user