|
1 | 1 | import MainSlider from "@/component/mainSlider"; |
2 | 2 | import HomeCategory from "@/component/homeCategory"; |
3 | 3 | import ProductCarousel from "@/component/productCarousel"; |
4 | | -import { HomePageAPI } from "@/const/endPoint"; |
| 4 | +import { HomePageAPI, MegaMenuAPI } from "@/const/endPoint"; |
5 | 5 | import Subscribe from "@/component/subscribe"; |
6 | 6 | import InstagramPost from "@/component/instagramPost"; |
7 | 7 | import { HomeProps, Product } from "@/utils/type"; |
8 | | -import { MegaMenuProvider } from "@/context/menuContext"; |
9 | 8 | import { HomeTransformer } from "@/utils/api/transformer/home"; |
10 | 9 | import { getData } from "@/utils/api/fetchData/apiCall"; |
11 | 10 | import { useScrollRestoration } from "@/utils/hooks"; |
12 | | -import { useQuery } from "react-query"; |
13 | | -import { useRouter } from "next/router"; |
| 11 | +import { useQuery } from "@tanstack/react-query"; |
14 | 12 | import { GetServerSidePropsContext } from "next"; |
15 | 13 | import MetaTags from "@/component/metaTags"; |
| 14 | +import { MegaMenuTransformer } from "@/utils/api/transformer/megaMenu"; |
16 | 15 |
|
17 | 16 | const fetchCHomeData = async () => { |
18 | 17 | const data = await getData(HomePageAPI); |
19 | 18 | return HomeTransformer(data).homeProductCarousel; |
20 | 19 | }; |
21 | 20 |
|
22 | | -export default function Home({ |
23 | | - homeProductCarousel, |
24 | | - menu, |
25 | | -}: HomeProps): JSX.Element { |
| 21 | +export default function Home({ homeProductCarousel }: HomeProps): JSX.Element { |
26 | 22 | useScrollRestoration(); |
27 | | - const router = useRouter(); |
28 | | - const locale = router.locale || "en"; |
29 | | - const { data: carouselData } = useQuery<Product[]>( |
30 | | - ["homePage"], |
31 | | - () => fetchCHomeData(), |
32 | 23 |
|
33 | | - { |
34 | | - initialData: homeProductCarousel || undefined, |
35 | | - enabled: !homeProductCarousel, |
36 | | - refetchOnMount: false, |
37 | | - } |
38 | | - ); |
| 24 | + const { data: carouselData } = useQuery<Product[]>({ |
| 25 | + queryKey: ["homePage"], |
| 26 | + queryFn: () => fetchCHomeData(), |
| 27 | + initialData: homeProductCarousel || undefined, |
| 28 | + enabled: !homeProductCarousel, |
| 29 | + refetchOnMount: false, |
| 30 | + }); |
39 | 31 |
|
40 | 32 | return ( |
41 | 33 | <> |
42 | | - <MegaMenuProvider initialMenu={menu} language={locale}> |
43 | | - <MetaTags /> |
44 | | - <MainSlider /> |
45 | | - <HomeCategory /> |
46 | | - <ProductCarousel product={carouselData} /> |
47 | | - <Subscribe /> |
48 | | - <InstagramPost /> |
49 | | - </MegaMenuProvider> |
| 34 | + <MetaTags /> |
| 35 | + <MainSlider /> |
| 36 | + <HomeCategory /> |
| 37 | + <ProductCarousel product={carouselData} /> |
| 38 | + <Subscribe /> |
| 39 | + <InstagramPost /> |
50 | 40 | </> |
51 | 41 | ); |
52 | 42 | } |
53 | 43 |
|
54 | 44 | export async function getServerSideProps(context: GetServerSidePropsContext) { |
55 | 45 | const referer = context.req.headers.referer || null; |
| 46 | + |
56 | 47 | if (!referer) { |
57 | 48 | const data = await getData(HomePageAPI); |
58 | 49 | const { homeProductCarousel } = HomeTransformer(data); |
59 | | - |
60 | | - return { props: { homeProductCarousel } }; |
| 50 | + const megaMenuData = await getData(MegaMenuAPI); |
| 51 | + const menu = MegaMenuTransformer(megaMenuData).menuItems; |
| 52 | + return { props: { homeProductCarousel, menu } }; |
61 | 53 | } |
62 | | - return { props: { homeProductCarousel: null } }; |
| 54 | + |
| 55 | + return { props: { homeProductCarousel: null, menu: [] } }; |
63 | 56 | } |
0 commit comments