Skip to content

Commit 2b48fb6

Browse files
Merge pull request #15 from binshops/fix/routing
Fix/routing
2 parents 9aa1bd5 + e4d9686 commit 2b48fb6

File tree

21 files changed

+549
-217
lines changed

21 files changed

+549
-217
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"react-dom": "^18",
2222
"react-hot-toast": "^2.4.1",
2323
"react-i18next": "^15.1.4",
24+
"react-query": "^3.39.3",
2425
"sass": "^1.76.0",
2526
"scss": "^0.2.4",
2627
"swiper": "^11.1.1",

src/component/category/categoryOptions/categoryOptions.types.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ export type CategoryOptionsProps = {
44
filters: Filter[];
55
sortOptions: Sort[];
66
count: number;
7-
setCategory: (value: any) => void;
8-
categoryId: string;
9-
setIsLoading: (value: boolean) => void;
10-
activeSort: string;
11-
activeFilter: string;
7+
setFilterQuery: (value: string) => void;
8+
setOrderQuery: (value: string) => void;
129
};

src/component/category/categoryOptions/index.tsx

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
import React, { FC, useEffect, useState } from "react";
1+
import React, { FC, useState } from "react";
22
import { useTranslation } from "react-i18next";
33

4-
import { CategoryTransformer } from "@/utils/api/transformer/category";
5-
import { getData } from "@/utils/api/fetchData/apiCall";
6-
import { CategoryAPI } from "@/const/endPoint";
7-
84
import { CategoryOptionsProps } from "./categoryOptions.types";
95
import Sort from "../sort";
106
import Filter from "../filter";
@@ -15,40 +11,13 @@ const CategoryOptions: FC<CategoryOptionsProps> = ({
1511
filters,
1612
sortOptions,
1713
count,
18-
setCategory,
19-
categoryId,
20-
setIsLoading,
21-
activeSort,
22-
activeFilter,
14+
setFilterQuery,
15+
setOrderQuery,
2316
}) => {
24-
const [filterQuery, setFilterQuery] = useState<string | undefined>();
25-
const [orderQuery, setOrderQuery] = useState<string | undefined>();
2617
const [isOpenFilter, setIsOpenFilter] = useState(false);
2718
const [showSortOption, setShowSortOption] = useState(false);
2819
const { t } = useTranslation();
2920

30-
useEffect(() => {
31-
const fetchData = async () => {
32-
setIsLoading(true);
33-
try {
34-
const productData = await getData(CategoryAPI, {
35-
id_category: categoryId,
36-
page: 1,
37-
q: filterQuery || activeFilter.replace("+", " "),
38-
order: orderQuery || activeSort,
39-
});
40-
const transformedData = CategoryTransformer(productData);
41-
setCategory(transformedData);
42-
setIsLoading(false);
43-
} catch (error) {
44-
console.error("Failed to fetch product data:", error);
45-
}
46-
};
47-
(orderQuery || filterQuery) && fetchData();
48-
setShowSortOption(false);
49-
setIsOpenFilter(false);
50-
}, [orderQuery, filterQuery]);
51-
5221
return (
5322
<div className={styles.optionsWrapper}>
5423
<Filter

src/component/megaMenu/index.tsx

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,44 @@
11
import React from "react";
2-
import Link from "next/link";
32
import { useRouter } from "next/router";
4-
5-
import { useMegaMenu } from "@/context/menuContext";
6-
73
import styles from "./megaMenu.module.scss";
4+
import { useMegaMenu } from "@/context/menuContext";
85

96
const MegaMenu: React.FC = () => {
10-
const megaMenuContext = useMegaMenu();
7+
const menu = useMegaMenu();
118
const router = useRouter();
129

13-
const menu = megaMenuContext?.menu;
14-
const handelSubmenuClick = (url: string) => {
10+
const handleSubmenuClick = (url: string) => {
1511
router.push(url);
1612
};
13+
1714
return (
1815
<div className={styles.megaMenu}>
1916
{menu?.map((item, idx) => (
20-
<Link href={item.link} className={styles.menuItem} key={idx} passHref>
17+
<div
18+
className={styles.menuItem}
19+
key={idx}
20+
onClick={() => handleSubmenuClick(item.link)}
21+
>
2122
<p className={styles.megaMenuItem}>{item.label}</p>
2223
{Boolean(item.children.length) && (
2324
<div className={styles.subMenu}>
2425
<div className={`${styles.linkBox} container`}>
25-
{item.children?.map((subLink, idx) => {
26-
return (
27-
<p
28-
onClick={() => handelSubmenuClick(subLink.link)}
29-
className={styles.link}
30-
key={idx}
31-
>
32-
{subLink.title}
33-
</p>
34-
);
35-
})}
26+
{item.children?.map((subLink, idx) => (
27+
<p
28+
onClick={(e) => {
29+
e.stopPropagation();
30+
handleSubmenuClick(subLink.link);
31+
}}
32+
className={styles.link}
33+
key={idx}
34+
>
35+
{subLink.title}
36+
</p>
37+
))}
3638
</div>
3739
</div>
3840
)}
39-
</Link>
41+
</div>
4042
))}
4143
</div>
4244
);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Product } from "@/utils/type";
22

33
export interface ProductCarouselProps {
4-
product: Product[];
4+
product?: Product[];
55
}

src/component/productDetails/index.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { productDetailsProps } from "./productDetails.types";
1414

1515
import styles from "./productDetails.module.scss";
1616

17-
const ProductDetails: FC<productDetailsProps> = ({ product, setProduct }) => {
17+
const ProductDetails: FC<productDetailsProps> = ({ product }) => {
1818
const [featuredProduct, setFeaturedProduct] = useState<Product[]>([]);
1919
useEffect(() => {
2020
const fetchData = async () => {
@@ -40,7 +40,6 @@ const ProductDetails: FC<productDetailsProps> = ({ product, setProduct }) => {
4040
price={product.price}
4141
options={product.options}
4242
description={product.description}
43-
setProduct={setProduct}
4443
productAttributeId={product.productAttributeId}
4544
/>
4645
</div>

src/component/productDetails/productDetails.types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ import { ProductType } from "@/utils/type";
22

33
export interface productDetailsProps {
44
product: ProductType;
5-
setProduct: Function;
65
}

src/component/productInfo/index.tsx

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import React, { FC, useEffect, useState } from "react";
2-
3-
import { ProductTransformer } from "@/utils/api/transformer/product";
4-
import { getData } from "@/utils/api/fetchData/apiCall";
5-
6-
import { ProductDetailAPI } from "@/const/endPoint";
1+
import React, { FC, useState } from "react";
72

83
import { useCart } from "@/context/cartContext";
94

@@ -14,21 +9,32 @@ import Price from "../product/price";
149
import { productInfoProps } from "./productInfo.types";
1510

1611
import styles from "./productInfo.module.scss";
12+
import { useFetchProductData } from "@/utils/hooks/api/useFetchProductData";
1713

1814
const ProductInfo: FC<productInfoProps> = ({
1915
id,
2016
title,
2117
price,
2218
options,
2319
description,
24-
setProduct,
2520
productAttributeId,
2621
}) => {
2722
const [selectedOption, setSelectedOption] = useState<
2823
{ id: string; value: string }[]
2924
>([]);
3025
const { addToCart, isLoading } = useCart();
3126
const [quantity, setQuantity] = useState(1);
27+
28+
const {
29+
data: productData,
30+
isFetching,
31+
isError,
32+
} = useFetchProductData({
33+
productId: id,
34+
selectedOption,
35+
refresh: true,
36+
});
37+
3238
const handleSelectOption = (id: string, value: string) => {
3339
const index = selectedOption.findIndex((option) => option.id === id);
3440
if (index > -1) {
@@ -39,30 +45,7 @@ const ProductInfo: FC<productInfoProps> = ({
3945
setSelectedOption([...selectedOption, { id, value }]);
4046
}
4147
};
42-
useEffect(() => {
43-
const fetchData = async () => {
44-
const resultString =
45-
"&" +
46-
selectedOption
47-
.map((item) => `group[${item.id}]=${item.value}`)
48-
.join("&");
49-
try {
50-
const productData = await getData(
51-
ProductDetailAPI,
52-
{
53-
product_id: id,
54-
refresh: true,
55-
},
56-
resultString
57-
);
58-
const transformedData = ProductTransformer(productData);
59-
setProduct(transformedData);
60-
} catch (error) {
61-
console.error("Failed to fetch product data:", error);
62-
}
63-
};
64-
selectedOption.length > 0 && fetchData();
65-
}, [selectedOption]);
48+
6649
const handleAdd = async () => {
6750
const item = {
6851
id,
@@ -72,10 +55,11 @@ const ProductInfo: FC<productInfoProps> = ({
7255
};
7356
addToCart(item);
7457
};
58+
7559
return (
7660
<div className={styles.productInfo}>
77-
<h2 className={styles.productTitle}>{title}</h2>
78-
<Price price={price} />
61+
<h2 className={styles.productTitle}>{productData?.title || title}</h2>
62+
<Price price={productData?.price || price} />
7963
{options && (
8064
<Options options={options} handleSelectOption={handleSelectOption} />
8165
)}
@@ -86,7 +70,9 @@ const ProductInfo: FC<productInfoProps> = ({
8670
isLoading={isLoading}
8771
/>
8872
<div
89-
dangerouslySetInnerHTML={{ __html: description }}
73+
dangerouslySetInnerHTML={{
74+
__html: productData?.description || description,
75+
}}
9076
className={styles.description}
9177
/>
9278
</div>

src/component/productInfo/productInfo.types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@ export interface productInfoProps {
66
price: string;
77
options: ProductOptions[];
88
description: string;
9-
setProduct: Function;
109
productAttributeId: number;
1110
}

src/const/queryClient.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { QueryClient } from "react-query";
2+
3+
export const queryClient = new QueryClient({
4+
defaultOptions: {
5+
queries: {
6+
staleTime: 5 * 60 * 1000,
7+
cacheTime: 30 * 60 * 1000,
8+
retry: 3,
9+
retryDelay: (attempt: any) => Math.min(1000 * 2 ** attempt, 30000),
10+
refetchOnWindowFocus: true,
11+
refetchOnReconnect: true,
12+
refetchOnMount: false,
13+
},
14+
mutations: {
15+
retry: 1,
16+
},
17+
},
18+
});

0 commit comments

Comments
 (0)