diff options
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/api/biteShip.js | 30 | ||||
| -rw-r--r-- | src/core/api/odooApi.js | 36 | ||||
| -rw-r--r-- | src/core/components/elements/Navbar/NavbarMobile.jsx | 4 | ||||
| -rw-r--r-- | src/core/components/elements/Navbar/TopBannerMobile.jsx | 72 |
4 files changed, 130 insertions, 12 deletions
diff --git a/src/core/api/biteShip.js b/src/core/api/biteShip.js new file mode 100644 index 00000000..f18421d8 --- /dev/null +++ b/src/core/api/biteShip.js @@ -0,0 +1,30 @@ +import axios from 'axios'; + +const biteShipAPI = async (method, url, body = {}) => { + try { + const key = process.env.NEXT_PUBLIC_BITSEHIP_KEY; + const baseUrl = process.env.NEXT_PUBLIC_BITE_SHIP_HOST; + + const axiosParameter = { + method, + url: baseUrl + url, + headers: { + Authorization: `Bearer ${key}`, // Tambahkan "Bearer " di depan key + 'Content-Type': 'application/json', + }, + data: body, // Tidak perlu JSON.stringify + }; + + const data = await axios(axiosParameter); + + return { success: true, data: data }; + } catch (error) { + console.log(error); + return { + success: false, + data: {}, + }; + } +}; + +export default biteShipAPI; diff --git a/src/core/api/odooApi.js b/src/core/api/odooApi.js index 504d097a..ab3dedb0 100644 --- a/src/core/api/odooApi.js +++ b/src/core/api/odooApi.js @@ -42,17 +42,30 @@ const odooApi = async (method, url, data = {}, headers = {}) => { url: process.env.NEXT_PUBLIC_ODOO_API_HOST + url, headers: { Authorization: token, ...headers }, }; - if (auth) axiosParameter.headers['Token'] = auth.token; - if (method.toUpperCase() == 'POST') - axiosParameter.headers['Content-Type'] = - 'application/x-www-form-urlencoded'; - if (Object.keys(data).length > 0) - axiosParameter.data = new URLSearchParams( - Object.entries(data) - ).toString(); + + if (auth) { + axiosParameter.headers['Token'] = auth.token; + } + + // Tentukan format data berdasarkan metode dan data + if (Object.keys(data).length > 0) { + if (method.toUpperCase() === 'POST') { + // Gunakan URL-encoded untuk POST + axiosParameter.data = new URLSearchParams( + Object.entries(data) + ).toString(); + axiosParameter.headers['Content-Type'] = + 'application/x-www-form-urlencoded'; + } else { + // Gunakan JSON untuk GET/PUT atau metode lainnya + axiosParameter.data = data; + axiosParameter.headers['Content-Type'] = 'application/json'; + } + } let res = await axios(axiosParameter); - if (res.data.status.code == 401) { + + if (res.data?.status?.code === 401) { if (connectionAttempt < maxConnectionAttempt) { await renewToken(); return odooApi(method, url, data, headers); @@ -62,10 +75,13 @@ const odooApi = async (method, url, data = {}, headers = {}) => { return false; } } + return camelcaseObjectDeep(res.data.result) || []; } catch (error) { - // console.log(error); + console.error('API Error:', error); + throw error; // Opsional, lempar error agar bisa ditangkap di level atas } }; + export default odooApi; diff --git a/src/core/components/elements/Navbar/NavbarMobile.jsx b/src/core/components/elements/Navbar/NavbarMobile.jsx index 47182a47..7c148440 100644 --- a/src/core/components/elements/Navbar/NavbarMobile.jsx +++ b/src/core/components/elements/Navbar/NavbarMobile.jsx @@ -11,7 +11,7 @@ import Image from 'next/image'; import { useEffect, useState } from 'react'; import MobileView from '../../views/MobileView'; import Link from '../Link/Link'; -import TopBanner from './TopBanner'; +import TopBannerMobile from './TopBannerMobile'; import { useCartStore } from '~/modules/cart/stores/useCartStore'; import useAuth from '@/core/hooks/useAuth'; @@ -53,7 +53,7 @@ const NavbarMobile = () => { return ( <MobileView> - <TopBanner /> + <TopBannerMobile /> <nav className='px-4 py-2 pb-3 sticky top-0 z-50 bg-white shadow'> <div className='flex justify-between items-center mb-2'> <Link href='/'> diff --git a/src/core/components/elements/Navbar/TopBannerMobile.jsx b/src/core/components/elements/Navbar/TopBannerMobile.jsx new file mode 100644 index 00000000..c3f42f85 --- /dev/null +++ b/src/core/components/elements/Navbar/TopBannerMobile.jsx @@ -0,0 +1,72 @@ +import Image from 'next/image'; +import { useQuery } from 'react-query'; +import useDevice from '@/core/hooks/useDevice'; +import odooApi from '@/core/api/odooApi'; +import SmoothRender from '~/components/ui/smooth-render'; +import Link from '../Link/Link'; +import { background } from '@chakra-ui/react'; +import { useEffect, useState } from 'react'; + +const TopBannerMobile = ({ onLoad = () => {} }) => { + const [topBannerMobile, setTopBannerMobile] = useState([]); + const { isDesktop, isMobile } = useDevice(); + + useEffect(() => { + const fetchData = async () => { + const res = await fetch(`/api/hero-banner?type=top-banner-mobile`); + const { data } = await res.json(); + if (data) { + setTopBannerMobile(data); + } + }; + + fetchData(); + }, []); + + // const topBannerMobile = useQuery({ + // queryKey: 'topBannerMobile', + // queryFn: async () => await odooApi('GET', '/api/v1/banner?type=top-banner'), + // refetchOnWindowFocus: false, + // }); + + // const backgroundColor = topBannerMobile.data?.[0]?.backgroundColor || 'transparent'; + const hasData = topBannerMobile?.length > 0; + const data = topBannerMobile?.[0] || null; + + useEffect(() => { + if (hasData) { + onLoad(); + } + }, [hasData, onLoad]); + + + if (!hasData || !data?.image) { + return null; + } + return ( + <SmoothRender + isLoaded={hasData} + duration='700ms' + delay='300ms' + className='h-auto' + > + <Link + href={data?.url} + aria-label='panduan pick up barang' + className='block w-full' + > + <Image + src={data?.image} + alt='Panduan Pick Up Barang' + width={0} + height={0} + sizes='100vw' + className='w-full h-auto' + priority + /> + </Link> + </SmoothRender> + ); +}; + +export default TopBannerMobile; |
