diff --git a/src/app/dashboard/new/[id]/page.tsx b/src/app/dashboard/new/[id]/page.tsx new file mode 100644 index 0000000..d676e3e --- /dev/null +++ b/src/app/dashboard/new/[id]/page.tsx @@ -0,0 +1,254 @@ +"use client"; +import React, { useEffect } from "react"; +import { useForm } from "react-hook-form"; +import { z } from "zod"; // Import Zod +import { zodResolver } from "@hookform/resolvers/zod"; +import { Button } from "@/components/ui/button"; +import { + Form, + FormControl, + FormDescription, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@/components/ui/form"; +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from "@/components/ui/select"; +import { Input } from "@/components/ui/input"; +import { Textarea } from "@/components/ui/textarea"; +import { useToast } from "@/components/ui/use-toast"; +import { useInnovationsCreateMutation } from "@/redux/features/innovations/innovationsApiSlice"; +import RequireAuth from "@/redux/features/auth/RequireAuth"; +import { FileInput } from "@/components/ui/FileInput"; +import { useInnovationsFetchOneQuery } from "@/redux/features/innovations/innovationsApiSlice"; + +const isBrowser = typeof window !== "undefined"; +const FileListType = isBrowser ? FileList : Array; + +//Define the schema for the form +const MAX_UPLOAD_SIZE = 1024 * 1024 * 3; // 3MB +const InnovationSchema = z.object({ + title: z.string().min(2).max(50), + description: z.string().min(2), + category: z.string().min(1).max(3), + status: z.string().min(1).max(3), + co_authors: z.string().min(2).max(50).optional(), + banner_image: z + .instanceof(FileListType) + .optional() + .refine((file) => file == null || file?.length == 1, "File is required."), + // .refine((file) => { + // return !file || file.size <= MAX_UPLOAD_SIZE; + // }, "File size must be less than 3MB"), + + //banner_image +}); +type Innovation = z.infer; + +interface Params { + id: string; +} + +const InnovationPage = ({ params }: { params: Params }) => { + const id = parseInt(params.id, 10); + console.log(id); + const { + data: innovationData, + isLoading: innovationGetLoading, + error, + } = useInnovationsFetchOneQuery({ id: id }); + console.log(innovationData); + + const form = useForm({ + defaultValues: innovationData + ? InnovationSchema.parse({ + title: innovationData.title, + description: innovationData.description, + category: innovationData.category, + status: innovationData.status, + }) + : undefined, + resolver: zodResolver(InnovationSchema), + }); + + const [createInovation, { isLoading }] = useInnovationsCreateMutation(); + + //initialize toast + const { toast } = useToast(); + + //Function that handles submision of validated data + const onSubmit = async (data: Innovation) => { + // Submit the data to your API or perform any other action + createInovation(data) + .unwrap() + .then((response) => { + // toast created successfully + toast({ + title: "Innovation Created successfully", + description: "You can now view you innovation in your profile", + }); + console.log(response); + }) + .catch((error) => { + console.log(error); + }); + }; + + const fileRef = form.register("banner_image"); + + return ( + // +
+

+ Create a New Innovation +

+ +
+ + ( + + Title + + + + + + + )} + /> + + ( + + Co Authors + + + + + + + )} + /> + + ( + + Category + + + + + )} + /> + ( + + Status + + + + + )} + /> + + ( + + Banner image + + + + + )} + /> + + ( + + Description + +