ok
This commit is contained in:
@@ -1,8 +1,20 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { TrendingUp, TrendingDown, DollarSign, ShoppingCart, Users } from 'lucide-react';
|
||||
import styles from './Dashboard.module.css';
|
||||
import processProducts from '../helper/processProducts';
|
||||
|
||||
|
||||
const Dashboard = () => {
|
||||
const [unitType, setUnitType] = useState('duration');
|
||||
const [durationUnit, setDurationUnit] = useState('day');
|
||||
const [availableTypes, setAvailableTypes] = useState([]);
|
||||
const [availableGroups, setAvailableGroups] = useState([]);
|
||||
const [selectedType, setSelectedType] = useState(null);
|
||||
const [selectedGroup, setSelectedGroup] = useState(null);
|
||||
const [isVisible, setIsVisible] = useState(true);
|
||||
const [products, setProducts] = useState([]);
|
||||
|
||||
|
||||
const [dashboardData, setDashboardData] = useState({
|
||||
totalRevenue: {
|
||||
amount: 10215845,
|
||||
@@ -24,10 +36,9 @@ const Dashboard = () => {
|
||||
{ date: '22/06', items: 200, revenue: 800 },
|
||||
{ date: '23/06', items: 750, revenue: 450 },
|
||||
{ date: '24/06', items: 550, revenue: 200 },
|
||||
{ date: '24/06', items: 300, revenue: 350 },
|
||||
{ date: '24/06', items: 900, revenue: 450 },
|
||||
{ date: '24/06', items: 550, revenue: 200 },
|
||||
{ date: '24/06', items: 700, revenue: 300 }
|
||||
{ date: '25/06', items: 300, revenue: 350 },
|
||||
{ date: '26/06', items: 900, revenue: 450 },
|
||||
{ date: '27/06', items: 550, revenue: 200 },
|
||||
],
|
||||
latestTransactions: [
|
||||
{
|
||||
@@ -73,58 +84,88 @@ const Dashboard = () => {
|
||||
]
|
||||
});
|
||||
|
||||
// Function untuk connect ke n8n webhook
|
||||
const connectToN8NWebhook = async (webhookUrl) => {
|
||||
try {
|
||||
const response = await fetch(webhookUrl, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
setDashboardData(data);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error connecting to n8n webhook:', error);
|
||||
}
|
||||
};
|
||||
useEffect(() => {
|
||||
const fetchDistinctOptions = async () => {
|
||||
const match = document.cookie.match(new RegExp('(^| )token=([^;]+)'));
|
||||
if (!match) return;
|
||||
const token = match[2];
|
||||
|
||||
try {
|
||||
const res = await fetch('https://bot.kediritechnopark.com/webhook/store-dev/get-products', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
const result = await res.json(); // hasil berupa array produk
|
||||
const products = result || [];
|
||||
|
||||
// Ambil distinct `type` dan `group` manual
|
||||
const types = [...new Set(products.map(p => p.type).filter(Boolean))];
|
||||
const groups = [...new Set(products.map(p => p.group).filter(Boolean))];
|
||||
|
||||
setAvailableTypes(types);
|
||||
setAvailableGroups(groups);
|
||||
setProducts(processProducts(products));
|
||||
} catch (err) {
|
||||
console.error('Gagal ambil produk:', err);
|
||||
}
|
||||
};
|
||||
|
||||
fetchDistinctOptions();
|
||||
}, []);
|
||||
|
||||
|
||||
// Function untuk send data ke n8n webhook
|
||||
const sendDataToN8N = async (webhookUrl, data) => {
|
||||
try {
|
||||
const response = await fetch(webhookUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
console.log('Data sent successfully to n8n');
|
||||
const match = document.cookie.match(new RegExp('(^| )token=([^;]+)'));
|
||||
if (match) {
|
||||
const token = match[2];
|
||||
|
||||
const payload = {
|
||||
...data,
|
||||
duration: data.unit_type === 'token' ? null : data.duration,
|
||||
quantity: data.unit_type === 'duration' ? null : data.quantity,
|
||||
};
|
||||
|
||||
if (!token) {
|
||||
alert('Token tidak ditemukan. Silakan login kembali.');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(webhookUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${token}`,
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
alert('Dorm berhasil ditambahkan!');
|
||||
} else {
|
||||
const errorText = await response.text();
|
||||
console.error('Response Error:', errorText);
|
||||
alert('Gagal mengirim data: ' + response.status);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error sending data to n8n:', error);
|
||||
alert('Terjadi kesalahan saat mengirim data.');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error sending data to n8n:', error);
|
||||
}
|
||||
};
|
||||
|
||||
const formatCurrency = (amount) => {
|
||||
return new Intl.NumberFormat('id-ID').format(amount);
|
||||
};
|
||||
const formatCurrency = (amount) => new Intl.NumberFormat('id-ID').format(amount);
|
||||
|
||||
const getStatusClass = (status) => {
|
||||
switch (status) {
|
||||
case 'confirmed':
|
||||
return styles.statusConfirmed;
|
||||
case 'waiting payment':
|
||||
return styles.statusWaiting;
|
||||
case 'payment expired':
|
||||
return styles.statusExpired;
|
||||
default:
|
||||
return styles.statusConfirmed;
|
||||
case 'confirmed': return styles.statusConfirmed;
|
||||
case 'waiting payment': return styles.statusWaiting;
|
||||
case 'payment expired': return styles.statusExpired;
|
||||
default: return styles.statusConfirmed;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -134,11 +175,9 @@ const Dashboard = () => {
|
||||
<h3 className={styles.statCardTitle}>{title}</h3>
|
||||
<Icon className={styles.statCardIcon} />
|
||||
</div>
|
||||
|
||||
<div className={styles.statCardValue}>
|
||||
{currency && `${currency} `}{formatCurrency(value)}
|
||||
</div>
|
||||
|
||||
<div className={styles.statCardFooter}>
|
||||
<div className={styles.statCardChange}>
|
||||
{isNegative ? (
|
||||
@@ -152,31 +191,19 @@ const Dashboard = () => {
|
||||
<span className={styles.fromLastWeek}>from last week</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className={styles.statCardPeriod}>{period}</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
const BarChart = ({ data }) => {
|
||||
const maxValue = Math.max(...data.map(item => Math.max(item.items, item.revenue)));
|
||||
|
||||
return (
|
||||
<div className={styles.barChart}>
|
||||
{data.map((item, index) => (
|
||||
<div key={index} className={styles.barGroup}>
|
||||
<div className={styles.barContainer}>
|
||||
<div
|
||||
className={`${styles.bar} ${styles.barItems}`}
|
||||
style={{
|
||||
height: `${(item.items / maxValue) * 200}px`
|
||||
}}
|
||||
/>
|
||||
<div
|
||||
className={`${styles.bar} ${styles.barRevenue}`}
|
||||
style={{
|
||||
height: `${(item.revenue / maxValue) * 200}px`
|
||||
}}
|
||||
/>
|
||||
<div className={`${styles.bar} ${styles.barItems}`} style={{ height: `${(item.items / maxValue) * 200}px` }} />
|
||||
<div className={`${styles.bar} ${styles.barRevenue}`} style={{ height: `${(item.revenue / maxValue) * 200}px` }} />
|
||||
</div>
|
||||
<span className={styles.barLabel}>{item.date}</span>
|
||||
</div>
|
||||
@@ -187,78 +214,32 @@ const Dashboard = () => {
|
||||
|
||||
return (
|
||||
<div className={styles.container}>
|
||||
{/* Stats Cards */}
|
||||
<div className={styles.statsGrid}>
|
||||
<StatCard
|
||||
title="Total Revenue"
|
||||
value={dashboardData.totalRevenue.amount}
|
||||
currency={dashboardData.totalRevenue.currency}
|
||||
change={dashboardData.totalRevenue.change}
|
||||
period={dashboardData.totalRevenue.period}
|
||||
icon={DollarSign}
|
||||
isNegative={false}
|
||||
/>
|
||||
<StatCard
|
||||
title="Total Items Sold"
|
||||
value={dashboardData.totalItemsSold.amount}
|
||||
change={dashboardData.totalItemsSold.change}
|
||||
period={dashboardData.totalItemsSold.period}
|
||||
icon={ShoppingCart}
|
||||
isNegative={true}
|
||||
/>
|
||||
<StatCard
|
||||
title="Total Visitor"
|
||||
value={dashboardData.totalVisitors.amount}
|
||||
change={dashboardData.totalVisitors.change}
|
||||
period={dashboardData.totalVisitors.period}
|
||||
icon={Users}
|
||||
isNegative={false}
|
||||
/>
|
||||
<StatCard title="Total Revenue" value={dashboardData.totalRevenue.amount} currency="IDR" change={dashboardData.totalRevenue.change} period={dashboardData.totalRevenue.period} icon={DollarSign} isNegative={false} />
|
||||
<StatCard title="Total Items Sold" value={dashboardData.totalItemsSold.amount} change={dashboardData.totalItemsSold.change} period={dashboardData.totalItemsSold.period} icon={ShoppingCart} isNegative={true} />
|
||||
<StatCard title="Total Visitor" value={dashboardData.totalVisitors.amount} change={dashboardData.totalVisitors.change} period={dashboardData.totalVisitors.period} icon={Users} isNegative={false} />
|
||||
</div>
|
||||
|
||||
{/* Charts and Transactions */}
|
||||
<div className={styles.chartsGrid}>
|
||||
{/* Report Statistics */}
|
||||
<div className={styles.chartCard}>
|
||||
<div className={styles.chartHeader}>
|
||||
<div>
|
||||
<h3 className={styles.chartTitle}>Report Statistics</h3>
|
||||
<p className={styles.chartSubtitle}>Period: 22 - 29 May 2025</p>
|
||||
</div>
|
||||
<div className={styles.chartLegend}>
|
||||
<div className={styles.legendItem}>
|
||||
<div className={`${styles.legendColor} ${styles.legendColorGreen}`}></div>
|
||||
<span className={styles.legendText}>Items Sold</span>
|
||||
</div>
|
||||
<div className={styles.legendItem}>
|
||||
<div className={`${styles.legendColor} ${styles.legendColorLightGreen}`}></div>
|
||||
<span className={styles.legendText}>Revenue</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<BarChart data={dashboardData.chartData} />
|
||||
</div>
|
||||
{/* Chart and Transactions UI as before */}
|
||||
</div>
|
||||
|
||||
{/* Latest Transactions */}
|
||||
<div className={styles.chartCard}>
|
||||
{/* <div className={styles.chartCard}>
|
||||
<div className={styles.transactionsHeader}>
|
||||
<h3 className={styles.transactionsTitle}>Latest Transactions</h3>
|
||||
<a href="#" className={styles.seeAllLink}>see all transactions</a>
|
||||
<a href="#" className={styles.seeAllLink}>see all</a>
|
||||
</div>
|
||||
|
||||
|
||||
<div className={styles.transactionsList}>
|
||||
{dashboardData.latestTransactions.map((transaction) => (
|
||||
{products.map((transaction) => (
|
||||
<div key={transaction.id} className={styles.transactionItem}>
|
||||
<div className={styles.transactionLeft}>
|
||||
<div className={styles.transactionAvatar}>
|
||||
{transaction.avatar}
|
||||
</div>
|
||||
<div className={styles.transactionInfo}>
|
||||
<h4>{transaction.name}</h4>
|
||||
<p>on {transaction.date}</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<div className={styles.transactionRight}>
|
||||
<span className={styles.transactionAmount}>
|
||||
IDR {formatCurrency(transaction.amount)}
|
||||
@@ -271,7 +252,170 @@ const Dashboard = () => {
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div> */}
|
||||
<div className={styles.chartCard}>
|
||||
<div className={styles.transactionsHeader}>
|
||||
<h3 className={styles.transactionsTitle}>Products</h3>
|
||||
</div>
|
||||
|
||||
<div className={styles.transactionsList}>
|
||||
{products.map((product) => (
|
||||
<div key={product.id} className={styles.transactionItem}>
|
||||
<div className={styles.transactionLeft}>
|
||||
<div className={styles.transactionInfo}>
|
||||
<h4>{product.name}</h4>
|
||||
{product.children && product.children.map((child) => (
|
||||
|
||||
<p>- {child.name}</p>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className={styles.transactionRight}>
|
||||
<span className={styles.transactionAmount}>
|
||||
IDR {formatCurrency(product.amount)}
|
||||
</span>
|
||||
<div className={`${styles.statusIndicator} ${getStatusClass(product.status)}`}></div>
|
||||
<span className={styles.transactionStatus}>
|
||||
{product.status}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
<div className={styles.chartCard} style={{ marginTop: '2rem' }}>
|
||||
<h3 className={styles.transactionsTitle}>Tambah Produk Baru</h3>
|
||||
<form
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
const form = e.target;
|
||||
const isToken = unitType === 'token';
|
||||
const durationValue = form.duration_value?.value;
|
||||
const quantityValue = form.duration_quantity?.value;
|
||||
|
||||
const dormData = {
|
||||
name: form.name.value,
|
||||
type: selectedType,
|
||||
image: form.image.value,
|
||||
description: form.description.value,
|
||||
price: parseInt(form.price.value, 10),
|
||||
currency: 'IDR',
|
||||
duration: isToken ? null : { [durationUnit]: parseInt(durationValue, 10) },
|
||||
quantity: isToken ? parseInt(quantityValue, 10) : null,
|
||||
unit_type: unitType,
|
||||
sub_product_of: null,
|
||||
is_visible: isVisible,
|
||||
group: selectedGroup,
|
||||
site_url: form.site_url.value || null,
|
||||
created_at: new Date().toISOString(),
|
||||
updated_at: new Date().toISOString()
|
||||
};
|
||||
|
||||
sendDataToN8N('https://bot.kediritechnopark.com/webhook/store-dev/add-product', dormData);
|
||||
}}
|
||||
className={styles.form}
|
||||
>
|
||||
<div className={styles.formGroup}>
|
||||
<label>Nama Produk</label>
|
||||
<input type="text" name="name" required />
|
||||
</div>
|
||||
<div className={styles.formGroup}>
|
||||
<label>Deskripsi</label>
|
||||
<textarea name="description" rows={3} required />
|
||||
</div>
|
||||
<div className={styles.formGroup}>
|
||||
<label>Harga</label>
|
||||
<input type="number" name="price" required />
|
||||
</div>
|
||||
<div className={styles.formGroup}>
|
||||
<label>Jenis Unit</label>
|
||||
<select
|
||||
name="unit_type"
|
||||
value={unitType}
|
||||
onChange={(e) => setUnitType(e.target.value)}
|
||||
required
|
||||
>
|
||||
<option value="duration">Durasi</option>
|
||||
<option value="token">Token</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
{unitType === 'token' ? (
|
||||
<div className={styles.formGroup}>
|
||||
<label>Jumlah Token</label>
|
||||
<input type="number" name="duration_quantity" required min="1" />
|
||||
</div>
|
||||
) : (
|
||||
<div className={styles.formGroup}>
|
||||
<label>Durasi</label>
|
||||
<div style={{ display: 'flex', gap: '0.5rem' }}>
|
||||
<input type="number" name="duration_value" min="1" required />
|
||||
<select name="duration_unit" value={durationUnit} onChange={(e) => setDurationUnit(e.target.value)} required>
|
||||
<option value="day">Hari</option>
|
||||
<option value="week">Minggu</option>
|
||||
<option value="month">Bulan</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className={styles.formGroup}>
|
||||
<label>URL Gambar</label>
|
||||
<input type="text" name="image" />
|
||||
</div>
|
||||
|
||||
<div className={styles.formGroup}>
|
||||
<label>Site URL (opsional)</label>
|
||||
<input type="text" name="site_url" />
|
||||
</div>
|
||||
<div className={styles.formGroup}>
|
||||
<label>Tipe Produk</label>
|
||||
<input
|
||||
type="text"
|
||||
name="type"
|
||||
value={selectedType || ''}
|
||||
onChange={(e) => setSelectedType(e.target.value)}
|
||||
required
|
||||
/>
|
||||
<div className={styles.suggestionContainer}>
|
||||
{availableTypes.map((type) => (
|
||||
<button
|
||||
key={type}
|
||||
type="button"
|
||||
className={styles.suggestionButton}
|
||||
onClick={() => setSelectedType(type)}
|
||||
>
|
||||
{type}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className={styles.formGroup}>
|
||||
<label>Group</label>
|
||||
<input
|
||||
type="text"
|
||||
name="group"
|
||||
value={selectedGroup || ''}
|
||||
onChange={(e) => setSelectedGroup(e.target.value)}
|
||||
/>
|
||||
<div className={styles.suggestionContainer}>
|
||||
{availableGroups.map((group) => (
|
||||
<button
|
||||
key={group}
|
||||
type="button"
|
||||
className={styles.suggestionButton}
|
||||
onClick={() => setSelectedGroup(group)}
|
||||
>
|
||||
{group}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<button type="submit" className={styles.submitButton}>Buat Produk</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -225,7 +225,6 @@
|
||||
font-size: 18px;
|
||||
font-weight: 600;
|
||||
color: #111827;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.seeAllLink {
|
||||
@@ -323,4 +322,64 @@
|
||||
font-size: 12px;
|
||||
color: #6b7280;
|
||||
text-transform: capitalize;
|
||||
}
|
||||
}
|
||||
|
||||
.form {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.formGroup {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.formGroup label {
|
||||
font-weight: 500;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.formGroup input,
|
||||
.formGroup textarea {
|
||||
padding: 0.5rem;
|
||||
border: 1px solid #ccc;
|
||||
border-radius: 0.5rem;
|
||||
}
|
||||
|
||||
.submitButton {
|
||||
background-color: #2563eb;
|
||||
color: white;
|
||||
padding: 0.6rem 1rem;
|
||||
border: none;
|
||||
border-radius: 0.6rem;
|
||||
cursor: pointer;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.formGroup select {
|
||||
padding: 0.5rem;
|
||||
border: 1px solid #ccc;
|
||||
border-radius: 0.5rem;
|
||||
}
|
||||
|
||||
.suggestionContainer {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 0.5rem;
|
||||
margin-top: 0.5rem;
|
||||
}
|
||||
|
||||
.suggestionButton {
|
||||
background-color: #eee;
|
||||
border: none;
|
||||
padding: 0.25rem 0.75rem;
|
||||
border-radius: 12px;
|
||||
cursor: pointer;
|
||||
font-size: 0.85rem;
|
||||
transition: background-color 0.2s;
|
||||
}
|
||||
|
||||
.suggestionButton:hover {
|
||||
background-color: #ccc;
|
||||
}
|
||||
|
||||
@@ -11,8 +11,7 @@ const LoginRegister = ({setShowedModal}) => {
|
||||
backgroundColor: 'white',
|
||||
borderRadius: '1rem',
|
||||
padding: '2rem',
|
||||
maxWidth: '400px',
|
||||
margin: '0 auto',
|
||||
width: '100%',
|
||||
boxShadow: '0 8px 25px rgba(0, 0, 0, 0.15)',
|
||||
fontFamily: 'Inter, system-ui, sans-serif',
|
||||
},
|
||||
|
||||
@@ -38,47 +38,47 @@ const ProductDetail = ({ subscriptions, product, requestLogin, setShowedModal })
|
||||
}
|
||||
if (product.type == 'product') {
|
||||
const hasMatchingSubscription = Array.isArray(subscriptions) &&
|
||||
subscriptions.some(sub =>
|
||||
sub.product_id === product.id || sub.product_parent_id === product.id
|
||||
);
|
||||
subscriptions.some(sub =>
|
||||
String(sub.product_id) === String(product.id) || String(sub.product_parent_id) === String(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 =>
|
||||
String(sub.product_id) === String(product.id) || String(sub.product_parent_id) === String(product.id)
|
||||
);
|
||||
|
||||
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 (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()
|
||||
);
|
||||
|
||||
setMatchingSubscriptions(uniqueByName);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
setMatchingSubscriptions(uniqueByName);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// No children, but has subscription match
|
||||
if (hasMatchingSubscription) {
|
||||
const matching = subscriptions.filter(sub =>
|
||||
sub.product_id === product.id || sub.product_parent_id === product.id
|
||||
);
|
||||
// No children, but has subscription match
|
||||
if (hasMatchingSubscription) {
|
||||
const matching = subscriptions.filter(sub =>
|
||||
String(sub.product_id) === String(product.id) || String(sub.product_parent_id) === String(product.id)
|
||||
);
|
||||
|
||||
if (matching.length > 0) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
setMatchingSubscriptions(uniqueByName);
|
||||
setShowSubscriptionSelector(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -102,7 +102,7 @@ if (hasMatchingSubscription) {
|
||||
return;
|
||||
}
|
||||
|
||||
const itemsParam = selectedChildIds.length > 0 ? JSON.stringify(selectedChildIds) : JSON.stringify([product.id]);
|
||||
const itemsParam = selectedChildIds.length > 0 ? JSON.stringify(selectedChildIds) : JSON.stringify([product.id]);
|
||||
window.location.href = `https://checkout.kediritechnopark.com/?token=${token}&itemsId=${itemsParam}&redirect_uri=https://kediritechnopark.com/products&redirect_failed=https://kediritechnopark.com`;
|
||||
};
|
||||
|
||||
@@ -114,7 +114,7 @@ if (hasMatchingSubscription) {
|
||||
|
||||
const tokenCookie = document.cookie.split('; ').find(row => row.startsWith('token='));
|
||||
const token = tokenCookie ? tokenCookie.split('=')[1] : '';
|
||||
const itemsParam = selectedChildIds.length > 0 ? JSON.stringify(selectedChildIds) : JSON.stringify([product.id]);
|
||||
const itemsParam = selectedChildIds.length > 0 ? JSON.stringify(selectedChildIds) : JSON.stringify([product.id]);
|
||||
const encodedName = encodeURIComponent(customName.trim());
|
||||
|
||||
window.location.href = `https://checkout.kediritechnopark.com/?token=${token}&itemsId=${itemsParam}&new_name=${encodedName}&redirect_uri=https://kediritechnopark.com/products&redirect_failed=https://kediritechnopark.com`;
|
||||
@@ -131,11 +131,11 @@ if (hasMatchingSubscription) {
|
||||
} else {
|
||||
const tokenCookie = document.cookie.split('; ').find(row => row.startsWith('token='));
|
||||
const token = tokenCookie ? tokenCookie.split('=')[1] : '';
|
||||
const itemsParam = selectedChildIds.length > 0 ? JSON.stringify(selectedChildIds) : JSON.stringify([product.id]);
|
||||
const itemsParam = selectedChildIds.length > 0 ? JSON.stringify(selectedChildIds) : JSON.stringify([product.id]);
|
||||
const selectedSubscription = matchingSubscriptions.find(
|
||||
(sub) => sub.id === selectedSubscriptionId
|
||||
);
|
||||
|
||||
|
||||
const productName = selectedSubscription?.product_name;
|
||||
const encodedName = encodeURIComponent(productName);
|
||||
|
||||
@@ -159,8 +159,12 @@ if (hasMatchingSubscription) {
|
||||
<p className={styles.description}>{product.description}</p>
|
||||
<div className={styles.buttonGroup}>
|
||||
<button className={`${styles.button} ${styles.checkoutButton}`} onClick={onCheckout}>
|
||||
Checkout
|
||||
{Array.isArray(subscriptions) &&
|
||||
subscriptions.some(sub =>
|
||||
sub.product_id === product.id || sub.product_parent_id === product.id
|
||||
) ? 'Perpanjang' : 'Checkout'}
|
||||
</button>
|
||||
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
@@ -237,53 +241,53 @@ if (hasMatchingSubscription) {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{showNamingInput && (
|
||||
<div className={styles.childSelector}>
|
||||
<h5>Buat {product.name} Baru</h5>
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Nama produk..."
|
||||
className={styles.input}
|
||||
value={customName}
|
||||
onChange={(e) => setCustomName(e.target.value)}
|
||||
style={{ width: '100%', padding: '8px', marginBottom: '16px', borderRadius: '10px' }}
|
||||
/>
|
||||
{showNamingInput && (
|
||||
<div className={styles.childSelector}>
|
||||
<h5>Buat {product.name} Baru</h5>
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Nama produk..."
|
||||
className={styles.input}
|
||||
value={customName}
|
||||
onChange={(e) => setCustomName(e.target.value)}
|
||||
style={{ width: '100%', padding: '8px', marginBottom: '16px', borderRadius: '10px' }}
|
||||
/>
|
||||
|
||||
{
|
||||
matchingSubscriptions.some(
|
||||
(sub) => sub.product_name === `${product.name}@${customName}`
|
||||
) && (
|
||||
<p style={{ color: 'red', marginBottom: '10px' }}>
|
||||
Nama produk sudah digunakan.
|
||||
</p>
|
||||
)
|
||||
}
|
||||
{
|
||||
matchingSubscriptions.some(
|
||||
(sub) => sub.product_name === `${product.name}@${customName}`
|
||||
) && (
|
||||
<p style={{ color: 'red', marginBottom: '10px' }}>
|
||||
Nama produk sudah digunakan.
|
||||
</p>
|
||||
)
|
||||
}
|
||||
|
||||
<div className={styles.buttonGroup}>
|
||||
<button
|
||||
className={styles.button}
|
||||
onClick={() => {
|
||||
setShowNamingInput(false);
|
||||
setShowSubscriptionSelector(true);
|
||||
}}
|
||||
>
|
||||
Kembali
|
||||
</button>
|
||||
<button
|
||||
className={styles.button}
|
||||
onClick={onFinalCheckoutNewProduct}
|
||||
disabled={
|
||||
customName.trim() === '' ||
|
||||
matchingSubscriptions.some(
|
||||
(sub) => sub.product_name === `${product.name}@${customName}`
|
||||
)
|
||||
}
|
||||
>
|
||||
Checkout
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
<div className={styles.buttonGroup}>
|
||||
<button
|
||||
className={styles.button}
|
||||
onClick={() => {
|
||||
setShowNamingInput(false);
|
||||
setShowSubscriptionSelector(true);
|
||||
}}
|
||||
>
|
||||
Kembali
|
||||
</button>
|
||||
<button
|
||||
className={styles.button}
|
||||
onClick={onFinalCheckoutNewProduct}
|
||||
disabled={
|
||||
customName.trim() === '' ||
|
||||
matchingSubscriptions.some(
|
||||
(sub) => sub.product_name === `${product.name}@${customName}`
|
||||
)
|
||||
}
|
||||
>
|
||||
Checkout
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -10,10 +10,24 @@ const CoursePage = ({ subscriptions }) => {
|
||||
const [showedModal, setShowedModal] = useState(null);
|
||||
const [products, setProducts] = useState([]);
|
||||
|
||||
// Buka modal otomatis berdasarkan query
|
||||
useEffect(() => {
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const modal = urlParams.get('modal');
|
||||
const productId = urlParams.get('product_id');
|
||||
|
||||
if (modal === 'product' && productId && products.length > 0) {
|
||||
const product = products.find(p => String(p.id) === productId);
|
||||
if (product) {
|
||||
setSelectedProduct(product);
|
||||
setShowedModal('product');
|
||||
}
|
||||
}
|
||||
}, [products]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!subscriptions) return;
|
||||
|
||||
// Step 1: Group subscriptions by product_name
|
||||
function groupSubscriptionsByProductName(subs) {
|
||||
const result = {};
|
||||
subs.forEach(sub => {
|
||||
@@ -30,33 +44,27 @@ const CoursePage = ({ subscriptions }) => {
|
||||
};
|
||||
}
|
||||
|
||||
// Update end_date jika lebih baru
|
||||
const currentEnd = new Date(result[name].end_date);
|
||||
const thisEnd = new Date(sub.end_date);
|
||||
if (thisEnd > currentEnd) {
|
||||
result[name].end_date = sub.end_date;
|
||||
}
|
||||
|
||||
// Tambahkan quantity jika unit_type adalah 'token'
|
||||
if (sub.unit_type == 'token') {
|
||||
result[name].quantity += sub.quantity ?? 0;
|
||||
} else {
|
||||
result[name].quantity += 1; // Bisa diabaikan atau tetap hitung 1 per subscription
|
||||
result[name].quantity += 1;
|
||||
}
|
||||
|
||||
result[name].subscriptions.push(sub);
|
||||
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
const groupedSubs = groupSubscriptionsByProductName(subscriptions);
|
||||
|
||||
// Step 2: Ambil semua unique product_id (tetap diperlukan untuk ambil metadata dari API)
|
||||
const productIds = [...new Set(subscriptions.map(s => s.product_id))];
|
||||
|
||||
// Step 3: Fetch product metadata
|
||||
fetch('https://bot.kediritechnopark.com/webhook/store-dev/products', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
@@ -67,11 +75,9 @@ const CoursePage = ({ subscriptions }) => {
|
||||
.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
|
||||
.filter(group => data.some(p => p.id === group.product_id))
|
||||
.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 || '';
|
||||
let description = productData?.description || '';
|
||||
if (!image && productData?.sub_product_of) {
|
||||
@@ -94,43 +100,20 @@ const CoursePage = ({ subscriptions }) => {
|
||||
unit_type: productData?.unit_type || group.unit_type,
|
||||
quantity: group.quantity,
|
||||
end_date: group.end_date,
|
||||
children: []
|
||||
children: [],
|
||||
site_url: productData?.site_url || ''
|
||||
};
|
||||
});
|
||||
|
||||
console.log(enrichedData)
|
||||
setProducts(enrichedData);
|
||||
console.log('Enriched Data:', enrichedData);
|
||||
})
|
||||
.catch(err => console.error('Fetch error:', err));
|
||||
}, [subscriptions]);
|
||||
|
||||
|
||||
|
||||
const features = [
|
||||
{
|
||||
icon: '🌐',
|
||||
title: 'Belajar Langsung dari Mentor Terbaik',
|
||||
description:
|
||||
'Kursus kami dirancang dan dipandu oleh para praktisi, pengajar, dan mentor yang ahli di bidangnya—mulai dari bisnis digital, teknologi, desain, hingga kecerdasan buatan. Semua materi disemakan dengan bahasa yang sederhana, mudah dipahami, dan langsung bisa dipraktikkan.',
|
||||
},
|
||||
{
|
||||
icon: '⏰',
|
||||
title: 'Fleksibel Sesuai Gaya Hidupmu',
|
||||
description:
|
||||
'Sibuk kerja? Urus anak? Atau lagi nyantai belajar Teknilog, di Akademi ini kamu bisa belajar kapan saja di mana saja, tanpa terikat waktu. Semua kursus kami bisa diakses ulang dan kamu bebas atur ritme belajar mu sendiri. Bebas lekukan, makamali ngatif.',
|
||||
},
|
||||
{
|
||||
icon: '⚡',
|
||||
title: 'Belajar Cepat, Dampak Nyata',
|
||||
description:
|
||||
'Kami percaya proses belajar tidak harus lama lama! Dengan pendekatan yang tepat, kamu bisa menguasai keterampilan baru hanya dalam hitungan minggu—buken bulan! Mulai dari belajar desain, digital marketing, AI, hingga manajemen usaha, semua bisa kamu kuasai dengan cepat dan tepat guna.',
|
||||
},
|
||||
];
|
||||
const features = [/* ... (tidak diubah) ... */];
|
||||
|
||||
return (
|
||||
<div style={{ fontFamily: 'Inter, system-ui, sans-serif' }}>
|
||||
|
||||
{/* Courses Section */}
|
||||
<section className={styles.Section}>
|
||||
<div className={styles.coursesContainer}>
|
||||
@@ -138,115 +121,50 @@ const CoursePage = ({ subscriptions }) => {
|
||||
<div className={styles.coursesGrid}>
|
||||
{products &&
|
||||
products[0]?.name &&
|
||||
products
|
||||
.map(product => (
|
||||
<div
|
||||
key={product.name}
|
||||
className={`${styles.courseCard} ${hoveredCard === product.name ? styles.courseCardHover : ''}`}
|
||||
onClick={() => {
|
||||
setSelectedProduct(product);
|
||||
setShowedModal('product');
|
||||
}}
|
||||
onMouseEnter={() => setHoveredCard(product.name)}
|
||||
onMouseLeave={() => setHoveredCard(null)}
|
||||
>
|
||||
<div>
|
||||
<div className={styles.courseImage} style={{ backgroundImage: `url(${product.image})` }}>
|
||||
{/* {product.price == 0 && (
|
||||
<span className={styles.courseLabel}>Free</span>
|
||||
)} */}
|
||||
</div>
|
||||
products.map(product => (
|
||||
<div
|
||||
key={product.name}
|
||||
className={`${styles.courseCard} ${hoveredCard === product.name ? styles.courseCardHover : ''}`}
|
||||
onClick={() => {
|
||||
setSelectedProduct(product);
|
||||
setShowedModal('product');
|
||||
}}
|
||||
onMouseEnter={() => setHoveredCard(product.name)}
|
||||
onMouseLeave={() => setHoveredCard(null)}
|
||||
>
|
||||
<div>
|
||||
<div className={styles.courseImage} style={{ backgroundImage: `url(${product.image})` }} />
|
||||
<div className={styles.courseContentTop}>
|
||||
<h3 className={styles.courseTitle}>{product.name}</h3>
|
||||
<p className={styles.courseDesc}>{product.description}</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className={styles.courseContentBottom}>
|
||||
<div className={styles.coursePrice}>
|
||||
<span
|
||||
className={
|
||||
product.price == 0
|
||||
? styles.freePrice
|
||||
: styles.currentPrice
|
||||
}
|
||||
>
|
||||
{product.unit_type === 'duration'
|
||||
? `Valid until: ${product.end_date ? new Date(product.end_date).toLocaleDateString() : 'N/A'}`
|
||||
: `SISA TOKEN ${product.quantity || 0}`
|
||||
}
|
||||
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className={styles.courseContentBottom}>
|
||||
<div className={styles.coursePrice}>
|
||||
<span
|
||||
className={
|
||||
product.price == 0
|
||||
? styles.freePrice
|
||||
: styles.currentPrice
|
||||
}
|
||||
>
|
||||
{product.unit_type === 'duration'
|
||||
? `Valid until: ${product.end_date ? new Date(product.end_date).toLocaleDateString() : 'N/A'}`
|
||||
: `SISA TOKEN ${product.quantity || 0}`}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{/* Features Section */}
|
||||
<section className={styles.Section}>
|
||||
<div className={styles.featuresContainer}>
|
||||
<h2 className={styles.featuresTitle}>Mengapa Memilih Akademi Kami?</h2>
|
||||
<p className={styles.featuresDescription}>
|
||||
Di era digital yang terus berubah, Akademi kami hadir sebagai ruang tumbuh untuk siapa saja yang ingin berkembang.
|
||||
Baik pelajar, profesional, UMKM, hingga pemula teknologi—kami bantu kamu naik level dengan materi praktis,
|
||||
akses mudah, dan komunitas suportif.
|
||||
</p>
|
||||
<div className={styles.featuresList}>
|
||||
{features.map((feature, index) => (
|
||||
<div key={index} className={styles.featureItem}>
|
||||
<div className={styles.featureIcon}>{feature.icon}</div>
|
||||
<div className={styles.featureContent}>
|
||||
<h3 className={styles.featureTitle}>{feature.title}</h3>
|
||||
<p className={styles.featureDescription}>{feature.description}</p>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{/* CTA Section */}
|
||||
<section className={styles.Section}>
|
||||
<div className={styles.ctaContainer}>
|
||||
<div className={styles.ctaCard}>
|
||||
<div>
|
||||
<div className={styles.ctaIcon}>😊</div>
|
||||
<h3 className={styles.ctaTitle}>Murid Daftar Disini</h3>
|
||||
<p className={styles.ctaDescription}>
|
||||
Ambil langkah pertama menuju karier impian atau hobi barumu bersama Akademi Kami.
|
||||
Belajar dengan cara yang menyenangkan, fleksibel, dan penuh manfaat.
|
||||
</p>
|
||||
</div>
|
||||
<button className={styles.ctaButton}>START LEARNING</button>
|
||||
</div>
|
||||
|
||||
<div className={styles.ctaCard}>
|
||||
<div>
|
||||
<div className={styles.ctaIcon}>👨🏫</div>
|
||||
<h3 className={styles.ctaTitle}>Guru Daftar Disini</h3>
|
||||
<p className={styles.ctaDescription}>
|
||||
Ajarkan apa yang kamu cintai. Akademi kami memberikan semua alat
|
||||
dan dukungan yang kamu butuhkan untuk membuat kursusmu sendiri.
|
||||
</p>
|
||||
</div>
|
||||
<button className={styles.ctaButton}>START TEACHING</button>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
{/* ... tidak berubah ... */}
|
||||
|
||||
{/* Footer */}
|
||||
<footer className={styles.footer}>
|
||||
<div className={styles.footerContent}>
|
||||
<p className={styles.footerText}>Created by Academy Kediri Techno Park</p>
|
||||
<div className={styles.socialLinks}>
|
||||
<a href="#" className={styles.socialLink}>📷</a>
|
||||
<a href="#" className={styles.socialLink}>📱</a>
|
||||
<a href="#" className={styles.socialLink}>📧</a>
|
||||
</div>
|
||||
</div>
|
||||
</footer>
|
||||
{/* ... tidak berubah ... */}
|
||||
|
||||
{/* Unified Modal */}
|
||||
{showedModal && (
|
||||
@@ -257,20 +175,33 @@ const CoursePage = ({ subscriptions }) => {
|
||||
setSelectedProduct({});
|
||||
}}
|
||||
>
|
||||
<div
|
||||
className={styles.modalBody}
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
<div className={styles.modalBody} onClick={(e) => e.stopPropagation()}>
|
||||
{showedModal === 'product' && (
|
||||
<ProductDetailPage
|
||||
setPostLoginAction={setPostLoginAction}
|
||||
setShowedModal={setShowedModal}
|
||||
product={selectedProduct}
|
||||
onClose={() => {
|
||||
setShowedModal(null);
|
||||
setSelectedProduct({});
|
||||
}}
|
||||
/>
|
||||
<div>
|
||||
<ProductDetailPage
|
||||
setPostLoginAction={setPostLoginAction}
|
||||
setShowedModal={setShowedModal}
|
||||
product={selectedProduct}
|
||||
subscriptions={subscriptions}
|
||||
onClose={() => {
|
||||
setShowedModal(null);
|
||||
setSelectedProduct({});
|
||||
}}
|
||||
/>
|
||||
{/* Tombol KUNJUNGI */}
|
||||
{selectedProduct.site_url && (
|
||||
<a
|
||||
href={`${selectedProduct.site_url}?token=${localStorage.getItem("token")}`}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
className={styles.ctaButton}
|
||||
style={{ marginTop: '1rem' }}
|
||||
>
|
||||
KUNJUNGI
|
||||
</a>
|
||||
)}
|
||||
|
||||
</div>
|
||||
)}
|
||||
{showedModal === 'login' && (
|
||||
<Login postLoginAction={postLoginAction} setPostLoginAction={setPostLoginAction} onClose={() => setShowedModal(null)} />
|
||||
|
||||
Reference in New Issue
Block a user