Advertisement
Guest User

Untitled

a guest
Apr 1st, 2024
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 16.26 KB | Source Code | 0 0
  1. "use client";
  2.  
  3. import { Calendar } from "@/components/ui/calendar";
  4. import { useState } from "react";
  5. import { Button } from "@/components/ui/button";
  6. import { format, formatDate, parse } from "date-fns";
  7. import {
  8.   Dialog,
  9.   DialogClose,
  10.   DialogContent,
  11.   DialogDescription,
  12.   DialogFooter,
  13.   DialogHeader,
  14.   DialogTitle,
  15.   DialogTrigger,
  16. } from "@/components/ui/dialog";
  17. import { Input } from "@/components/ui/input";
  18. import { Label } from "@/components/ui/label";
  19. import { zodResolver } from "@hookform/resolvers/zod";
  20. import { useForm } from "react-hook-form";
  21. import { z } from "zod";
  22. import {
  23.   Form,
  24.   FormControl,
  25.   FormDescription,
  26.   FormField,
  27.   FormItem,
  28.   FormLabel,
  29.   FormMessage,
  30. } from "@/components/ui/form";
  31. import {
  32.   Select,
  33.   SelectContent,
  34.   SelectItem,
  35.   SelectTrigger,
  36.   SelectValue,
  37. } from "@/components/ui/select";
  38. import { RadioGroup, RadioGroupItem } from "./ui/radio-group";
  39. import { formSchemaData } from "@/lib/schema";
  40.  
  41. const steps = [
  42.   {
  43.     id: "Step 1",
  44.     name: "General Information",
  45.     fields: ["title", "email", "fullName", "department"],
  46.   },
  47.   {
  48.     id: "Step 2",
  49.     name: "Purpose, Date and Time",
  50.     fields: ["dateOfEvent", "staringTime", "endingTime", "purpose"],
  51.   },
  52.   {
  53.     id: "Step 3",
  54.     name: "Additional Information",
  55.     fields: ["doesHaveDryRun"],
  56.   },
  57.   {
  58.     id: "Step 4",
  59.     name: "Finallizing",
  60.   },
  61. ];
  62.  
  63. export function CalendarPicker() {
  64.   const [date, setDate] = useState<Date | undefined>(new Date());
  65.   const [dialogBox, setDialogBox] = useState(false);
  66.  
  67.   const [currentFormPage, setCurrentFormPage] = useState(0);
  68.   const [previousStep, setPreviousStep] = useState(0);
  69.  
  70.   type Inputs = z.infer<typeof formSchemaData>;
  71.   const form = useForm<Inputs>({
  72.     resolver: zodResolver(formSchemaData),
  73.     defaultValues: {
  74.       title: "",
  75.       email: "",
  76.       fullName: "",
  77.       department: "",
  78.       dateOfEvent: "",
  79.       purpose: "",
  80.       staringTime: "",
  81.       endingTime: "",
  82.       doesHaveDryRun: "",
  83.       dryRunDate: "",
  84.       dryRunStart: "",
  85.       dryRunEnd: "",
  86.     },
  87.   });
  88.  
  89.   function onSubmit(values: any) {
  90.     console.log(values);
  91.  
  92.   }
  93.  
  94.   const openDialogBox = (currentDate: Date | undefined) => {
  95.     setDate(currentDate);
  96.     const dateString = currentDate ? currentDate.toLocaleDateString() : ""; // Convert Date to string
  97.     form.setValue("dateOfEvent", dateString); // Set the value of dateOfEvent field in the form
  98.     setDialogBox(true);
  99.   };
  100.  
  101.   type FieldName = keyof Inputs;
  102.  
  103.   const goToNextPage = async () => {
  104.     const fields = steps[currentFormPage].fields;
  105.     const output = await form.trigger(fields as FieldName[], {
  106.       shouldFocus: true,
  107.     });
  108.  
  109.     if (!output) return;
  110.  
  111.     if (currentFormPage < steps.length - 1) {
  112.       setPreviousStep(currentFormPage);
  113.       setCurrentFormPage((step) => step + 1);
  114.     }
  115.  
  116.   };
  117.  
  118.   const goToPreviewsPage = () => {
  119.     if (currentFormPage > 0) {
  120.       setPreviousStep(currentFormPage);
  121.       setCurrentFormPage((step) => step - 1);
  122.     }
  123.   };
  124.  
  125.   return (
  126.     <div className="flex flex-col h-full gap-y-2">
  127.       {dialogBox && (
  128.         <Dialog>
  129.           <DialogTrigger asChild>
  130.             <Button color="primary-foreground">Add Schedule</Button>
  131.           </DialogTrigger>
  132.           <DialogContent className="sm:max-w-[600px]">
  133.             <Form {...form}>
  134.               <form
  135.                 onSubmit={form.handleSubmit(onSubmit)}
  136.                 className="space-y-8"
  137.               >
  138.                 {currentFormPage === 0 && (
  139.                   <>
  140.                     <DialogHeader>
  141.                       <DialogTitle>General Information</DialogTitle>
  142.                       <DialogDescription>
  143.                         provides assistance in gathering general information,
  144.                         including the name, email, department, and purpose of
  145.                         the meeting
  146.                       </DialogDescription>
  147.                     </DialogHeader>
  148.                     <FormField
  149.                       control={form.control}
  150.                       name="title"
  151.                       render={({ field }) => (
  152.                         <FormItem>
  153.                           <FormLabel>Title</FormLabel>
  154.                           <FormControl>
  155.                             <Input placeholder="title of event" {...field} />
  156.                           </FormControl>
  157.                           <FormMessage />
  158.                         </FormItem>
  159.                       )}
  160.                     />
  161.  
  162.                     <FormField
  163.                       control={form.control}
  164.                       name="email"
  165.                       render={({ field }) => (
  166.                         <FormItem>
  167.                           <FormLabel>Email</FormLabel>
  168.                           <FormControl>
  169.                             <Input placeholder="email" {...field} />
  170.                           </FormControl>
  171.                           <FormMessage />
  172.                         </FormItem>
  173.                       )}
  174.                     />
  175.  
  176.                     <FormField
  177.                       control={form.control}
  178.                       name="fullName"
  179.                       render={({ field }) => (
  180.                         <FormItem>
  181.                           <FormLabel>Full Name</FormLabel>
  182.                           <FormControl>
  183.                             <Input placeholder="Full name" {...field} />
  184.                           </FormControl>
  185.                           <FormMessage />
  186.                         </FormItem>
  187.                       )}
  188.                     />
  189.  
  190.                     <FormField
  191.                       control={form.control}
  192.                       name="department"
  193.                       render={({ field }) => (
  194.                         <FormItem>
  195.                           <FormLabel>Department</FormLabel>
  196.                           <FormControl>
  197.                             <Input placeholder="Department" {...field} />
  198.                           </FormControl>
  199.                           <FormMessage />
  200.                         </FormItem>
  201.                       )}
  202.                     />
  203.                   </>
  204.                 )}
  205.  
  206.                 {currentFormPage === 1 && (
  207.                   <>
  208.                     <DialogHeader>
  209.                       <DialogTitle>Purpose, Date and Time</DialogTitle>
  210.                     </DialogHeader>
  211.                     <FormField
  212.                       control={form.control}
  213.                       name="dateOfEvent"
  214.                       render={({ field }) => (
  215.                         <FormItem>
  216.                           <FormLabel>Date of Event (DD/MM/YYY)</FormLabel>
  217.                           <FormControl>
  218.                             <Input disabled {...field} />
  219.                           </FormControl>
  220.                           <FormMessage />
  221.                         </FormItem>
  222.                       )}
  223.                     />
  224.  
  225.                     <FormField
  226.                       control={form.control}
  227.                       name="staringTime"
  228.                       render={({ field }) => (
  229.                         <FormItem>
  230.                           <FormLabel>Start</FormLabel>
  231.                           <FormControl>
  232.                             <Input type="time" placeholder="From" {...field} />
  233.                           </FormControl>
  234.                           <FormMessage />
  235.                         </FormItem>
  236.                       )}
  237.                     />
  238.  
  239.                     <FormField
  240.                       control={form.control}
  241.                       name="endingTime"
  242.                       render={({ field }) => (
  243.                         <FormItem>
  244.                           <FormLabel>Start</FormLabel>
  245.                           <FormControl>
  246.                             <Input type="time" placeholder="End" {...field} />
  247.                           </FormControl>
  248.                           <FormMessage />
  249.                         </FormItem>
  250.                       )}
  251.                     />
  252.  
  253.                     <FormField
  254.                       control={form.control}
  255.                       name="purpose"
  256.                       render={({ field }) => (
  257.                         <FormItem>
  258.                           <FormLabel>Purpose</FormLabel>
  259.                           <Select
  260.                             onValueChange={field.onChange}
  261.                             defaultValue={field.value}
  262.                           >
  263.                             <FormControl>
  264.                               <SelectTrigger>
  265.                                 <SelectValue placeholder="Select a purpose" />
  266.                               </SelectTrigger>
  267.                             </FormControl>
  268.                             <SelectContent>
  269.                               <SelectItem value="class">
  270.                                 Online Class
  271.                               </SelectItem>
  272.                               <SelectItem value="academicCulminatingClass">
  273.                                 Online Academic Culminating Classes
  274.                               </SelectItem>
  275.                               <SelectItem value="meeting">
  276.                                 Online Meeting
  277.                               </SelectItem>
  278.                               <SelectItem value="studentDevelopment">
  279.                                 Online Student Development
  280.                               </SelectItem>
  281.                               <SelectItem value="facultyDevelopment">
  282.                                 Online Faculty Development
  283.                               </SelectItem>
  284.                             </SelectContent>
  285.                           </Select>
  286.  
  287.                           <FormMessage />
  288.                         </FormItem>
  289.                       )}
  290.                     />
  291.                   </>
  292.                 )}
  293.  
  294.                 {currentFormPage === 2 && (
  295.                   <>
  296.                     <DialogHeader>
  297.                       <DialogTitle>Purpose, Date and Time</DialogTitle>
  298.                     </DialogHeader>
  299.                     <FormField
  300.                       control={form.control}
  301.                       name="doesHaveDryRun"
  302.                       render={({ field }) => (
  303.                         <FormItem className="space-y-3">
  304.                           <FormLabel>
  305.                             (Optional) Preffered Meeting Date / Dry Run
  306.                           </FormLabel>
  307.                           <FormControl>
  308.                             <RadioGroup
  309.                               onValueChange={field.onChange}
  310.                               defaultValue={field.value}
  311.                               className="flex flex-col space-y-1"
  312.                             >
  313.                               <FormItem className="flex items-center space-x-3 space-y-0">
  314.                                 <FormControl>
  315.                                   <RadioGroupItem value="none" />
  316.                                 </FormControl>
  317.                                 <FormLabel className="font-normal">
  318.                                   None / No
  319.                                 </FormLabel>
  320.                               </FormItem>
  321.                               <FormItem className="flex items-center space-x-3 space-y-0">
  322.                                 <FormControl>
  323.                                   <RadioGroupItem value="yes" />
  324.                                 </FormControl>
  325.                                 <FormLabel className="font-normal">
  326.                                   Yes
  327.                                 </FormLabel>
  328.                               </FormItem>
  329.                             </RadioGroup>
  330.                           </FormControl>
  331.  
  332.                           {field.value === "yes" && (
  333.                             <FormItem>
  334.                               <div className="flex flex-col gap-2 pt-2">
  335.                                 <Label>(Dry Run) Time of Event</Label>
  336.                                 <div className="flex flex-row gap-2">
  337.                                   <FormField
  338.                                     control={form.control}
  339.                                     name="dryRunDate"
  340.                                     render={({ field }) => (
  341.                                       <FormItem>
  342.                                         <FormLabel>Date</FormLabel>
  343.                                         <FormControl>
  344.                                           <Input
  345.                                             type="date"
  346.                                             placeholder="Date"
  347.                                             {...field}
  348.                                           />
  349.                                         </FormControl>
  350.                                         <FormMessage />
  351.                                       </FormItem>
  352.                                     )}
  353.                                   />
  354.                                   <FormField
  355.                                     control={form.control}
  356.                                     name="dryRunStart"
  357.                                     render={({ field }) => (
  358.                                       <FormItem>
  359.                                         <FormLabel>Start</FormLabel>
  360.                                         <FormControl>
  361.                                           <Input
  362.                                             type="time"
  363.                                             placeholder="Start"
  364.                                             {...field}
  365.                                           />
  366.                                         </FormControl>
  367.                                         <FormMessage />
  368.                                       </FormItem>
  369.                                     )}
  370.                                   />
  371.                                   <FormField
  372.                                     control={form.control}
  373.                                     name="dryRunEnd"
  374.                                     render={({ field }) => (
  375.                                       <FormItem>
  376.                                         <FormLabel>End</FormLabel>
  377.                                         <FormControl>
  378.                                           <Input
  379.                                             type="time"
  380.                                             placeholder="End"
  381.                                             {...field}
  382.                                           />
  383.                                         </FormControl>
  384.                                         <FormMessage />
  385.                                       </FormItem>
  386.                                     )}
  387.                                   />
  388.                                 </div>
  389.                               </div>
  390.                             </FormItem>
  391.                           )}
  392.  
  393.                           <FormMessage />
  394.                         </FormItem>
  395.                       )}
  396.                     />
  397.                   </>
  398.                 )}
  399.  
  400.                 {currentFormPage === 3 && (
  401.                   <>
  402.                     <DialogHeader>
  403.                       <DialogTitle>Finalize</DialogTitle>
  404.                     </DialogHeader>
  405.                   </>
  406.                 )}
  407.  
  408.                 <DialogFooter>
  409.                   <Button variant="default" onClick={goToPreviewsPage}>
  410.                     Back
  411.                   </Button>
  412.                   {currentFormPage === 3 ? (
  413.                     <Button type="submit">Submit</Button>
  414.                   ) : (
  415.                     <Button variant="default" onClick={goToNextPage}>
  416.                       Next
  417.                     </Button>
  418.                   )}
  419.                 </DialogFooter>
  420.               </form>
  421.             </Form>
  422.           </DialogContent>
  423.         </Dialog>
  424.       )}
  425.  
  426.       <Calendar
  427.         mode="single"
  428.         selected={date}
  429.         onSelect={openDialogBox} // Pass selected date to a function
  430.         className="shadow border rounded-md h-full w-full flex"
  431.         classNames={{
  432.           months:
  433.             "flex w-full flex-col sm:flex-row space-y-4 sm:space-x-4 sm:space-y-0 flex-1",
  434.           month: "space-y-4 w-full flex flex-col",
  435.           table: "w-full h-full border-collapse space-y-1",
  436.           head_row: "",
  437.           row: "w-full mt-2",
  438.         }}
  439.       />
  440.     </div>
  441.   );
  442. }
  443.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement