Refactor Apply page component to handle yes/no choices with DualOptionSelector and Selector components, and update form field change handling

This commit is contained in:
Pattadon 2024-10-11 14:37:57 +07:00
parent 5fea323938
commit e9ce0714b3

View File

@ -28,11 +28,24 @@ import { DualOptionSelector } from "@/components/dualSelector";
import { MultipleOptionSelector } from "@/components/multipleSelector"; import { MultipleOptionSelector } from "@/components/multipleSelector";
export default function Apply() { export default function Apply() {
const pitchDeckSchema = z
.union([
z.string().url("Pitch deck must be a valid URL."),
z.instanceof(File).refine((file) => file.size > 0, {
message: "A file must be selected.",
}),
])
.refine((value) => typeof value === "string" || value instanceof File, {
message: "Pitch deck must be either a file or a URL.",
});
const formSchema = z.object({ const formSchema = z.object({
companyName: z.string().min(5, { companyName: z.string().min(5, {
message: "Company name must be at least 5 characters.", message: "Company name must be at least 5 characters.",
}), }),
industry: z.string(), industry: z.string({
required_error: "Please select one of the option",
}),
isInUS: z isInUS: z
.string({ .string({
required_error: "Please select either 'Yes' or 'No'.", required_error: "Please select either 'Yes' or 'No'.",
@ -64,6 +77,10 @@ export default function Apply() {
}) })
.positive() .positive()
.max(9999999999, "Total raised must be a realistic amount."), .max(9999999999, "Total raised must be a realistic amount."),
communitySize: z.string({
required_error: "Please select one of the option",
}),
pitchDeck: pitchDeckSchema,
}); });
let supabase = createSupabaseClient(); let supabase = createSupabaseClient();
const { const {
@ -133,9 +150,19 @@ export default function Apply() {
}, },
{} {}
); );
if (data.pitchDeck instanceof File) {
console.log("File Uploaded:", data.pitchDeck.name);
} else {
console.log("URL Provided:", data.pitchDeck);
}
alert(JSON.stringify(transformedData)); alert(JSON.stringify(transformedData));
}; };
const handleBusinessPitchChange = (type: string) => {
setBusinessPitch(type);
// clear out old data
setValue("pitchDeck", "");
};
const handleFieldChange = (fieldName: string, value: any) => { const handleFieldChange = (fieldName: string, value: any) => {
switch (fieldName) { switch (fieldName) {
case "isInUS": case "isInUS":
@ -204,7 +231,8 @@ export default function Apply() {
<div className="grid grid-flow-row auto-rows-max w-3/4 ml-1/2"> <div className="grid grid-flow-row auto-rows-max w-3/4 ml-1/2">
<h1 className="text-3xl font-bold mt-10 ml-96">About your company</h1> <h1 className="text-3xl font-bold mt-10 ml-96">About your company</h1>
<p className="ml-96 mt-5 text-neutral-500"> <p className="ml-96 mt-5 text-neutral-500">
All requested information in this section is required. <span className="text-red-500 font-bold">**</span>All requested
information in this section is required.
</p> </p>
{/* company name */} {/* company name */}
@ -248,6 +276,11 @@ export default function Apply() {
placeholder="Select an industry" placeholder="Select an industry"
selectLabel="Industry" selectLabel="Industry"
/> />
{errors.industry && (
<p className="text-red-500 text-sm">
{errors.industry.message as string}
</p>
)}
{/* <input {/* <input
type="hidden" type="hidden"
{...register("industry")} {...register("industry")}
@ -277,11 +310,7 @@ export default function Apply() {
</div> </div>
{errors.totalRaised && ( {errors.totalRaised && (
<p className="text-red-500 text-sm"> <p className="text-red-500 text-sm">
{errors.totalRaised && ( {errors.totalRaised.message as string}
<p className="text-red-500 text-sm">
{errors.totalRaised.message as string}
</p>
)}
</p> </p>
)} )}
</div> </div>
@ -310,11 +339,7 @@ export default function Apply() {
/> />
{errors.isInUS && ( {errors.isInUS && (
<p className="text-red-500 text-sm"> <p className="text-red-500 text-sm">
{errors.isInUS && ( {errors.isInUS.message as string}
<p className="text-red-500 text-sm">
{errors.isInUS.message as string}
</p>
)}
</p> </p>
)} )}
@ -339,6 +364,11 @@ export default function Apply() {
} }
value={isForSale} value={isForSale}
/> />
{errors.isForSale && (
<p className="text-red-500 text-sm">
{errors.isForSale.message as string}
</p>
)}
{/* Is your company generating revenue?*/} {/* Is your company generating revenue?*/}
<DualOptionSelector <DualOptionSelector
@ -355,7 +385,11 @@ export default function Apply() {
} }
value={isGenerating} value={isGenerating}
/> />
{errors.isGenerating && (
<p className="text-red-500 text-sm">
{errors.isGenerating.message as string}
</p>
)}
{/* Pitch deck */} {/* Pitch deck */}
<div className="space-y-5"> <div className="space-y-5">
<Label htmlFor="pitchDeck" className="font-bold text-lg"> <Label htmlFor="pitchDeck" className="font-bold text-lg">
@ -365,7 +399,7 @@ export default function Apply() {
<Button <Button
type="button" type="button"
variant={businessPitch === "text" ? "default" : "outline"} variant={businessPitch === "text" ? "default" : "outline"}
onClick={() => setBusinessPitch("text")} onClick={() => handleBusinessPitchChange("text")}
className="w-32 h-12 text-base" className="w-32 h-12 text-base"
> >
Paste URL Paste URL
@ -373,24 +407,27 @@ export default function Apply() {
<Button <Button
type="button" type="button"
variant={businessPitch === "file" ? "default" : "outline"} variant={businessPitch === "file" ? "default" : "outline"}
onClick={() => setBusinessPitch("file")} onClick={() => handleBusinessPitchChange("file")}
className="w-32 h-12 text-base" className="w-32 h-12 text-base"
> >
Upload a file Upload a file
</Button> </Button>
</div> </div>
<div className="flex space-x-5"> <div className="flex space-x-5">
{businessPitch}
<Input <Input
type={businessPitch === "file" ? "file" : "text"} type={businessPitch === "file" ? "file" : "text"}
id="companyName" id="pitchDeck"
className="w-96" className="w-96"
placeholder={ placeholder={
businessPitch === "file" businessPitch === "file"
? "Upload your Markdown file" ? "Upload your Markdown file"
: "https:// " : "https:// "
} }
accept={businessPitch === "file" ? ".md" : undefined} // accept only markdown file accept={businessPitch === "file" ? ".md" : undefined}
{...register("pitchDeck", { required: true })}
/> />
<span className="text-[12px] text-neutral-500 self-center"> <span className="text-[12px] text-neutral-500 self-center">
Your pitch deck and other application info will be used for{" "} Your pitch deck and other application info will be used for{" "}
<br /> <br />
@ -401,12 +438,17 @@ export default function Apply() {
</span> </span>
</div> </div>
</div> </div>
{errors.pitchDeck && (
<p className="text-red-500 text-sm">
{errors.pitchDeck.message as string}
</p>
)}
{/* What's the rough size of your community? */} {/* What's the rough size of your community? */}
<div className="mt-10 space-y-5"> {/* <div className="mt-10 space-y-5">
<Label htmlFor="companySize" className="font-bold text-lg mt-10"> <Label
What's the rough size of your <br /> community? htmlFor="companySize"
</Label> className="font-bold text-lg mt-10"
></Label>
<div className="flex space-x-5"> <div className="flex space-x-5">
<Select> <Select>
<SelectTrigger className="w-96"> <SelectTrigger className="w-96">
@ -421,14 +463,36 @@ export default function Apply() {
</SelectGroup> </SelectGroup>
</SelectContent> </SelectContent>
</Select> </Select>
<span className="text-[12px] text-neutral-500 self-center"> <span className="text-[12px] text-neutral-500 self-center"></span>
</div>
</div> */}
<MultipleOptionSelector
header={
<>
What's the rough size of your <br /> community?
</>
}
fieldName="communitySize"
choices={communitySize}
handleFunction={handleFieldChange}
description={
<>
{" "}
Include your email list, social media following (i.e. Include your email list, social media following (i.e.
Instagram, <br /> Discord, Facebook, Twitter, TikTok). Wed Instagram, <br /> Discord, Facebook, Twitter, TikTok). Wed
like to understand the <br /> rough size of your current like to understand the <br /> rough size of your current
audience. audience.
</span> </>
</div> }
</div> placeholder="Select"
selectLabel="Select"
/>
{errors.communitySize && (
<p className="text-red-500 text-sm">
{errors.communitySize.message as string}
</p>
)}
<div className="flex space-x-5"> <div className="flex space-x-5">
<Switch <Switch