diff --git a/src/components/pages/ProductsPage.js b/src/components/pages/ProductsPage.js index f0e4189..75cfe0d 100644 --- a/src/components/pages/ProductsPage.js +++ b/src/components/pages/ProductsPage.js @@ -1,8 +1,6 @@ import React, { useState, useEffect } from "react"; -import styles from "../Styles.module.css"; -import { Box, Settings, ShoppingCart } from "lucide-react"; -import { useNavigate } from 'react-router-dom'; - +import { Container, Row, Col, Card, Button, Tabs, Tab, Form } from "react-bootstrap"; +import { useNavigate } from "react-router-dom"; const Dashboard = ({ subscriptions, @@ -13,10 +11,9 @@ const Dashboard = ({ }) => { const navigate = useNavigate(); const [activeTab, setActiveTab] = useState("products"); - const [hoveredCard, setHoveredCard] = useState(null); const [products, setProducts] = useState([]); + const [hoveredCard, setHoveredCard] = useState(null); - // User Settings form state const [settings, setSettings] = useState({ username: "", email: "", @@ -30,76 +27,83 @@ const Dashboard = ({ } }); - useEffect(() => { - if (userData) { - setSettings(userData); + // 🔹 Handle input settings + const handleSettingsChange = (field, value, nested = false) => { + if (nested) { + setSettings((prev) => ({ + ...prev, + profile_data: { ...prev.profile_data, [field]: value } + })); + } else { + setSettings((prev) => ({ ...prev, [field]: value })); } - }, [userData]); + }; + // 🔹 Save profile + const saveSettings = () => { + fetch("https://bot.kediritechnopark.com/webhook-test/user-production/data", { + method: "PUT", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(settings) + }) + .then((res) => res.json()) + .then(() => alert("Settings updated successfully!")) + .catch((err) => alert("Error updating settings: " + err)); + }; + + // 🔹 Group subscriptions + const groupSubscriptionsByProductName = (subs) => { + const result = {}; + subs.forEach((sub) => { + const name = sub.product_name; + const productId = sub.product_id; + if (!result[name]) { + result[name] = { + product_id: productId, + product_name: name, + unit_type: sub.unit_type, + end_date: sub.end_date, + quantity: 0, + subscriptions: [] + }; + } + 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; + + if (sub.unit_type === "token") { + result[name].quantity += sub.quantity ?? 0; + } else { + result[name].quantity += 1; + } + result[name].subscriptions.push(sub); + }); + return result; + }; + + // 🔹 Fetch produk berdasarkan subscription useEffect(() => { if (!subscriptions) return; - function groupSubscriptionsByProductName(subs) { - const result = {}; - subs.forEach((sub) => { - const name = sub.product_name; - const productId = sub.product_id; - if (!result[name]) { - result[name] = { - product_id: productId, - product_name: name, - unit_type: sub.unit_type, - end_date: sub.end_date, - quantity: 0, - subscriptions: [] - }; - } - - 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; - } - - if (sub.unit_type === "token") { - result[name].quantity += sub.quantity ?? 0; - } else { - result[name].quantity += 1; - } - - result[name].subscriptions.push(sub); - }); - - return result; - } - const groupedSubs = groupSubscriptionsByProductName(subscriptions); const productIds = [...new Set(subscriptions.map((s) => s.product_id))]; - fetch( - "https://bot.kediritechnopark.com/webhook/store-production/products", - { - method: "POST", - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify({ itemsId: productIds}) - } - ) + fetch("https://bot.kediritechnopark.com/webhook/store-production/products", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ itemsId: productIds }) + }) .then((res) => res.json()) .then((data) => { - // Step 1: Enrich base products (without children yet) const enrichedData = Object.values(groupedSubs) .filter((group) => data.some((p) => p.id === group.product_id)) .map((group) => { - const productData = data.find((p) => p.id == group.product_id); + const productData = data.find((p) => p.id === group.product_id); let image = productData?.image || ""; let description = productData?.description || ""; let site_url = productData?.site_url || ""; if (!image && productData?.sub_product_of) { - const parent = data.find( - (p) => p.id === productData.sub_product_of - ); + const parent = data.find((p) => p.id === productData.sub_product_of); image = parent?.image || ""; description = parent?.description || ""; site_url = parent?.site_url || ""; @@ -124,301 +128,203 @@ const Dashboard = ({ }; }); - // Step 2: Create a quick lookup table for enrichedData - const productMap = {}; - enrichedData.forEach((p) => { - console.log(p) - productMap[p.name.split("%%%")[1]] = p; - }); - - // Step 3: Find children in API `data` and attach to parents - data - .filter((p) => p.sub_product_of) // only those with a parent - .forEach((child) => { - // ✅ Current logic — attach to the real parent - const parent = productMap[child.sub_product_of]; - if (parent) { - parent.children.push(child); - } - // ➕ New logic — attach to other products with the same sub_product_of - Object.values(productMap).forEach((possibleParent) => { - if ( - possibleParent.id !== child.id && // not itself - possibleParent.sub_product_of === child.sub_product_of // same parent reference - ) { - possibleParent.children.push(child); - } - }); - }); - console.log(enrichedData) setProducts(enrichedData); }) .catch((err) => console.error("Fetch error:", err)); - }, [subscriptions]); - const handleSettingsChange = (field, value, nested = false) => { - if (nested) { - setSettings((prev) => ({ - ...prev, - profile_data: { ...prev.profile_data, [field]: value } - })); - } else { - setSettings((prev) => ({ ...prev, [field]: value })); - } - }; - - const saveSettings = () => { - fetch( - "https://bot.kediritechnopark.com/webhook-test/user-production/data", - { - method: "PUT", - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify(settings) - } - ) - .then((res) => res.json()) - .then(() => { - alert("Settings updated successfully!"); - }) - .catch((err) => alert("Error updating settings: " + err)); - }; - return ( -
- {product.description} -
-Orders list will be displayed here.
-