Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

Refactor datepicker

Khuram Niaz31f29f25ee7a
diff --git a/src/components/AddJobForm.tsx b/src/components/AddJobForm.tsx
index 2efab26..ece0489 100644
--- a/src/components/AddJobForm.tsx
+++ b/src/components/AddJobForm.tsx
@@ -19,17 +19,13 @@ import {
SelectTrigger,
SelectValue,
} from "./ui/select";
-import { Popover, PopoverContent, PopoverTrigger } from "./ui/popover";
-import { cn } from "@/lib/utils";
-import { Calendar as CalendarIcon } from "lucide-react";
-import { addDays, format } from "date-fns";
-import { Calendar } from "./ui/calendar";
import { z } from "zod";
import { AddJobFormSchema } from "@/models/addJobForm.schema";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { toast } from "./ui/use-toast";
import { ComboboxFormItem } from "./ComboBoxFormItem";
+import { DatePicker } from "./DatePicker";
interface AddJobFormProps {
jobStatuses: { id: string; statusName: string }[];
@@ -47,13 +43,6 @@ export default function AddJobForm({ jobStatuses }: AddJobFormProps) {
{ label: "Job Street", value: "jobstreet" },
{ label: "Other", value: "other" },
];
- const comboOptions = [
- { label: "option1", value: "optionvalue1" },
- { label: "option2", value: "optionvalue2" },
- { label: "option3", value: "optionvalue3" },
- { label: "option4", value: "optionvalue4" },
- ];
- const selectedValue = comboOptions[1].value;
const form = useForm<z.infer<typeof AddJobFormSchema>>({
resolver: zodResolver(AddJobFormSchema),
@@ -202,55 +191,7 @@ export default function AddJobForm({ jobStatuses }: AddJobFormProps) {
render={({ field }) => (
<FormItem className="flex flex-col">
<FormLabel>Due Date</FormLabel>
- <Popover>
- <PopoverTrigger asChild>
- <FormControl>
- <Button
- variant={"outline"}
- className={cn(
- "w-[280px] pl-3 text-left font-normal",
- !field.value && "text-muted-foreground"
- )}
- >
- {field.value ? (
- format(field.value, "PPP")
- ) : (
- <span>Pick a date</span>
- )}
- <CalendarIcon className="ml-auto h-4 w-4 opacity-50" />
- </Button>
- </FormControl>
- </PopoverTrigger>
- <PopoverContent className="w-auto p-0" align="start">
- {/* <Select
- onValueChange={(value) =>
- setDate(addDays(new Date(), parseInt(value)))
- }
- >
- <SelectTrigger>
- <SelectValue placeholder="Select" />
- </SelectTrigger>
- <SelectContent position="popper">
- <SelectItem value="0">Today</SelectItem>
- <SelectItem value="1">Tomorrow</SelectItem>
- <SelectItem value="3">In 3 days</SelectItem>
- <SelectItem value="7">In a week</SelectItem>
- </SelectContent>
- </Select> */}
- <Calendar
- mode="single"
- selected={field.value}
- onSelect={field.onChange}
- /* onSelect={() => {
- setCalendarOpen(false);
- }} */
- disabled={(date) =>
- date > new Date() || date < new Date("1900-01-01")
- }
- initialFocus
- />
- </PopoverContent>
- </Popover>
+ <DatePicker field={field} presets={true} />
<FormMessage />
</FormItem>
)}
@@ -264,55 +205,7 @@ export default function AddJobForm({ jobStatuses }: AddJobFormProps) {
render={({ field }) => (
<FormItem className="flex flex-col">
<FormLabel>Date Applied</FormLabel>
- <Popover>
- <PopoverTrigger asChild>
- <FormControl>
- <Button
- variant={"outline"}
- className={cn(
- "w-[280px] pl-3 text-left font-normal",
- !field.value && "text-muted-foreground"
- )}
- >
- {field.value ? (
- format(field.value, "PPP")
- ) : (
- <span>Pick a date</span>
- )}
- <CalendarIcon className="ml-auto h-4 w-4 opacity-50" />
- </Button>
- </FormControl>
- </PopoverTrigger>
- <PopoverContent className="w-auto p-0" align="start">
- {/* <Select
- onValueChange={(value) =>
- setDate(addDays(new Date(), parseInt(value)))
- }
- >
- <SelectTrigger>
- <SelectValue placeholder="Select" />
- </SelectTrigger>
- <SelectContent position="popper">
- <SelectItem value="0">Today</SelectItem>
- <SelectItem value="1">Tomorrow</SelectItem>
- <SelectItem value="3">In 3 days</SelectItem>
- <SelectItem value="7">In a week</SelectItem>
- </SelectContent>
- </Select> */}
- <Calendar
- mode="single"
- selected={field.value}
- onSelect={field.onChange}
- /* onSelect={() => {
- setCalendarOpen(false);
- }} */
- disabled={(date) =>
- date > new Date() || date < new Date("1900-01-01")
- }
- initialFocus
- />
- </PopoverContent>
- </Popover>
+ <DatePicker field={field} presets={false} />
<FormMessage />
</FormItem>
)}
diff --git a/src/components/DatePicker.tsx b/src/components/DatePicker.tsx
new file mode 100644
index 0000000..24aab1b
--- /dev/null
+++ b/src/components/DatePicker.tsx
@@ -0,0 +1,83 @@
+"use client";
+
+import { addDays, format } from "date-fns";
+import { Calendar as CalendarIcon } from "lucide-react";
+
+import { cn } from "@/lib/utils";
+import { Button } from "@/components/ui/button";
+import { Calendar } from "@/components/ui/calendar";
+import {
+ Popover,
+ PopoverContent,
+ PopoverTrigger,
+} from "@/components/ui/popover";
+import {
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+} from "@/components/ui/select";
+import { ControllerRenderProps } from "react-hook-form";
+import { Job } from "@/models/job.model";
+import { useState } from "react";
+
+interface DatePickerProps {
+ field: ControllerRenderProps<Job, any>;
+ presets: boolean;
+}
+
+export function DatePicker({ field, presets }: DatePickerProps) {
+ const [isPopoverOpen, setIsPopoverOpen] = useState<boolean>(false);
+
+ return (
+ <Popover open={isPopoverOpen} onOpenChange={setIsPopoverOpen}>
+ <PopoverTrigger asChild>
+ <Button
+ variant={"outline"}
+ className={cn(
+ "w-[280px] justify-start text-left font-normal",
+ !field.value && "text-muted-foreground"
+ )}
+ >
+ <CalendarIcon className="mr-2 h-4 w-4" />
+ {field.value ? format(field.value, "PPP") : <span>Pick a date</span>}
+ </Button>
+ </PopoverTrigger>
+ <PopoverContent className="flex w-auto flex-col space-y-2 p-2">
+ {presets ? (
+ <Select
+ onValueChange={(value) => {
+ field.onChange(addDays(new Date(), parseInt(value)));
+ setIsPopoverOpen(false);
+ }}
+ >
+ <SelectTrigger>
+ <SelectValue placeholder="Select Preset" />
+ </SelectTrigger>
+ <SelectContent position="popper">
+ <SelectItem value="0">Today</SelectItem>
+ <SelectItem value="1">Tomorrow</SelectItem>
+ <SelectItem value="3">In 3 days</SelectItem>
+ <SelectItem value="7">In a week</SelectItem>
+ </SelectContent>
+ </Select>
+ ) : null}
+ <div className="rounded-md border">
+ <Calendar
+ mode="single"
+ selected={field.value}
+ onSelect={(value) => {
+ field.onChange(value);
+ setIsPopoverOpen(false);
+ }}
+ // disabled={(date) =>
+ // field.value > new Date() || field.value < new Date("1900-01-01")
+ // }
+ initialFocus
+ />
+ </div>
+ </PopoverContent>
+ </Popover>
+ );
+}
diff --git a/src/components/DatePickerWithPresets.tsx b/src/components/DatePickerWithPresets.tsx
deleted file mode 100644
index 203bb6e..0000000
--- a/src/components/DatePickerWithPresets.tsx
+++ /dev/null
@@ -1,62 +0,0 @@
-"use client";
-
-import * as React from "react";
-import { addDays, format } from "date-fns";
-import { Calendar as CalendarIcon } from "lucide-react";
-
-import { cn } from "@/lib/utils";
-import { Button } from "@/components/ui/button";
-import { Calendar } from "@/components/ui/calendar";
-import {
- Popover,
- PopoverContent,
- PopoverTrigger,
-} from "@/components/ui/popover";
-import {
- Select,
- SelectContent,
- SelectItem,
- SelectTrigger,
- SelectValue,
-} from "@/components/ui/select";
-
-export function DatePickerWithPresets() {
- const [date, setDate] = React.useState<Date>();
-
- return (
- <Popover>
- <PopoverTrigger asChild>
- <Button
- variant={"outline"}
- className={cn(
- "w-[280px] justify-start text-left font-normal",
- !date && "text-muted-foreground"
- )}
- >
- <CalendarIcon className="mr-2 h-4 w-4" />
- {date ? format(date, "PPP") : <span>Pick a date</span>}
- </Button>
- </PopoverTrigger>
- <PopoverContent className="flex w-auto flex-col space-y-2 p-2">
- <Select
- onValueChange={(value) =>
- setDate(addDays(new Date(), parseInt(value)))
- }
- >
- <SelectTrigger>
- <SelectValue placeholder="Select" />
- </SelectTrigger>
- <SelectContent position="popper">
- <SelectItem value="0">Today</SelectItem>
- <SelectItem value="1">Tomorrow</SelectItem>
- <SelectItem value="3">In 3 days</SelectItem>
- <SelectItem value="7">In a week</SelectItem>
- </SelectContent>
- </Select>
- <div className="rounded-md border">
- <Calendar mode="single" selected={date} onSelect={setDate} />
- </div>
- </PopoverContent>
- </Popover>
- );
-}