import React, { useEffect, useState } from '@wordpress/element'; import { Card, CardBody, CardHeader, Dashicon, Flex, Spinner, } from '@wordpress/components'; import { vfAPI } from '../../lib/vfAPI'; import type { Dashboard } from './Dashboard'; const apiUrl = '/stats/dashboard/'; export const DashboardOrders = () => { const minHeightInitial = { minHeight: 'initial' }; const [isBusy, setIsBusy] = useState(true); const [orders, setOrders] = useState(); const toCurrency = (num: number): string => { return num.toLocaleString('en-US', { style: 'currency', currency: 'USD', }); }; const toGrowth = (last: number, prev: number) => { let icon = 'arrow-down-alt'; if (last === prev) { icon = 'arrow-right-alt'; } else if (last > prev) { icon = 'arrow-up-alt'; } let colorClass = 'neutral'; if (last > prev) { colorClass = 'profit'; } else if (last < prev) { colorClass = 'loss'; } if (last && prev) { return (  {Math.round((last / prev - 1) * 100)}% ); } }; const getOrders = () => { setIsBusy(true); vfAPI .get(apiUrl) .then((response) => { if (response.data?.orders) { setOrders(response.data.orders); } setIsBusy(false); }) .catch(() => { setIsBusy(false); }); }; useEffect(() => { getOrders(); }, []); return ( <> {isBusy && ( )} {orders && ( <>

Gross revenue

Today {toCurrency(orders.today_totals)}

{orders.today ? orders.today.toLocaleString() : 'No'}{' '} orders

Last 7 days {toCurrency( orders.last7_totals )} {orders.last7 > 0 && ( {' '} ( {orders.last7.toLocaleString()}{' '} order {orders.last7 > 1 ? 's' : ''} ) )} {toGrowth( orders.last7_totals, orders.prev7_totals )}

Previous 7 days:{' '} {toCurrency(orders.prev7_totals)} {orders.prev7 > 0 && ( {' '} ({orders.prev7.toLocaleString()}{' '} order {orders.prev7 > 1 ? 's' : ''}) )}

Last 30 days {toCurrency( orders.last30_totals )} {orders.last30 > 0 && ( {' '} ( {orders.last30.toLocaleString()}{' '} order {orders.last30 > 1 ? 's' : ''} ) )} {toGrowth( orders.last30_totals, orders.prev30_totals )}

Previous 30 days:{' '} {toCurrency(orders.prev30_totals)} {orders.prev30 > 0 && ( {' '} ( {orders.prev30.toLocaleString()}{' '} order {orders.prev30 > 1 ? 's' : ''}) )}

)} ); };