Skip to content

Commit ea980e4

Browse files
committed
add categories
1 parent ce38558 commit ea980e4

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/hooks/Queries/shop/useSwagList.jsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,22 @@ const useSingleSwag = (id) =>
3737
refetchOnWindowFocus: false,
3838
});
3939

40-
export { useSingleSwag, useSwagList };
40+
const fetchSwagCategories = async () => {
41+
try {
42+
const response = await publicAxios.get("/product-categories");
43+
return response.data;
44+
} catch (error) {
45+
console.log(error);
46+
toast.error("Error fetching swag list");
47+
throw error;
48+
}
49+
};
50+
51+
const useSwagCategory = () =>
52+
useQuery({
53+
queryKey: ["swagCategory"],
54+
queryFn: () => fetchSwagCategories(),
55+
refetchOnWindowFocus: false,
56+
});
57+
58+
export { useSingleSwag, useSwagList, useSwagCategory };

src/pages/shop/sections/FilterSection.jsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Link, useParams } from "react-router-dom";
33
import SearchInput from "../../../components/shop/SearchInput";
44
import SectionWrapper from "../../../components/shop/SectionWrapper";
55
import SortItems from "../../../components/shop/SortItems";
6-
import { categories } from "../data";
6+
import { useSwagCategory } from "@/hooks/Queries/shop/useSwagList";
77

88
function FilterSection() {
99
return (
@@ -27,22 +27,28 @@ function FilterSection() {
2727
}
2828

2929
function Categories() {
30+
const { data, isSuccess } = useSwagCategory();
3031
return (
3132
<div className="flex gap-4 flex-wrap ">
32-
{categories.map((item) => (
33-
<CategoryItem key={crypto.randomUUID()} name={item.name} />
34-
))}
33+
{isSuccess &&
34+
data?.map((item) => (
35+
<CategoryItem
36+
key={crypto.randomUUID()}
37+
name={item.name}
38+
slug={item.slug}
39+
/>
40+
))}
3541
</div>
3642
);
3743
}
3844

39-
function CategoryItem({ name }) {
45+
function CategoryItem({ name, slug }) {
4046
const params = useParams();
41-
const category = params.category === name;
47+
const category = params.category === slug;
4248

4349
return (
4450
<Link
45-
to={`/shop/category/${name}`}
51+
to={`/shop/category/${slug}`}
4652
className={`rounded-3xl flex items-center border shadow-sm py-2 px-4 ${category ? "bg-green-dark" : " hover:bg-gray-500/20 "}`}
4753
>
4854
<p

0 commit comments

Comments
 (0)