Skip to content

Commit 9779944

Browse files
Merge pull request #20 from binshops/fix/apiCall
Fix/api call
2 parents 475aa6f + e43f4ab commit 9779944

File tree

7 files changed

+22
-42
lines changed

7 files changed

+22
-42
lines changed

src/component/languageSelector/languageSelector.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const LanguageSelector: FC = () => {
1111
const divRef = useRef<HTMLDivElement | null>(null);
1212

1313
const router = useRouter();
14-
14+
const locale = router.locale;
1515
const handleClickOutside = (event: MouseEvent) => {
1616
if (divRef.current && !divRef.current.contains(event.target as Node)) {
1717
setOpenLanguage(false);
@@ -22,12 +22,13 @@ const LanguageSelector: FC = () => {
2222
i18n.changeLanguage(lng);
2323
const { pathname, query } = router;
2424
router.push({ pathname, query }, undefined, { locale: lng });
25-
setOpenLanguage(!openLanguage);
25+
setOpenLanguage(false);
2626
queryClient.invalidateQueries();
2727
};
2828

2929
useEffect(() => {
3030
document.addEventListener("mousedown", handleClickOutside);
31+
locale && changeLanguage(locale);
3132
return () => {
3233
document.removeEventListener("mousedown", handleClickOutside);
3334
};

src/component/productDetails/index.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { useQuery } from "react-query";
33

44
import { FeaturedProductAPI } from "@/const/endPoint";
55
import { FeaturedProductTransformer } from "@/utils/api/transformer/featuredProduct";
6-
import { Product } from "@/utils/type";
76
import { getData } from "@/utils/api/fetchData/apiCall";
87

98
import ProductCarousel from "../productCarousel";
@@ -12,13 +11,12 @@ import ProductGallery from "../productGallery";
1211

1312
import { productDetailsProps } from "./productDetails.types";
1413
import styles from "./productDetails.module.scss";
15-
import { useRouter } from "next/router";
1614

1715
const ProductDetails: FC<productDetailsProps> = ({ product }) => {
1816
const { data: featuredProduct = [] } = useQuery({
1917
queryKey: ["featuredProduct"],
2018
queryFn: async () => {
21-
const productData = await getData(FeaturedProductAPI, {}, "", "");
19+
const productData = await getData(FeaturedProductAPI);
2220
return FeaturedProductTransformer(productData);
2321
},
2422
enabled: !!product.id,

src/pages/category/[slug].tsx

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,12 @@ const fetchCategoryData = async (
2323
filterQuery?: string,
2424
orderQuery?: string
2525
) => {
26-
const categoryData = await getData(
27-
CategoryAPI,
28-
{
29-
id_category: categoryId,
30-
page,
31-
q: filterQuery,
32-
order: orderQuery,
33-
},
34-
"",
35-
"",
36-
locale
37-
);
26+
const categoryData = await getData(CategoryAPI, {
27+
id_category: categoryId,
28+
page,
29+
q: filterQuery,
30+
order: orderQuery,
31+
});
3832
return CategoryTransformer(categoryData);
3933
};
4034

@@ -109,16 +103,13 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
109103
const categoryId = context.query.slug;
110104
const page = context.query.page;
111105
const referer = context.req.headers.referer || null;
112-
const menuData = await getData(MegaMenuAPI, {}, "", "", locale);
106+
const menuData = await getData(MegaMenuAPI);
113107
const menu = MegaMenuTransformer(menuData).menuItems;
114108
if (!referer) {
115-
const categoryData = await getData(
116-
CategoryAPI,
117-
{ id_category: categoryId, page },
118-
"",
119-
"",
120-
locale
121-
);
109+
const categoryData = await getData(CategoryAPI, {
110+
id_category: categoryId,
111+
page,
112+
});
122113
const data = CategoryTransformer(categoryData);
123114

124115
return {

src/pages/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ export default function Home({
5555
export async function getServerSideProps(context: GetServerSidePropsContext) {
5656
const referer = context.req.headers.referer || null;
5757
const locale = context.locale;
58-
const menuData = await getData(MegaMenuAPI, {}, "", "", locale);
58+
const menuData = await getData(MegaMenuAPI);
5959
const menu = MegaMenuTransformer(menuData).menuItems;
6060
if (!referer) {
61-
const data = await getData(HomePageAPI, {}, "", "", locale);
61+
const data = await getData(HomePageAPI);
6262
const { homeProductCarousel } = HomeTransformer(data);
6363

6464
return { props: { homeProductCarousel, menu } };

src/pages/product/[slug].tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,11 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
3737
const locale = context.locale;
3838
const productId = context.query.slug;
3939
const referer = context.req.headers.referer || null;
40-
const menuData = await getData(MegaMenuAPI, {}, "", "", locale);
40+
const menuData = await getData(MegaMenuAPI);
4141
const menu = MegaMenuTransformer(menuData).menuItems;
4242
if (!referer) {
4343
const productData =
44-
productId &&
45-
(await getData(
46-
ProductDetailAPI,
47-
{ product_id: productId },
48-
"",
49-
"",
50-
locale
51-
));
44+
productId && (await getData(ProductDetailAPI, { product_id: productId }));
5245
const data = ProductTransformer(productData);
5346
return { props: { initialProduct: data, productId, menu } };
5447
}

src/utils/api/fetchData/apiCall.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ export async function getData(
55
endPoint: string,
66
queryParams?: any,
77
extraParam?: string,
8-
body?: any,
9-
locale?: string
8+
body?: any
109
): Promise<any> {
11-
const currentLanguage = locale || i18n.language;
10+
const currentLanguage = i18n.language;
1211
let url = `/${currentLanguage}/rest${endPoint}`;
1312

1413
if (queryParams && Object.keys(queryParams).length > 0) {

src/utils/hooks/api/useFetchProductData.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ const fetchProductData = async ({
2626
const productData = await getData(
2727
ProductDetailAPI,
2828
{ product_id: productId, refresh },
29-
queryString,
30-
"",
31-
locale
29+
queryString
3230
);
3331

3432
return ProductTransformer(productData);

0 commit comments

Comments
 (0)