forked from SashenJayathilaka/Airbnb-Build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CategoryBox.tsx
58 lines (47 loc) · 1.32 KB
/
CategoryBox.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
"use client";
import { useRouter, useSearchParams } from "next/navigation";
import qs from "query-string";
import React, { useCallback } from "react";
import { IconType } from "react-icons";
type Props = {
icon: IconType;
label: string;
selected?: boolean;
};
function CategoryBox({ icon: Icon, label, selected }: Props) {
const router = useRouter();
const params = useSearchParams();
const handleClick = useCallback(() => {
let currentQuery = {};
if (params) {
currentQuery = qs.parse(params.toString());
}
const updatedQuery: any = {
...currentQuery,
category: label,
};
if (params?.get("category") === label) {
delete updatedQuery.category;
}
const url = qs.stringifyUrl(
{
url: "/",
query: updatedQuery,
},
{ skipNull: true }
);
router.push(url);
}, [label, params, router]);
return (
<div
onClick={handleClick}
className={`flex flex-col items-center justify-center gap-2 p-3 border-b-2 hover:text-neutral-800 transition cursor-pointer ${
selected ? "border-b-neutral-800" : "border-transparent"
} ${selected ? "text-neutral-800" : "text-neutral-500"}`}
>
<Icon size={26} />
<div className="font-medium text-xs">{label}</div>
</div>
);
}
export default CategoryBox;