From 0783ff5d6423f9f397720ad2f336f3bab11c91db Mon Sep 17 00:00:00 2001 From: sirin Date: Mon, 14 Oct 2024 22:31:32 +0700 Subject: [PATCH] fix: error on multiple files upload --- src/app/business/apply/page.tsx | 325 ++++++++++---------------------- 1 file changed, 98 insertions(+), 227 deletions(-) diff --git a/src/app/business/apply/page.tsx b/src/app/business/apply/page.tsx index 8c53e27..b4dbe73 100644 --- a/src/app/business/apply/page.tsx +++ b/src/app/business/apply/page.tsx @@ -7,12 +7,7 @@ import { createSupabaseClient } from "@/lib/supabase/clientComponentClient"; import { useEffect, useState } from "react"; import { Textarea } from "@/components/ui/textarea"; import { useForm } from "react-hook-form"; -import { - Tooltip, - TooltipContent, - TooltipProvider, - TooltipTrigger, -} from "@/components/ui/tooltip"; +import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip"; import { z } from "zod"; import { zodResolver } from "@hookform/resolvers/zod"; import { DualOptionSelector } from "@/components/dualSelector"; @@ -62,10 +57,9 @@ export default function Apply() { } }; const imageSchema = z - .custom( - (val) => val && typeof val === "object" && "size" in val && "type" in val, - { message: "Input must be a file." } - ) + .custom((val) => val && typeof val === "object" && "size" in val && "type" in val, { + message: "Input must be a file.", + }) .refine((file) => file.size < MAX_FILE_SIZE, { message: "File can't be bigger than 5MB.", }) @@ -90,9 +84,19 @@ export default function Apply() { projectPitchDeck: createPitchDeckSchema(projectPitch), projectLogo: imageSchema, - projectPhotos: z - .array(z.instanceof(File)) - .min(1, "At least one project photo is required"), + projectPhotos: z.custom( + (value) => { + console.log("Tozod", value); + if (value instanceof FileList || Array.isArray(value)) { + if (value.length === 1) { + return false; + } + return Array.from(value).every((item) => item instanceof File); + } + return false; + }, + { message: "Must be a FileList or an array of File objects with at least one file." } + ), minInvest: z .number({ required_error: "Minimum invesment must be a number.", @@ -179,15 +183,7 @@ export default function Apply() { resolver: zodResolver(projectFormSchema), }); - const communitySize = [ - "N/A", - "0-5K", - "5-10K", - "10-20K", - "20-50K", - "50-100K", - "100K+", - ]; + const communitySize = ["N/A", "0-5K", "5-10K", "10-20K", "20-50K", "50-100K", "100K+"]; useEffect(() => { register("industry"); @@ -199,6 +195,7 @@ export default function Apply() { const handleFileChange = (event: React.ChangeEvent) => { if (event.target.files) { const filesArray = Array.from(event.target.files); + console.log("first file", filesArray); setSelectedImages((prevImages) => { const updatedImages = [...prevImages, ...filesArray]; console.log("Updated Images Array:", updatedImages); @@ -227,24 +224,21 @@ export default function Apply() { 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 - } + 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 types unchanged + acc[key] = value; // keep other string values unchanged } - return acc; - }, - {} - ); + } else { + acc[key] = value; // keep other types unchanged + } + return acc; + }, {}); return transformedData; }; const handleBusinessPitchChange = (type: string) => { @@ -274,9 +268,7 @@ export default function Apply() { }; const fetchIndustry = async () => { - let { data: BusinessType, error } = await supabase - .from("business_type") - .select("value"); + let { data: BusinessType, error } = await supabase.from("business_type").select("value"); if (error) { console.error(error); @@ -319,9 +311,7 @@ export default function Apply() { } }; const fetchProjectType = async () => { - let { data: ProjectType, error } = await supabase - .from("project_type") - .select("value"); + let { data: ProjectType, error } = await supabase.from("project_type").select("value"); if (error) { console.error(error); @@ -344,12 +334,10 @@ export default function Apply() {

- All information submitted in this application is for internal use - only and is treated with the utmost{" "} + All information submitted in this application is for internal use only and is treated with the utmost{" "}

- confidentiality. Companies may apply to raise with B2DVentures more - than once. + confidentiality. Companies may apply to raise with B2DVentures more than once.

