Refactor projectFormSchema in application.schema.ts

This commit is contained in:
THIS ONE IS A LITTLE BIT TRICKY KRUB 2024-10-19 17:32:56 +07:00
parent d6c967f0b4
commit 0e25a210fe
3 changed files with 359 additions and 597 deletions

View File

@ -1,310 +1,26 @@
"use client"; "use client";
import { useState } from "react"; import { useState } from "react";
import { Button } from "@/components/ui/button"; import ProjectForm from "@/components/ProjectForm";
import { Input } from "@/components/ui/input"; import { projectFormSchema } from "@/types/schemas/application.schema";
import { Label } from "@/components/ui/label"; import { z } from "zod";
import { Textarea } from "@/components/ui/textarea"; import { SubmitHandler } from "react-hook-form";
type projectSchema = z.infer<typeof projectFormSchema>;
export default function ApplyProject() { export default function ApplyProject() {
const [projectType, setProjectType] = useState<string[]>([]); const [projectType, setProjectType] = useState<string[]>([]);
const [projectPitch, setProjectPitch] = useState("text"); const [projectPitch, setProjectPitch] = useState("text");
const [applyProject, setApplyProject] = useState(false); const [applyProject, setApplyProject] = useState(false);
const [selectedImages, setSelectedImages] = useState<File[]>([]); const [selectedImages, setSelectedImages] = useState<File[]>([]);
const [projectPitchFile, setProjectPitchFile] = useState(""); const [projectPitchFile, setProjectPitchFile] = useState("");
const onSubmit: SubmitHandler<projectSchema> = async (data) => {
alert("มาแน้ววว");
console.table(data);
};
return ( return (
<div> <div>
{" "}
<div className="grid auto-rows-max w-3/4 ml-48 bg-zinc-100 dark:bg-zinc-900 mt-10 pt-12 pb-12"> <div className="grid auto-rows-max w-3/4 ml-48 bg-zinc-100 dark:bg-zinc-900 mt-10 pt-12 pb-12">
{/* header */} <ProjectForm onSubmit={onSubmit} />
<div className="ml-[15%]">
<h1 className="text-3xl font-bold mt-10">
Begin Your First Fundraising Project
</h1>
<p className="mt-3 text-sm text-neutral-500">
Starting a fundraising project is mandatory for all businesses. This
step is crucial <br />
to begin your journey and unlock the necessary tools for raising
funds.
</p>
{/* project's name */}
<div className="mt-10 space-y-5">
<Label htmlFor="projectName" className="font-bold text-lg">
Project name
</Label>
<div className="flex space-x-5">
<Input
type="text"
id="projectName"
className="w-96"
{...registerSecondForm("projectName")}
/>
</div>
</div>
{errorsProject.projectName && (
<p className="text-red-500 text-sm">
{errorsProject.projectName.message as string}
</p>
)}
{/* project type */}
{/* <MultipleOptionSelector
header={<>Project type</>}
fieldName="projectType"
choices={projectType}
// handleFunction={handleProjectFieldChange}
description={<>Please specify the primary purpose of the funds</>}
placeholder="Select a Project type"
selectLabel="Project type"
/> */}
{errorsProject.projectType && (
<p className="text-red-500 text-sm">
{errorsProject.projectType.message as string}
</p>
)}
{/* short description */}
<div className="mt-10 space-y-5">
<Label htmlFor="shortDescription" className="font-bold text-lg">
Short description
</Label>
<div className="flex space-x-5">
<Textarea
id="shortDescription"
className="w-96"
{...registerSecondForm("shortDescription")}
/>
<span className="text-[12px] text-neutral-500 self-center">
Could you provide a brief description of your project <br /> in
one or two sentences?
</span>
</div>
</div>
{errorsProject.shortDescription && (
<p className="text-red-500 text-sm">
{errorsProject.shortDescription.message as string}
</p>
)}
{/* Pitch deck */}
<div className="mt-10 space-y-5">
<Label htmlFor="projectPitchDeck" className="font-bold text-lg">
Pitch deck
</Label>
<div className="flex space-x-2 w-96">
<Button
type="button"
variant={projectPitch === "text" ? "default" : "outline"}
onClick={() => setProjectPitch("text")}
className="w-32 h-12 text-base"
>
Paste URL
</Button>
<Button
type="button"
variant={projectPitch === "file" ? "default" : "outline"}
onClick={() => setProjectPitch("file")}
className="w-32 h-12 text-base"
>
Upload a file
</Button>
</div>
<div className="flex space-x-5">
<Input
type={projectPitch}
id="projectPitchDeck"
className="w-96"
placeholder={
projectPitch === "file"
? "Upload your Markdown file"
: "https:// "
}
accept={projectPitch === "file" ? ".md" : undefined}
{...(projectPitch === "text"
? registerSecondForm("projectPitchDeck", {
required: true,
})
: {
onChange: (e) => {
const file = e.target.files?.[0];
setValueProject("projectPitchDeck", file);
setProjectPitchFile(file?.name || "");
},
})}
/>
<span className="text-[12px] text-neutral-500 self-center">
Please upload a file or paste a link to your pitch, which should{" "}
<br />
cover key aspects of your project: what it will do, what
investors <br /> can expect to gain, and any highlights that
make it stand out.
</span>
</div>
{projectPitchFile && (
<div className="flex justify-between items-center border p-2 rounded w-96 text-sm text-foreground">
<span>1. {projectPitchFile}</span>
<Button
className="ml-4"
onClick={() => {
setValueProject("projectPitchDeck", "");
setProjectPitchFile("");
}}
>
Remove
</Button>
</div>
)}
</div>
{errorsProject.projectPitchDeck && (
<p className="text-red-500 text-sm">
{errorsProject.projectPitchDeck.message as string}
</p>
)}
{/* project logo */}
<div className="mt-10 space-y-5">
<Label htmlFor="projectLogo" className="font-bold text-lg mt-10">
Project logo
</Label>
<div className="flex space-x-5">
<Input
type="file"
id="projectLogo"
className="w-96"
accept="image/*"
onChange={(e) => {
const file = e.target.files?.[0];
registerSecondForm("projectLogo").onChange({
target: { name: "projectLogo", value: file },
});
}}
/>
<span className="text-[12px] text-neutral-500 self-center">
Please upload the logo picture that best represents your
project.
</span>
</div>
</div>
{errorsProject.projectLogo && (
<p className="text-red-500 text-sm">
{errorsProject.projectLogo.message as string}
</p>
)}
<div className="mt-10 space-y-5">
<Label htmlFor="projectPhotos" className="font-bold text-lg mt-10">
Project photos
</Label>
<div className="flex space-x-5">
<Input
type="file"
id="projectPhotos"
multiple
accept="image/*"
className="w-96"
{...registerSecondForm("projectPhotos", {
required: true,
onChange: handleFileChange,
})}
/>
<span className="text-[12px] text-neutral-500 self-center">
Feel free to upload any additional images that provide <br />
further insight into your project.
</span>
</div>
<div className="mt-5 space-y-2 w-96">
{selectedImages.map((image, index) => (
<div
key={index}
className="flex justify-between items-center border p-2 rounded"
>
<span>{image.name}</span>
<Button
variant="outline"
onClick={() => handleRemoveImage(index)}
className="ml-4"
type="reset"
>
Remove
</Button>
</div>
))}
</div>
</div>
{errorsProject.projectPhotos && (
<p className="text-red-500 text-sm">
{errorsProject.projectPhotos.message as string}
</p>
)}
{/* Minimum Investment */}
<div className="space-y-5 mt-10">
<Label htmlFor="minInvest" className="font-bold text-lg">
Minimum investment
</Label>
<div className="flex space-x-5">
<Input
type="number"
id="minInvest"
className="w-96"
placeholder="$ 500"
{...registerSecondForm("minInvest", {
valueAsNumber: true,
})}
/>
<span className="text-[12px] text-neutral-500 self-center">
This helps set clear expectations for investors
</span>
</div>
</div>
{errorsProject.minInvest && (
<p className="text-red-500 text-sm">
{errorsProject.minInvest.message as string}
</p>
)}
{/* Target Investment */}
<div className="space-y-5 mt-10">
<Label htmlFor="targetInvest" className="font-bold text-lg">
Target investment
</Label>
<div className="flex space-x-5">
<Input
type="number"
id="targetInvest"
className="w-96"
placeholder="$ 1,000,000"
{...registerSecondForm("targetInvest", {
valueAsNumber: true,
})}
/>
<span className="text-[12px] text-neutral-500 self-center">
We encourage you to set a specific target investment <br />{" "}
amount that reflects your funding goals.
</span>
</div>
</div>
{errorsProject.targetInvest && (
<p className="text-red-500 text-sm">
{errorsProject.targetInvest.message as string}
</p>
)}
{/* Deadline */}
<div className="space-y-5 mt-10">
<Label htmlFor="deadline" className="font-bold text-lg">
Deadline
</Label>
<div className="flex space-x-5">
<Input
type="datetime-local"
id="deadline"
className="w-96"
{...registerSecondForm("deadline")}
/>
<span className="text-[12px] text-neutral-500 self-center">
What is the deadline for your fundraising project? Setting{" "}
<br /> a clear timeline can help motivate potential investors.
</span>
</div>
</div>
{errorsProject.deadline && (
<p className="text-red-500 text-sm">
{errorsProject.deadline.message as string}
</p>
)}
</div>
</div> </div>
</div> </div>
); );

View File

@ -1,7 +1,6 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { SubmitHandler, useForm } from "react-hook-form"; import { SubmitHandler, useForm, ControllerRenderProps } from "react-hook-form";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { DualOptionSelector } from "@/components/dualSelector";
import { MultipleOptionSelector } from "@/components/multipleSelector"; import { MultipleOptionSelector } from "@/components/multipleSelector";
import { import {
Form, Form,
@ -16,17 +15,11 @@ import { projectFormSchema } from "@/types/schemas/application.schema";
import { z } from "zod"; import { z } from "zod";
import { zodResolver } from "@hookform/resolvers/zod"; import { zodResolver } from "@hookform/resolvers/zod";
import { Label } from "@/components/ui/label"; import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from "@radix-ui/react-tooltip";
import { createSupabaseClient } from "@/lib/supabase/clientComponentClient"; import { createSupabaseClient } from "@/lib/supabase/clientComponentClient";
import { Textarea } from "./ui/textarea"; import { Textarea } from "./ui/textarea";
type projectSchema = z.infer<typeof projectFormSchema>; type projectSchema = z.infer<typeof projectFormSchema>;
type FieldType = ControllerRenderProps<any, "projectPhotos">;
interface ProjectFormProps { interface ProjectFormProps {
onSubmit: SubmitHandler<projectSchema>; onSubmit: SubmitHandler<projectSchema>;
@ -43,26 +36,31 @@ const ProjectForm = ({
{ id: number; name: string }[] { id: number; name: string }[]
>([]); >([]);
const [projectPitch, setProjectPitch] = useState("text"); const [projectPitch, setProjectPitch] = useState("text");
const [applyProject, setApplyProject] = useState(false);
const [selectedImages, setSelectedImages] = useState<File[]>([]); const [selectedImages, setSelectedImages] = useState<File[]>([]);
const [projectPitchFile, setProjectPitchFile] = useState(""); const [projectPitchFile, setProjectPitchFile] = useState("");
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => { const handleFileChange = (
event: React.ChangeEvent<HTMLInputElement>,
field: FieldType
) => {
if (event.target.files) { if (event.target.files) {
const filesArray = Array.from(event.target.files); const filesArray = Array.from(event.target.files);
console.log("first file", filesArray); console.log("first file", filesArray);
setSelectedImages((prevImages) => { setSelectedImages((prevImages) => {
const updatedImages = [...prevImages, ...filesArray]; const updatedImages = [...prevImages, ...filesArray];
console.log("Updated Images Array:", updatedImages); console.log("Updated Images Array:", updatedImages);
field.onChange(updatedImages);
return updatedImages; return updatedImages;
}); });
} }
}; };
const handleRemoveImage = (index: number) => { const handleRemoveImage = (index: number, field: FieldType) => {
setSelectedImages((prevImages) => { setSelectedImages((prevImages) => {
const updatedImages = prevImages.filter((_, i) => i !== index); const updatedImages = prevImages.filter((_, i) => i !== index);
console.log("After removal - Updated Images:", updatedImages); console.log("After removal - Updated Images:", updatedImages);
field.onChange(updatedImages);
return updatedImages; return updatedImages;
}); });
}; };
@ -86,7 +84,7 @@ const ProjectForm = ({
} }
}; };
useEffect(() => { useEffect(() => {
fetchProjectType; fetchProjectType();
}, []); }, []);
return ( return (
<Form {...form}> <Form {...form}>
@ -94,7 +92,6 @@ const ProjectForm = ({
onSubmit={form.handleSubmit(onSubmit as SubmitHandler<projectSchema>)} onSubmit={form.handleSubmit(onSubmit as SubmitHandler<projectSchema>)}
className="space-y-8" className="space-y-8"
> >
<div className="ml-[15%]">
<h1 className="text-3xl font-bold mt-10"> <h1 className="text-3xl font-bold mt-10">
Begin Your First Fundraising Project Begin Your First Fundraising Project
</h1> </h1>
@ -319,11 +316,13 @@ const ProjectForm = ({
multiple multiple
accept="image/*" accept="image/*"
className="w-96" className="w-96"
onChange={handleFileChange} onChange={(event) => {
handleFileChange(event, field);
}}
/> />
<span className="text-[12px] text-neutral-500 self-center"> <span className="text-[12px] text-neutral-500 self-center">
Please upload the logo picture that best represents Please upload the logo picture that best represents your
your project. project.
</span> </span>
</div> </div>
<div className="mt-5 space-y-2 w-96"> <div className="mt-5 space-y-2 w-96">
@ -335,7 +334,7 @@ const ProjectForm = ({
<span>{image.name}</span> <span>{image.name}</span>
<Button <Button
variant="outline" variant="outline"
onClick={() => handleRemoveImage(index)} onClick={() => handleRemoveImage(index, field)}
className="ml-4" className="ml-4"
type="reset" type="reset"
> >
@ -351,56 +350,103 @@ const ProjectForm = ({
)} )}
/> />
{/* Community Size */} {/* Minimum investment */}
<FormField <FormField
control={form.control} control={form.control}
name="communitySize" name="minInvest"
render={({ field }) => ( render={({ field }: { field: any }) => (
<FormItem> <FormItem>
<div className="mt-10 space-y-5">
<FormLabel className="font-bold text-lg">
Minimum investment
</FormLabel>
<FormControl> <FormControl>
<MultipleOptionSelector <div className="flex space-x-5">
header={<>What's the rough size of your community?</>} <Input
fieldName="communitySize" type="number"
choices={communitySize} id="minInvest"
handleFunction={(selectedValues: any) => { placeholder="$ 500"
field.onChange(selectedValues.name); className="w-96"
{...field}
onChange={(e) => {
const value = e.target.value;
field.onChange(value ? parseFloat(value) : null);
}} }}
description={ value={field.value}
<>
Include your email list, social media following (e.g.,
Instagram, Discord, Twitter).
</>
}
placeholder="Select"
selectLabel="Select"
/> />
<span className="text-[12px] text-neutral-500 self-center">
This helps set clear expectations for investors
</span>
</div>
</FormControl> </FormControl>
</div>
<FormMessage /> <FormMessage />
</FormItem> </FormItem>
)} )}
/> />
{/* Target investment */}
<FormField
control={form.control}
name="targetInvest"
render={({ field }: { field: any }) => (
<FormItem>
<div className="mt-10 space-y-5">
<FormLabel className="font-bold text-lg">
Target investment
</FormLabel>
<FormControl>
<div className="flex space-x-5"> <div className="flex space-x-5">
<Switch <Input
onCheckedChange={() => setApplyProject(!applyProject)} type="number"
></Switch> id="targetInvest"
<TooltipProvider> className="w-96"
<Tooltip> placeholder="$ 1,000,000"
<TooltipTrigger asChild> {...field}
<span className="text-[12px] text-neutral-500 self-center cursor-pointer"> onChange={(e) => {
Would you like to apply for your first fundraising project const value = e.target.value;
as well? field.onChange(value ? parseFloat(value) : null);
}}
value={field.value}
/>
<span className="text-[12px] text-neutral-500 self-center">
We encourage you to set a specific target investment{" "}
<br /> amount that reflects your funding goals.
</span> </span>
</TooltipTrigger>
<TooltipContent>
<p className="text-[11px]">
Toggling this option allows you to begin your first
project, <br /> which is crucial for unlocking the tools
necessary to raise funds.
</p>
</TooltipContent>
</Tooltip>
</TooltipProvider>
</div> </div>
</FormControl>
</div>
<FormMessage />
</FormItem>
)}
/>
{/* Deadline */}
<FormField
control={form.control}
name="deadline"
render={({ field }: { field: any }) => (
<FormItem>
<div className="mt-10 space-y-5">
<FormLabel className="font-bold text-lg">Deadline</FormLabel>
<FormControl>
<div className="flex space-x-5">
<Input
type="datetime-local"
id="deadline"
className="w-96"
{...field}
/>
<span className="text-[12px] text-neutral-500 self-center">
What is the deadline for your fundraising project?
Setting <br /> a clear timeline can help motivate
potential investors.
</span>
</div>
</FormControl>
</div>
<FormMessage />
</FormItem>
)}
/>
<center> <center>
<Button <Button
className="mt-12 mb-20 h-10 text-base font-bold py-6 px-5" className="mt-12 mb-20 h-10 text-base font-bold py-6 px-5"
@ -410,7 +456,6 @@ const ProjectForm = ({
</Button> </Button>
</center> </center>
</div> </div>
</div>
</form> </form>
</Form> </Form>
); );

View File

@ -53,10 +53,10 @@ const projectFormSchema = z.object({
projectPhotos: z.custom( projectPhotos: z.custom(
(value) => { (value) => {
if (value instanceof FileList || Array.isArray(value)) { if (value instanceof FileList || Array.isArray(value)) {
if (value.length === 1) { return (
return false; value.length > 0 &&
} Array.from(value).every((item) => item instanceof File)
return Array.from(value).every((item) => item instanceof File); );
} }
return false; return false;
}, },
@ -65,6 +65,7 @@ const projectFormSchema = z.object({
"Must be a FileList or an array of File objects with at least one file.", "Must be a FileList or an array of File objects with at least one file.",
} }
), ),
minInvest: z minInvest: z
.number({ .number({
required_error: "Minimum investment must be a number.", required_error: "Minimum investment must be a number.",