From 8c0b9568e8204a3797fd8064235f15b9ab2e711a Mon Sep 17 00:00:00 2001 From: Vassshhh Date: Tue, 5 Aug 2025 23:46:17 +0700 Subject: [PATCH] ok --- src/components/Login.js | 2 +- src/components/ProductDetailPage.js | 69 +++++++++++++++------------- src/components/pages/ProductsPage.js | 56 +++++++++++----------- 3 files changed, 67 insertions(+), 60 deletions(-) diff --git a/src/components/Login.js b/src/components/Login.js index 3607637..34940c3 100644 --- a/src/components/Login.js +++ b/src/components/Login.js @@ -99,7 +99,7 @@ const LoginRegister = ({postLoginAction, setPostLoginAction}) => { postLoginAction(); // resume action (e.g., checkout) setPostLoginAction(null); } - window.location.reload() + // window.location.reload() } else { alert('Token tidak ditemukan pada respons login'); } diff --git a/src/components/ProductDetailPage.js b/src/components/ProductDetailPage.js index acd564c..e1194fc 100644 --- a/src/components/ProductDetailPage.js +++ b/src/components/ProductDetailPage.js @@ -33,49 +33,54 @@ const ProductDetail = ({ subscriptions, product, setPostLoginAction, setShowedMo const token = tokenCookie ? tokenCookie.split('=')[1] : ''; if (!token) { - setPostLoginAction(() => () => onCheckout()); + setPostLoginAction(() => () => setShowedModal('product')); setShowedModal('login'); return; } if (product.type == 'product') { - - const hasMatchingSubscription = Array.isArray(subscriptions) && - subscriptions.some(sub => - sub.product_name?.toLowerCase().includes(product.name.toLowerCase()) - ); + subscriptions.some(sub => + sub.product_id === product.id || sub.product_parent_id === product.id + ); +// Always show children selector first if product has children +if (product.children && product.children.length > 0) { + setShowChildSelector(true); - // Always show children selector first if product has children - if (product.children && product.children.length > 0) { - setShowChildSelector(true); + if (hasMatchingSubscription) { + const matching = subscriptions.filter(sub => + sub.product_id === product.id || sub.product_parent_id === product.id + ); - if (hasMatchingSubscription) { - const matching = subscriptions.filter(sub => - sub.product_name?.toLowerCase().includes(product.name.toLowerCase()) - ); - const uniqueByName = Array.from(new Map(matching.map(sub => [sub.product_name, sub])).values()); + if (matching.length > 0) { + // ✅ Select only the first for each product_name + const uniqueByName = Array.from( + new Map(matching.map(sub => [sub.product_name, sub])).values() + ); - if (uniqueByName.length > 0) { - setMatchingSubscriptions(uniqueByName); - } - } - return; - } + setMatchingSubscriptions(uniqueByName); + } + } + return; +} - // No children, but has subscription match - if (hasMatchingSubscription) { - const matching = subscriptions.filter(sub => - sub.product_name?.toLowerCase().includes(product.name.toLowerCase()) - ); - const uniqueByName = Array.from(new Map(matching.map(sub => [sub.product_name, sub])).values()); +// No children, but has subscription match +if (hasMatchingSubscription) { + const matching = subscriptions.filter(sub => + sub.product_id === product.id || sub.product_parent_id === product.id + ); + + if (matching.length > 0) { + const uniqueByName = Array.from( + new Map(matching.map(sub => [sub.product_name, sub])).values() + ); + + setMatchingSubscriptions(uniqueByName); + setShowSubscriptionSelector(true); + return; + } +} - if (uniqueByName.length > 0) { - setMatchingSubscriptions(uniqueByName); - setShowSubscriptionSelector(true); - return; - } - } } // No children, no matching subscription diff --git a/src/components/pages/ProductsPage.js b/src/components/pages/ProductsPage.js index 533ac2f..dad2a27 100644 --- a/src/components/pages/ProductsPage.js +++ b/src/components/pages/ProductsPage.js @@ -62,39 +62,41 @@ const CoursePage = ({ subscriptions }) => { headers: { 'Content-Type': 'application/json', }, - body: JSON.stringify({ itemsId: productIds, type: 'product' }), + body: JSON.stringify({ itemsId: productIds }), }) .then(res => res.json()) .then(data => { const enrichedData = Object.values(groupedSubs) - .filter(group => data.some(p => p.id === group.product_id)) // ✅ hanya produk yang ada di metadata - .map(group => { - const productData = data.find(p => p.id == group.product_id); + .filter(group => data.some(p => p.id === group.product_id)) // ✅ hanya produk yang ada di metadata + .map(group => { + const productData = data.find(p => p.id == group.product_id); - // Cek fallback image dari parent jika image kosong dan sub_product_of ada - let image = productData?.image || ''; - if (!image && productData?.sub_product_of) { - const parent = data.find(p => p.id === productData.sub_product_of); - image = parent?.image || ''; - } + // Cek fallback image dari parent jika image kosong dan sub_product_of ada + let image = productData?.image || ''; + let description = productData?.description || ''; + if (!image && productData?.sub_product_of) { + const parent = data.find(p => p.id === productData.sub_product_of); + image = parent?.image || ''; + description = parent?.description || ''; + } - return { - id: group.product_id, - name: group.product_name, - type: productData?.type || 'product', - image: image, - description: productData?.description || '', - price: productData?.price || 0, - currency: productData?.currency || 'IDR', - duration: productData?.duration || {}, - sub_product_of: productData?.sub_product_of || null, - is_visible: productData?.is_visible ?? true, - unit_type: productData?.unit_type || group.unit_type, - quantity: group.quantity, - end_date: group.end_date, - children: [] - }; - }); + return { + id: group.product_id, + name: group.product_name, + type: productData?.type || 'product', + image: image, + description: description, + price: productData?.price || 0, + currency: productData?.currency || 'IDR', + duration: productData?.duration || {}, + sub_product_of: productData?.sub_product_of || null, + is_visible: productData?.is_visible ?? true, + unit_type: productData?.unit_type || group.unit_type, + quantity: group.quantity, + end_date: group.end_date, + children: [] + }; + }); console.log(enrichedData) setProducts(enrichedData);