ok
This commit is contained in:
40
src/App.js
40
src/App.js
@@ -194,31 +194,42 @@ function App() {
|
|||||||
if (!productModalRequest || !subscriptions) return;
|
if (!productModalRequest || !subscriptions) return;
|
||||||
|
|
||||||
const { productId, authorizedUri, unauthorizedUri } = productModalRequest;
|
const { productId, authorizedUri, unauthorizedUri } = productModalRequest;
|
||||||
console.log(subscriptions)
|
|
||||||
const hasAccess = subscriptions && subscriptions.some(
|
const hasAccess = subscriptions && subscriptions.some(
|
||||||
sub => sub.product_id === productId || sub.product_parent_id === productId
|
sub => sub.product_id === productId || sub.product_parent_id === productId
|
||||||
);
|
);
|
||||||
console.log(hasAccess)
|
console.log("hasAccess:", hasAccess);
|
||||||
|
|
||||||
if (hasAccess) {
|
if (hasAccess) {
|
||||||
if (authorizedUri) {
|
if (authorizedUri) {
|
||||||
let finalUri = decodeURIComponent(authorizedUri);
|
let finalUri = decodeURIComponent(authorizedUri);
|
||||||
const token = document.cookie.match(/(^| )token=([^;]+)/)?.[2];
|
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);
|
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=<nama> sebelum query lain
|
||||||
|
const url = new URL(finalUri);
|
||||||
|
url.searchParams.set("token", token || "");
|
||||||
|
url.searchParams.set("productName", distinctNames[0]);
|
||||||
finalUri = url.toString();
|
finalUri = url.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
window.location.href = finalUri;
|
window.location.href = finalUri;
|
||||||
}
|
} else {
|
||||||
else {// Assuming you already imported processProducts from './processProducts'
|
// fallback ambil detail produk via fetch
|
||||||
|
|
||||||
fetch('https://bot.kediritechnopark.com/webhook/store-production/products', {
|
fetch('https://bot.kediritechnopark.com/webhook/store-production/products', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: { 'Content-Type': 'application/json' },
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
itemsId: [productId],
|
itemsId: [productId],
|
||||||
withChildren: true,
|
withChildren: true,
|
||||||
@@ -227,9 +238,7 @@ function App() {
|
|||||||
.then(res => res.json())
|
.then(res => res.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
if (Array.isArray(data) && data.length > 0) {
|
if (Array.isArray(data) && data.length > 0) {
|
||||||
// Process the raw data to group children under their parent
|
|
||||||
const processed = processProducts(data);
|
const processed = processProducts(data);
|
||||||
// Set the first product (which should be the parent with children nested)
|
|
||||||
setSelectedProduct(processed[0]);
|
setSelectedProduct(processed[0]);
|
||||||
setShowedModal('product');
|
setShowedModal('product');
|
||||||
}
|
}
|
||||||
@@ -240,12 +249,9 @@ function App() {
|
|||||||
if (unauthorizedUri) {
|
if (unauthorizedUri) {
|
||||||
window.location.href = decodeURIComponent(unauthorizedUri);
|
window.location.href = decodeURIComponent(unauthorizedUri);
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
fetch('https://bot.kediritechnopark.com/webhook/store-production/products', {
|
fetch('https://bot.kediritechnopark.com/webhook/store-production/products', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: { 'Content-Type': 'application/json' },
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
itemsId: [productId],
|
itemsId: [productId],
|
||||||
withChildren: true,
|
withChildren: true,
|
||||||
@@ -254,9 +260,7 @@ function App() {
|
|||||||
.then(res => res.json())
|
.then(res => res.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
if (Array.isArray(data) && data.length > 0) {
|
if (Array.isArray(data) && data.length > 0) {
|
||||||
// Process the raw data to group children under their parent
|
|
||||||
const processed = processProducts(data);
|
const processed = processProducts(data);
|
||||||
// Set the first product (which should be the parent with children nested)
|
|
||||||
setSelectedProduct(processed[0]);
|
setSelectedProduct(processed[0]);
|
||||||
setShowedModal('product');
|
setShowedModal('product');
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user