From a04a3a228ffcc0c5d1c1198c88fc5af4e8371956 Mon Sep 17 00:00:00 2001 From: Pattadon Date: Mon, 11 Nov 2024 09:54:15 +0700 Subject: [PATCH] feat: refactor user application checks and move related functions to a new query module --- src/app/business/apply/page.tsx | 39 +++------------------------------ src/app/business/apply/query.ts | 38 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 36 deletions(-) create mode 100644 src/app/business/apply/query.ts diff --git a/src/app/business/apply/page.tsx b/src/app/business/apply/page.tsx index 1025b45..99832be 100644 --- a/src/app/business/apply/page.tsx +++ b/src/app/business/apply/page.tsx @@ -9,6 +9,7 @@ import Swal from "sweetalert2"; import { getCurrentUserID } from "@/app/api/userApi"; import { uploadFile } from "@/app/api/generalApi"; import { Loader } from "@/components/loading/loader"; +import { hasUserApplied, transformChoice } from "./query"; type businessSchema = z.infer; const BUCKET_PITCH_NAME = "business-application"; @@ -77,48 +78,14 @@ export default function ApplyBusiness() { }); }; - const hasUserApplied = async (userID: string) => { - let { data: business, error } = await supabase.from("business").select("*").eq("user_id", userID); - let { data: businessApplication, error: applicationError } = await supabase - .from("business_application") - .select("*") - .eq("user_id", userID); - // console.table(business); - if (error || applicationError) { - console.error(error); - console.error(applicationError); - } - if ((business && business.length != 0) || (businessApplication && businessApplication.length != 0)) { - return true; - } - return false; - }; - const transformChoice = (data: any) => { - // convert any yes and no to true or false - const transformedData = Object.entries(data).reduce((acc: Record, [key, value]) => { - if (typeof value === "string") { - const lowerValue = value.toLowerCase(); - if (lowerValue === "yes") { - acc[key] = true; - } else if (lowerValue === "no") { - acc[key] = false; - } else { - acc[key] = value; // keep other string values unchanged - } - } else { - acc[key] = value; // keep other types unchanged - } - return acc; - }, {}); - return transformedData; - }; + useEffect(() => { const fetchUserData = async () => { try { setSucess(false); const userID = await getCurrentUserID(); if (userID) { - const hasApplied = await hasUserApplied(userID); + const hasApplied = await hasUserApplied(supabase, userID); setSucess(true); if (hasApplied && !alertShownRef.current) { alertShownRef.current = true; diff --git a/src/app/business/apply/query.ts b/src/app/business/apply/query.ts new file mode 100644 index 0000000..0e778aa --- /dev/null +++ b/src/app/business/apply/query.ts @@ -0,0 +1,38 @@ +import { SupabaseClient } from "@supabase/supabase-js"; + +export const hasUserApplied = async (supabase: SupabaseClient, userID: string) => { + let { data: business, error } = await supabase.from("business").select("*").eq("user_id", userID); + let { data: businessApplication, error: applicationError } = await supabase + .from("business_application") + .select("*") + .eq("user_id", userID); + // console.table(business); + if (error || applicationError) { + console.error(error); + console.error(applicationError); + } + if ((business && business.length > 0) || (businessApplication && businessApplication.length > 0)) { + return true; + } + return false; +}; +export const transformChoice = (data: any) => { + // convert any yes and no to true or false + const transformedData = Object.entries(data).reduce((acc: Record, [key, value]) => { + if (typeof value === "string") { + const lowerValue = value.toLowerCase(); + if (lowerValue === "yes") { + acc[key] = true; + } else if (lowerValue === "no") { + acc[key] = false; + } else { + acc[key] = value; // keep other string values unchanged + } + } else { + acc[key] = value; // keep other types unchanged + } + return acc; + }, {}); + return transformedData; +}; +