Update Fix
This commit is contained in:
370
resources/js/admin/tailadmin/pages/Dashboard/Home.tsx
Normal file
370
resources/js/admin/tailadmin/pages/Dashboard/Home.tsx
Normal file
@@ -0,0 +1,370 @@
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import PageMeta from "../../components/common/PageMeta";
|
||||
import { apiGet } from "../../lib/api";
|
||||
import { useToastOnError } from "../../hooks/useToastOnError";
|
||||
|
||||
type Summary = {
|
||||
pendapatan: number;
|
||||
transaksi: number;
|
||||
item_terjual: number;
|
||||
margin: number | null;
|
||||
};
|
||||
|
||||
type ChartRow = { tanggal: string; total: number };
|
||||
type TopProduct = { id: number; nama: string; barcode: string; qty: number; total: number };
|
||||
type LowStockRow = { id: number; nama: string; barcode: string | null; sku: string | null; stok: number };
|
||||
type RecentRow = { id: number; nomor_penjualan: string; waktu: string; total: number; metode_pembayaran: string };
|
||||
|
||||
type Overview = {
|
||||
date: string;
|
||||
summary: Summary;
|
||||
chart_7d: ChartRow[];
|
||||
top_products: TopProduct[];
|
||||
low_stock_toko: {
|
||||
lokasi: { id: number; kode: string; nama: string } | null;
|
||||
threshold: number;
|
||||
data: LowStockRow[];
|
||||
};
|
||||
low_stock_gudang: {
|
||||
lokasi: { id: number; kode: string; nama: string } | null;
|
||||
threshold: number;
|
||||
data: LowStockRow[];
|
||||
};
|
||||
recent: RecentRow[];
|
||||
data_issues: { tanpa_barcode: number; tanpa_harga_jual: number };
|
||||
};
|
||||
|
||||
export default function Home() {
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [data, setData] = useState<Overview | null>(null);
|
||||
const [stokTab, setStokTab] = useState<"toko" | "gudang">("toko");
|
||||
|
||||
useToastOnError(error);
|
||||
|
||||
const load = async () => {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const res = await apiGet<Overview>("/admin-api/dashboard/overview");
|
||||
setData(res);
|
||||
} catch (e: any) {
|
||||
setError(e?.message || "Gagal memuat dashboard");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
load();
|
||||
}, []);
|
||||
|
||||
const maxChart = useMemo(() => {
|
||||
if (!data?.chart_7d?.length) return 1;
|
||||
return Math.max(...data.chart_7d.map((r) => r.total), 1);
|
||||
}, [data]);
|
||||
|
||||
return (
|
||||
<>
|
||||
<PageMeta title="Dashboard POS" description="Ringkasan operasional POS" />
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<h1 className="text-2xl font-semibold text-gray-800 dark:text-white/90">
|
||||
Dashboard
|
||||
</h1>
|
||||
<button
|
||||
type="button"
|
||||
onClick={load}
|
||||
className="h-9 rounded-xl border border-gray-200 bg-white px-4 text-sm font-semibold hover:bg-gray-50 dark:border-gray-800 dark:bg-gray-900"
|
||||
>
|
||||
Refresh
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="rounded-xl bg-red-50 px-4 py-3 text-sm text-red-700 dark:bg-red-500/10 dark:text-red-300">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{loading || !data ? (
|
||||
<div className="rounded-2xl border border-gray-200 bg-white p-5 text-sm text-gray-600 shadow-sm dark:border-gray-800 dark:bg-gray-900 dark:text-gray-400">
|
||||
Memuat dashboard...
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
{(() => {
|
||||
const showMargin = data.summary.margin !== null;
|
||||
return (
|
||||
<div
|
||||
className={`grid gap-3 sm:grid-cols-2 ${
|
||||
showMargin ? "lg:grid-cols-4" : "lg:grid-cols-3"
|
||||
}`}
|
||||
>
|
||||
<div className="rounded-2xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-800 dark:bg-gray-900">
|
||||
<div className="text-xs text-gray-500">Pendapatan Hari Ini</div>
|
||||
<div className="mt-1 text-xl font-semibold text-gray-800 dark:text-white/90">
|
||||
Rp {data.summary.pendapatan.toLocaleString("id-ID")}
|
||||
</div>
|
||||
</div>
|
||||
<div className="rounded-2xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-800 dark:bg-gray-900">
|
||||
<div className="text-xs text-gray-500">Transaksi</div>
|
||||
<div className="mt-1 text-xl font-semibold text-gray-800 dark:text-white/90">
|
||||
{data.summary.transaksi}
|
||||
</div>
|
||||
</div>
|
||||
<div className="rounded-2xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-800 dark:bg-gray-900">
|
||||
<div className="text-xs text-gray-500">Item Terjual</div>
|
||||
<div className="mt-1 text-xl font-semibold text-gray-800 dark:text-white/90">
|
||||
{data.summary.item_terjual}
|
||||
</div>
|
||||
</div>
|
||||
{showMargin && (
|
||||
<div className="rounded-2xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-800 dark:bg-gray-900">
|
||||
<div className="text-xs text-gray-500">Margin Kotor</div>
|
||||
<div className="mt-1 text-xl font-semibold text-gray-800 dark:text-white/90">
|
||||
Rp {(data.summary.margin || 0).toLocaleString("id-ID")}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
})()}
|
||||
|
||||
<div className="grid gap-4 lg:grid-cols-2">
|
||||
<div className="rounded-2xl border border-gray-200 bg-white p-5 shadow-sm dark:border-gray-800 dark:bg-gray-900">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="text-sm font-semibold text-gray-800 dark:text-white/90">
|
||||
Penjualan 7 Hari Terakhir
|
||||
</div>
|
||||
<div className="text-xs text-gray-500">{data.date}</div>
|
||||
</div>
|
||||
<div className="mt-4 flex items-end gap-2">
|
||||
{data.chart_7d.map((r) => (
|
||||
<div key={r.tanggal} className="flex flex-1 flex-col items-center gap-1">
|
||||
<div
|
||||
className="w-full rounded-lg bg-blue-500/80"
|
||||
style={{ height: `${Math.max(8, (r.total / maxChart) * 140)}px` }}
|
||||
title={`Rp ${r.total.toLocaleString("id-ID")}`}
|
||||
/>
|
||||
<div className="text-[10px] text-gray-500">
|
||||
{r.tanggal.slice(5)}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-2xl border border-gray-200 bg-white p-5 shadow-sm dark:border-gray-800 dark:bg-gray-900">
|
||||
<div className="text-sm font-semibold text-gray-800 dark:text-white/90">
|
||||
Perhatian Data
|
||||
</div>
|
||||
<div className="mt-4 grid gap-3 sm:grid-cols-2">
|
||||
<div className="rounded-xl border border-gray-200 p-3 text-sm dark:border-gray-800">
|
||||
<div className="text-xs text-gray-500">Produk tanpa barcode</div>
|
||||
<div className="mt-1 text-lg font-semibold text-gray-800 dark:text-white/90">
|
||||
{data.data_issues.tanpa_barcode}
|
||||
</div>
|
||||
</div>
|
||||
<div className="rounded-xl border border-gray-200 p-3 text-sm dark:border-gray-800">
|
||||
<div className="text-xs text-gray-500">Produk tanpa harga jual</div>
|
||||
<div className="mt-1 text-lg font-semibold text-gray-800 dark:text-white/90">
|
||||
{data.data_issues.tanpa_harga_jual}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid gap-4 lg:grid-cols-2">
|
||||
<div className="rounded-2xl border border-gray-200 bg-white p-5 shadow-sm dark:border-gray-800 dark:bg-gray-900">
|
||||
<div className="text-sm font-semibold text-gray-800 dark:text-white/90">
|
||||
Produk Terlaris Hari Ini
|
||||
</div>
|
||||
<div className="mt-4 overflow-x-auto">
|
||||
<table className="min-w-full text-sm">
|
||||
<thead>
|
||||
<tr className="text-left text-gray-500 dark:text-gray-400">
|
||||
<th className="py-2 pr-4">Nama</th>
|
||||
<th className="py-2 pr-4">Barcode</th>
|
||||
<th className="py-2 pr-4">Qty</th>
|
||||
<th className="py-2 pr-4">Total</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{data.top_products.map((r) => (
|
||||
<tr key={r.id} className="border-t border-gray-100 dark:border-gray-800">
|
||||
<td className="py-2 pr-4 text-gray-800 dark:text-white/90">
|
||||
{r.nama}
|
||||
</td>
|
||||
<td className="py-2 pr-4 text-gray-500 dark:text-gray-400">
|
||||
{r.barcode}
|
||||
</td>
|
||||
<td className="py-2 pr-4 text-gray-700 dark:text-gray-200">
|
||||
{r.qty}
|
||||
</td>
|
||||
<td className="py-2 pr-4 text-gray-800 dark:text-white/90">
|
||||
Rp {r.total.toLocaleString("id-ID")}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
{data.top_products.length === 0 && (
|
||||
<tr>
|
||||
<td
|
||||
colSpan={4}
|
||||
className="py-6 text-center text-gray-500 dark:text-gray-400"
|
||||
>
|
||||
Belum ada penjualan hari ini.
|
||||
</td>
|
||||
</tr>
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-2xl border border-gray-200 bg-white p-5 shadow-sm dark:border-gray-800 dark:bg-gray-900">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="text-sm font-semibold text-gray-800 dark:text-white/90">
|
||||
Stok Rendah
|
||||
{stokTab === "toko" && data.low_stock_toko.lokasi ? (
|
||||
<span className="ml-2 text-xs text-gray-500">
|
||||
({data.low_stock_toko.lokasi.kode})
|
||||
</span>
|
||||
) : null}
|
||||
{stokTab === "gudang" && data.low_stock_gudang.lokasi ? (
|
||||
<span className="ml-2 text-xs text-gray-500">
|
||||
({data.low_stock_gudang.lokasi.kode})
|
||||
</span>
|
||||
) : null}
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setStokTab("toko")}
|
||||
className={`h-8 rounded-lg px-3 text-xs font-semibold ${
|
||||
stokTab === "toko"
|
||||
? "bg-blue-600 text-white"
|
||||
: "border border-gray-200 bg-white text-gray-600 hover:bg-gray-50 dark:border-gray-800 dark:bg-gray-900 dark:text-gray-300"
|
||||
}`}
|
||||
>
|
||||
Toko
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setStokTab("gudang")}
|
||||
className={`h-8 rounded-lg px-3 text-xs font-semibold ${
|
||||
stokTab === "gudang"
|
||||
? "bg-blue-600 text-white"
|
||||
: "border border-gray-200 bg-white text-gray-600 hover:bg-gray-50 dark:border-gray-800 dark:bg-gray-900 dark:text-gray-300"
|
||||
}`}
|
||||
>
|
||||
Gudang
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-4 overflow-x-auto">
|
||||
<table className="min-w-full text-sm">
|
||||
<thead>
|
||||
<tr className="text-left text-gray-500 dark:text-gray-400">
|
||||
<th className="py-2 pr-4">Nama</th>
|
||||
<th className="py-2 pr-4">SKU</th>
|
||||
<th className="py-2 pr-4">Stok</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{(stokTab === "toko"
|
||||
? data.low_stock_toko.data
|
||||
: data.low_stock_gudang.data
|
||||
).map((r) => (
|
||||
<tr key={r.id} className="border-t border-gray-100 dark:border-gray-800">
|
||||
<td className="py-2 pr-4 text-gray-800 dark:text-white/90">
|
||||
{r.nama}
|
||||
</td>
|
||||
<td className="py-2 pr-4 text-gray-500 dark:text-gray-400">
|
||||
{r.sku || "-"}
|
||||
</td>
|
||||
<td className="py-2 pr-4">
|
||||
<span className="inline-flex rounded-full bg-red-50 px-3 py-1 text-xs font-semibold text-red-700 dark:bg-red-500/10 dark:text-red-300">
|
||||
{r.stok}
|
||||
</span>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
{(stokTab === "toko"
|
||||
? data.low_stock_toko.data
|
||||
: data.low_stock_gudang.data
|
||||
).length === 0 && (
|
||||
<tr>
|
||||
<td
|
||||
colSpan={3}
|
||||
className="py-6 text-center text-gray-500 dark:text-gray-400"
|
||||
>
|
||||
Tidak ada stok rendah.
|
||||
</td>
|
||||
</tr>
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div className="mt-2 text-xs text-gray-500">
|
||||
Ambang batas:{" "}
|
||||
{stokTab === "toko"
|
||||
? data.low_stock_toko.threshold
|
||||
: data.low_stock_gudang.threshold}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-2xl border border-gray-200 bg-white p-5 shadow-sm dark:border-gray-800 dark:bg-gray-900">
|
||||
<div className="text-sm font-semibold text-gray-800 dark:text-white/90">
|
||||
Transaksi Terakhir
|
||||
</div>
|
||||
<div className="mt-4 overflow-x-auto">
|
||||
<table className="min-w-full text-sm">
|
||||
<thead>
|
||||
<tr className="text-left text-gray-500 dark:text-gray-400">
|
||||
<th className="py-2 pr-4">Waktu</th>
|
||||
<th className="py-2 pr-4">No</th>
|
||||
<th className="py-2 pr-4">Total</th>
|
||||
<th className="py-2 pr-4">Metode</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{data.recent.map((r) => (
|
||||
<tr key={r.id} className="border-t border-gray-100 dark:border-gray-800">
|
||||
<td className="py-2 pr-4 text-gray-500 dark:text-gray-400">
|
||||
{new Date(r.waktu).toLocaleString("id-ID")}
|
||||
</td>
|
||||
<td className="py-2 pr-4 text-gray-800 dark:text-white/90">
|
||||
{r.nomor_penjualan}
|
||||
</td>
|
||||
<td className="py-2 pr-4 text-gray-800 dark:text-white/90">
|
||||
Rp {r.total.toLocaleString("id-ID")}
|
||||
</td>
|
||||
<td className="py-2 pr-4 text-gray-600 dark:text-gray-300">
|
||||
{r.metode_pembayaran}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
{data.recent.length === 0 && (
|
||||
<tr>
|
||||
<td
|
||||
colSpan={4}
|
||||
className="py-6 text-center text-gray-500 dark:text-gray-400"
|
||||
>
|
||||
Belum ada transaksi.
|
||||
</td>
|
||||
</tr>
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user