import React, { useEffect, useState } from "react"; import { ThreeDots } from "react-loader-spinner"; import { getFavourite, getAnalytics } from "../helpers/transactionHelpers.js"; import CircularDiagram from "./CircularDiagram"; import styles from "./Transactions.module.css"; const RoundedRectangle = ({ onClick, bgColor, title, value, percentage, fontSize = "20px", }) => { const containerStyle = { display: "flex", flexDirection: "column", alignItems: "flex-start", justifyContent: "center", width: "calc(100% / 2 - 10px)", maxWidth: "250px", height: "auto", borderRadius: "15px", padding: "20px", margin: "5px", textAlign: "left", fontFamily: "Arial, sans-serif", boxSizing: "border-box", }; const titleStyle = { fontWeight: "bold", marginBottom: "10px", width: "100%", }; const valueAndPercentageContainerStyle = { display: "flex", flexDirection: "row", justifyContent: "space-between", alignItems: "center", width: "100%", }; const valueStyle = { fontSize: fontSize, fontWeight: "bold", flex: "1", textAlign: "left", }; const percentageStyle = { fontSize: "16px", display: "flex", alignItems: "center", textAlign: "right", }; const arrowStyle = { marginLeft: "5px", }; return (
{title}
{value}
0 ? "#007bff" : "red", }} > {percentage} {percentage != undefined && "%"} {percentage != undefined && ( {percentage > 0 ? "↗" : percentage == 0 ? "-" : "↘"} )}
); }; const App = ({ cafeId }) => { const [favouriteItems, setFavouriteItems] = useState([]); const [analytics, setAnalytics] = useState({}); const [loading, setLoading] = useState(true); const [filter, setFilter] = useState("monthly"); // Default filter is 'monthly' const [colors, setColors] = useState([]); const [viewStock, setViewStock] = useState(false); useEffect(() => { const fetchData = async () => { try { setLoading(true); const items = await getFavourite(cafeId); const analyticsData = await getAnalytics(cafeId); if (items) setFavouriteItems(items); if (analyticsData) setAnalytics(analyticsData); console.log(analyticsData); } catch (error) { console.error("Error fetching data:", error); } finally { setLoading(false); } }; fetchData(); }, [cafeId]); useEffect(() => { const getRandomColor = () => { const letters = "0123456789ABCDEF"; let color = "#"; for (let i = 0; i < 6; i++) { color += letters[Math.floor(Math.random() * 16)]; } const r = parseInt(color.substring(1, 3), 16); const g = parseInt(color.substring(3, 5), 16); const b = parseInt(color.substring(5, 7), 16); return `rgba(${r}, ${g}, ${b}, 1)`; }; const colors = favouriteItems[filter]?.map(() => getRandomColor()) || []; setColors(colors); }, [favouriteItems, filter]); if (loading) return (
); const [sold, percentage] = analytics[filter] || [0, 0]; const filteredItems = favouriteItems[filter] || []; const totalSold = filteredItems.reduce((sum, item) => sum + item.sold, 0); const segments = filteredItems.map((item, index) => ({ value: item.sold, color: colors[index] || "#cccccc", })); const formatIncome = (amount) => { if (amount >= 1_000_000_000) { // Format for billions const billions = amount / 1_000_000_000; return billions.toFixed(0) + "b"; // No decimal places for billions } else if (amount >= 1_000_000) { // Format for millions const millions = amount / 1_000_000; return millions.toFixed(2).replace(/\.00$/, "") + "m"; // Two decimal places, remove trailing '.00' } else if (amount >= 1_000) { // Format for thousands const thousands = amount / 1_000; return thousands.toFixed(1).replace(/\.0$/, "") + "k"; // One decimal place, remove trailing '.0' } else { // Less than a thousand return amount.toString(); } }; return (

Reports

{filteredItems[0]?.Item != undefined && ( )} {filteredItems[0]?.Item == undefined && ( )} setViewStock(!viewStock)} // onClick should be a function />
{filteredItems.map((item, index) => (
{item.Item.name}: {item.sold}
))}
); }; export default App;