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) postLoginAction(); // resume action (e.g., checkout)
setPostLoginAction(null); setPostLoginAction(null);
} }
window.location.reload() // window.location.reload()
} else { } else {
alert('Token tidak ditemukan pada respons login'); 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] : ''; const token = tokenCookie ? tokenCookie.split('=')[1] : '';
if (!token) { if (!token) {
setPostLoginAction(() => () => onCheckout()); setPostLoginAction(() => () => setShowedModal('product'));
setShowedModal('login'); setShowedModal('login');
return; return;
} }
if (product.type == 'product') { if (product.type == 'product') {
const hasMatchingSubscription = Array.isArray(subscriptions) && const hasMatchingSubscription = Array.isArray(subscriptions) &&
subscriptions.some(sub => subscriptions.some(sub =>
sub.product_name?.toLowerCase().includes(product.name.toLowerCase()) 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 (hasMatchingSubscription) {
if (product.children && product.children.length > 0) { const matching = subscriptions.filter(sub =>
setShowChildSelector(true); sub.product_id === product.id || sub.product_parent_id === product.id
);
if (hasMatchingSubscription) { if (matching.length > 0) {
const matching = subscriptions.filter(sub => // ✅ Select only the first for each product_name
sub.product_name?.toLowerCase().includes(product.name.toLowerCase()) const uniqueByName = Array.from(
); new Map(matching.map(sub => [sub.product_name, sub])).values()
const uniqueByName = Array.from(new Map(matching.map(sub => [sub.product_name, sub])).values()); );
if (uniqueByName.length > 0) { setMatchingSubscriptions(uniqueByName);
setMatchingSubscriptions(uniqueByName); }
} }
} return;
return; }
}
// No children, but has subscription match // No children, but has subscription match
if (hasMatchingSubscription) { if (hasMatchingSubscription) {
const matching = subscriptions.filter(sub => const matching = subscriptions.filter(sub =>
sub.product_name?.toLowerCase().includes(product.name.toLowerCase()) sub.product_id === product.id || sub.product_parent_id === product.id
); );
const uniqueByName = Array.from(new Map(matching.map(sub => [sub.product_name, sub])).values());
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 // No children, no matching subscription

View File

@@ -62,39 +62,41 @@ const CoursePage = ({ subscriptions }) => {
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
body: JSON.stringify({ itemsId: productIds, type: 'product' }), body: JSON.stringify({ itemsId: productIds }),
}) })
.then(res => res.json()) .then(res => res.json())
.then(data => { .then(data => {
const enrichedData = Object.values(groupedSubs) const enrichedData = Object.values(groupedSubs)
.filter(group => data.some(p => p.id === group.product_id)) // ✅ hanya produk yang ada di metadata .filter(group => data.some(p => p.id === group.product_id)) // ✅ hanya produk yang ada di metadata
.map(group => { .map(group => {
const productData = data.find(p => p.id == group.product_id); const productData = data.find(p => p.id == group.product_id);
// Cek fallback image dari parent jika image kosong dan sub_product_of ada // Cek fallback image dari parent jika image kosong dan sub_product_of ada
let image = productData?.image || ''; let image = productData?.image || '';
if (!image && productData?.sub_product_of) { let description = productData?.description || '';
const parent = data.find(p => p.id === productData.sub_product_of); if (!image && productData?.sub_product_of) {
image = parent?.image || ''; const parent = data.find(p => p.id === productData.sub_product_of);
} image = parent?.image || '';
description = parent?.description || '';
}
return { return {
id: group.product_id, id: group.product_id,
name: group.product_name, name: group.product_name,
type: productData?.type || 'product', type: productData?.type || 'product',
image: image, image: image,
description: productData?.description || '', description: description,
price: productData?.price || 0, price: productData?.price || 0,
currency: productData?.currency || 'IDR', currency: productData?.currency || 'IDR',
duration: productData?.duration || {}, duration: productData?.duration || {},
sub_product_of: productData?.sub_product_of || null, sub_product_of: productData?.sub_product_of || null,
is_visible: productData?.is_visible ?? true, is_visible: productData?.is_visible ?? true,
unit_type: productData?.unit_type || group.unit_type, unit_type: productData?.unit_type || group.unit_type,
quantity: group.quantity, quantity: group.quantity,
end_date: group.end_date, end_date: group.end_date,
children: [] children: []
}; };
}); });
console.log(enrichedData) console.log(enrichedData)
setProducts(enrichedData); setProducts(enrichedData);