@@ -358,8 +346,7 @@ export default function Apply() {

About your company

- **All requested - information in this section is required. + **All requested information in this section is required.

{/* company name */} @@ -369,12 +356,7 @@ export default function Apply() { Company name
- + This should be the name your company uses on your
website and in the market. @@ -383,9 +365,7 @@ export default function Apply() { {errorsBusiness.companyName && (

{errorsBusiness.companyName && ( -

- {errorsBusiness.companyName.message as string} -

+

{errorsBusiness.companyName.message as string}

)}

)} @@ -397,16 +377,12 @@ export default function Apply() { fieldName="industry" choices={industry} handleFunction={handleBusinessFieldChange} - description={ - <>Choose the industry that best aligns with your business. - } + description={<>Choose the industry that best aligns with your business.} placeholder="Select an industry" selectLabel="Industry" /> {errorsBusiness.industry && ( -

- {errorsBusiness.industry.message as string} -

+

{errorsBusiness.industry.message as string}

)} {/* How much money has your company raised to date? */}
@@ -424,15 +400,12 @@ export default function Apply() { })} /> - The sum total of past financing, including angel or venture{" "} -
+ The sum total of past financing, including angel or venture
capital, loans, grants, or token sales.
{errorsBusiness.totalRaised && ( -

- {errorsBusiness.totalRaised.message as string} -

+

{errorsBusiness.totalRaised.message as string}

)}
{/* Is your company incorporated in the United States? */} @@ -449,20 +422,14 @@ export default function Apply() { handleFunction={handleBusinessFieldChange} description={ <> - Only companies that are incorporated or formed in the US are{" "} -
- eligible to raise via Reg CF. If your company is incorporated{" "} -
+ Only companies that are incorporated or formed in the US are
+ eligible to raise via Reg CF. If your company is incorporated
outside the US, we still encourage you to apply. } value={isInUS} /> - {errorsBusiness.isInUS && ( -

- {errorsBusiness.isInUS.message as string} -

- )} + {errorsBusiness.isInUS &&

{errorsBusiness.isInUS.message as string}

} {/* Is your product available (for sale) in market? */} - Only check this box if customers can access, use, or buy your{" "} -
+ Only check this box if customers can access, use, or buy your
product today. } value={isForSale} /> {errorsBusiness.isForSale && ( -

- {errorsBusiness.isForSale.message as string} -

+

{errorsBusiness.isForSale.message as string}

)} {/* Is your company generating revenue?*/} @@ -507,9 +471,7 @@ export default function Apply() { value={isGenerating} /> {errorsBusiness.isGenerating && ( -

- {errorsBusiness.isGenerating.message as string} -

+

{errorsBusiness.isGenerating.message as string}

)} {/* Pitch deck */}
@@ -521,16 +483,14 @@ export default function Apply() { type="button" variant={businessPitch === "text" ? "default" : "outline"} onClick={() => handleBusinessPitchChange("text")} - className="w-32 h-12 text-base" - > + className="w-32 h-12 text-base"> Paste URL
@@ -539,11 +499,7 @@ export default function Apply() { type={businessPitch === "file" ? "file" : "text"} id="pitchDeck" className="w-96" - placeholder={ - businessPitch === "file" - ? "Upload your Markdown file" - : "https:// " - } + placeholder={businessPitch === "file" ? "Upload your Markdown file" : "https:// "} accept={businessPitch === "file" ? ".md" : undefined} // if text use normal register {...(businessPitch === "text" @@ -557,16 +513,12 @@ export default function Apply() { })} /> - Your pitch deck and other application info will be used for{" "} -
+ Your pitch deck and other application info will be used for
internal purposes only.
- Please make sure this document is publicly accessible. This - can
+ Please make sure this document is publicly accessible. This can
be a DocSend, Box, Dropbox, Google Drive or other link.
-

- ** support only markdown(.md) format -

+

** support only markdown(.md) format

{/* box to show file name */} @@ -578,17 +530,14 @@ export default function Apply() { onClick={() => { setValueBusiness("businessPitchDeck", null); setBusinessPitchFile(""); - }} - > + }}> Remove )} {errorsBusiness.businessPitchDeck && ( -

- {errorsBusiness.businessPitchDeck.message as string} -

+

{errorsBusiness.businessPitchDeck.message as string}

)} {" "} - Include your email list, social media following (i.e. - Instagram,
Discord, Facebook, Twitter, TikTok). We’d - like to understand the
rough size of your current - audience. + Include your email list, social media following (i.e. Instagram,
Discord, Facebook, Twitter, + TikTok). We’d like to understand the
rough size of your current audience. } placeholder="Select" selectLabel="Select" /> {errorsBusiness.communitySize && ( -

- {errorsBusiness.communitySize.message as string} -

+

{errorsBusiness.communitySize.message as string}

)}
- setApplyProject(!applyProject)} - > + setApplyProject(!applyProject)}> - Would you like to apply for your first fundraising project - as well? + Would you like to apply for your first fundraising project as well?

- Toggling this option allows you to begin your first - project,
which is crucial for unlocking the tools - necessary to raise funds. + Toggling this option allows you to begin your first project,
which is crucial for unlocking + the tools necessary to raise funds.

@@ -647,14 +588,10 @@ export default function Apply() {
{/* header */}
-

- Begin Your First Fundraising Project -

+

Begin Your First Fundraising Project

- Starting a fundraising project is mandatory for all businesses. - This step is crucial
- to begin your journey and unlock the necessary tools for raising - funds. + Starting a fundraising project is mandatory for all businesses. This step is crucial
+ to begin your journey and unlock the necessary tools for raising funds.

{/* project's name */}
@@ -662,18 +599,11 @@ export default function Apply() { Project name
- +
{errorsProject.projectName && ( -

- {errorsProject.projectName.message as string} -

+

{errorsProject.projectName.message as string}

)} {/* project type */} Please specify the primary purpose of the funds - } + description={<>Please specify the primary purpose of the funds} placeholder="Select a Project type" selectLabel="Project type" /> {errorsProject.projectType && ( -

- {errorsProject.projectType.message as string} -

+

{errorsProject.projectType.message as string}

)} {/* short description */}
@@ -698,21 +624,14 @@ export default function Apply() { Short description
-