-
Notifications
You must be signed in to change notification settings - Fork 0
/
JobFilterSideBar.tsx
114 lines (108 loc) · 3.35 KB
/
JobFilterSideBar.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { JobFilterSchema, JobFilterValue } from "@/lib/validation";
import { Input } from "./ui/input";
import { Label } from "./ui/label";
import Select from "./ui/select";
import prisma from "@/lib/prisma";
import { redirect } from "next/navigation";
import { Button } from "./ui/button";
import FormSubmitButton from "./FormSubmitButton";
import { jobTypes } from "@/lib/job-types";
async function filterJobs(formData: FormData) {
"use server";
const values = Object.fromEntries(formData.entries());
const { q, type, location, remote } = JobFilterSchema.parse(values);
const searchParams = new URLSearchParams({
...(q && { q: q.trim() }),
...(type && { type }),
...(location && { location }),
...(remote && { remote: "true" }),
});
redirect(`?${searchParams.toString()}`);
}
interface JobFilterSidebarProps {
defaultValues: JobFilterValue;
}
export default async function JobFilterSideBar({
defaultValues,
}: JobFilterSidebarProps) {
interface JobFilterSidebarProps {
defaultValues: JobFilterValue;
}
const distinctLocation = (await prisma.jOB
.findMany({
where: {
approved: true,
},
select: {
location: true,
},
distinct: ["location"],
})
.then((locations) =>
locations
.map((location) => {
location;
return location.location;
})
.filter(Boolean),
)) as string[];
console.log(distinctLocation);
return (
<aside className="sticky top-0 h-fit rounded-lg border bg-background p-4 md:w-[260px]">
<form action={filterJobs} key={JSON.stringify(defaultValues)}>
<div className="space-y-4">
<div className="flex flex-col gap-2">
<Label htmlFor="q">Search</Label>
<Input
id="q"
name="q"
placeholder="Title, company, etc."
defaultValue={defaultValues.q || ""}
/>
</div>
<div className="flex flex-col gap-2">
<Label htmlFor="type">Type</Label>
<Select
id="type"
name="type"
defaultValue={defaultValues.type || ""}
>
<option value="">All types</option>
{jobTypes.map((type) => (
<option key={type} value={type}>
{type}
</option>
))}
</Select>
</div>
<div className="flex flex-col gap-2">
<Label htmlFor="location">Location</Label>
<Select
id="location"
name="location"
defaultValue={defaultValues.location || ""}
>
<option value="">All locations</option>
{distinctLocation.map((location) => (
<option key={location} value={location}>
{location}
</option>
))}
</Select>
</div>
<div className="flex items-center gap-2">
<input
id="remote"
name="remote"
type="checkbox"
className="scale-125 accent-black"
defaultChecked={defaultValues.remote}
/>
<Label htmlFor="remote">Remote jobs</Label>
</div>
<Button type="submit" className="w-full">Filter jobs</Button>
</div>
</form>
</aside>
);
}