diff options
| author | it-fixcomart <it@fixcomart.co.id> | 2024-10-24 11:01:08 +0700 |
|---|---|---|
| committer | it-fixcomart <it@fixcomart.co.id> | 2024-10-24 11:01:08 +0700 |
| commit | 9abd621285d739a9c502d661013db5ce96edec33 (patch) | |
| tree | 98ff4ae0bf8adcced77699de33aebe50616233dc /src/lib/product/components/Product/ProductDesktopVariant.jsx | |
| parent | ca30c28dd0b19977eb771fc32ff5e520cdef1068 (diff) | |
| parent | e0aa9e04a473a4f7a21b389b42314d3fed06b43e (diff) | |
Merge branch 'new-release' into CR/new_product_detail
Diffstat (limited to 'src/lib/product/components/Product/ProductDesktopVariant.jsx')
| -rw-r--r-- | src/lib/product/components/Product/ProductDesktopVariant.jsx | 41 |
1 files changed, 33 insertions, 8 deletions
diff --git a/src/lib/product/components/Product/ProductDesktopVariant.jsx b/src/lib/product/components/Product/ProductDesktopVariant.jsx index 55effdfb..86b58e23 100644 --- a/src/lib/product/components/Product/ProductDesktopVariant.jsx +++ b/src/lib/product/components/Product/ProductDesktopVariant.jsx @@ -17,6 +17,7 @@ import { updateItemCart } from '@/core/utils/cart'; import currencyFormat from '@/core/utils/currencyFormat'; import { createSlug } from '@/core/utils/slug'; import whatsappUrl from '@/core/utils/whatsappUrl'; +import { getAuth } from '~/libs/auth'; import { RWebShare } from 'react-web-share'; import productSimilarApi from '../../api/productSimilarApi'; @@ -33,11 +34,9 @@ const ProductDesktopVariant = ({ isVariant, }) => { const router = useRouter(); - const auth = useAuth(); + let auth = useAuth(); const { slug } = router.query; - - console.log('ini product variant', product); - + const { srsltid } = router.query; const [lowestPrice, setLowestPrice] = useState(null); const [addCartAlert, setAddCartAlert] = useState(false); @@ -88,7 +87,7 @@ const ProductDesktopVariant = ({ const handleAddToCart = (variant) => { if (!auth) { - router.push(`/login?next=/shop/product/${slug}`); + router.push(`/login?next=/shop/product/${slug}?srsltid=${srsltid}`); return; } const quantity = quantityInput; @@ -105,8 +104,34 @@ const ProductDesktopVariant = ({ setAddCartAlert(true); }; - const handleBuy = (variant) => { - const quantity = quantityInput; + const handleBuy = async (variant) => { + const quantity = variantQuantityRefs?.current[product.id]?.value; + let isLoggedIn = typeof auth === 'object'; + + if (!isLoggedIn) { + const currentUrl = encodeURIComponent(router.asPath); + await router.push(`/login?next=${currentUrl}`); + + // Tunggu login berhasil, misalnya dengan memantau perubahan status auth. + const authCheckInterval = setInterval(() => { + const newAuth = getAuth(); + if (typeof newAuth === 'object') { + isLoggedIn = true; + auth = newAuth; // Update nilai auth setelah login + clearInterval(authCheckInterval); + } + }, 500); // Periksa status login setiap 500ms + + await new Promise((resolve) => { + const checkLogin = setInterval(() => { + if (isLoggedIn) { + clearInterval(checkLogin); + resolve(null); + } + }, 500); + }); + } + if (!validQuantity(quantity)) return; updateItemCart({ @@ -255,7 +280,7 @@ const ProductDesktopVariant = ({ </div> </div> - <div className='p-4 md:p-6 '> + <div className='p-4 md:p-6 w-full'> <ProductPromoSection product={product} productId={product.id} /> <div className='p-4 md:p-6 md:bg-gray-50 rounded-xl'> |
