From a4d6f9ae43bc0baf829722a33f86eff6712d8bf2 Mon Sep 17 00:00:00 2001 From: Vassshhh Date: Sat, 23 Aug 2025 14:59:08 +0700 Subject: [PATCH] ok --- src/App.js | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/src/App.js b/src/App.js index 29396d6..056a8d7 100644 --- a/src/App.js +++ b/src/App.js @@ -194,31 +194,42 @@ function App() { if (!productModalRequest || !subscriptions) return; const { productId, authorizedUri, unauthorizedUri } = productModalRequest; - console.log(subscriptions) + const hasAccess = subscriptions && subscriptions.some( sub => sub.product_id === productId || sub.product_parent_id === productId ); - console.log(hasAccess) + console.log("hasAccess:", hasAccess); + if (hasAccess) { if (authorizedUri) { let finalUri = decodeURIComponent(authorizedUri); const token = document.cookie.match(/(^| )token=([^;]+)/)?.[2]; - if (finalUri.includes('token=null') || finalUri.includes('token=')) { + // --- ambil product_name distinct berdasarkan productId/parent --- + const relatedSubs = subscriptions.filter( + sub => sub.product_id === productId || sub.product_parent_id === productId + ); + const distinctNames = [...new Set(relatedSubs.map(sub => sub.product_name))]; + + if (distinctNames.length > 1) { + // lebih dari 1 → pakai dashboard=true const url = new URL(finalUri); - url.searchParams.set('token', token || ''); + url.searchParams.set("token", token || ""); + finalUri = url.toString(); + } else if (distinctNames.length === 1) { + // hanya 1 → tambahkan productName= sebelum query lain + const url = new URL(finalUri); + url.searchParams.set("token", token || ""); + url.searchParams.set("productName", distinctNames[0]); finalUri = url.toString(); } window.location.href = finalUri; - } - else {// Assuming you already imported processProducts from './processProducts' - + } else { + // fallback ambil detail produk via fetch fetch('https://bot.kediritechnopark.com/webhook/store-production/products', { method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, + headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ itemsId: [productId], withChildren: true, @@ -227,9 +238,7 @@ function App() { .then(res => res.json()) .then(data => { if (Array.isArray(data) && data.length > 0) { - // Process the raw data to group children under their parent const processed = processProducts(data); - // Set the first product (which should be the parent with children nested) setSelectedProduct(processed[0]); setShowedModal('product'); } @@ -240,12 +249,9 @@ function App() { if (unauthorizedUri) { window.location.href = decodeURIComponent(unauthorizedUri); } else { - fetch('https://bot.kediritechnopark.com/webhook/store-production/products', { method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, + headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ itemsId: [productId], withChildren: true, @@ -254,9 +260,7 @@ function App() { .then(res => res.json()) .then(data => { if (Array.isArray(data) && data.length > 0) { - // Process the raw data to group children under their parent const processed = processProducts(data); - // Set the first product (which should be the parent with children nested) setSelectedProduct(processed[0]); setShowedModal('product'); }