This commit is contained in:
Vassshhh
2025-08-05 23:46:17 +07:00
parent 4d12884941
commit 8c0b9568e8
3 changed files with 67 additions and 60 deletions

View File

@@ -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');
}

View File

@@ -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

View File

@@ -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);