From fd7891359c71961a4e21ba9d8f88c514fdd543df Mon Sep 17 00:00:00 2001 From: keannu125 Date: Tue, 7 Mar 2023 19:41:28 +0800 Subject: [PATCH] Fixed dashboard loading and error pages not showing properly and made lowest stock widget functional --- src/Components/Api/Api.tsx | 13 +++++++++++++ src/Routes/Dashboard/Dashboard.tsx | 24 +++++++++++++++++------- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/Components/Api/Api.tsx b/src/Components/Api/Api.tsx index 6ff7299..bcfc084 100644 --- a/src/Components/Api/Api.tsx +++ b/src/Components/Api/Api.tsx @@ -57,6 +57,19 @@ export function UpdateProduct(product: UpdateProductParams) { }); } +export function GetLowestStockedProduct() { + const token = JSON.parse(localStorage.getItem("token") || "{}"); + return axios + .get("http://localhost:8000/api/v1/lowest_stock_product/", { + headers: { + Authorization: "Token " + token, + }, + }) + .then((response) => { + return response.data; + }); +} + export function GetLogs() { const token = JSON.parse(localStorage.getItem("token") || "{}"); return axios diff --git a/src/Routes/Dashboard/Dashboard.tsx b/src/Routes/Dashboard/Dashboard.tsx index 4cc1474..12e7f3a 100644 --- a/src/Routes/Dashboard/Dashboard.tsx +++ b/src/Routes/Dashboard/Dashboard.tsx @@ -10,13 +10,24 @@ import ColoredCube from "../../Components/ColoredCube/ColoredCube"; import RecentlyAddedIcon from "../../Components/Icons/RecentlyAddedIcon/RecentlyAddedIcon"; import LoginChecker from "../../Components/LoginChecker/LoginChecker"; import { useQuery } from "react-query"; -import { GetLogs, GetProducts } from "../../Components/Api/Api"; +import { + GetLogs, + GetLowestStockedProduct, + GetProducts, +} from "../../Components/Api/Api"; import { ProductLog } from "../../Interfaces/Interfaces"; export default function Dashboard() { const logs = useQuery("logs", GetLogs, { retry: 0 }); const products = useQuery("products", GetProducts, { retry: 0 }); - if (logs.isLoading && products.isLoading) { + const lowest_stock_product = useQuery( + "lowest_stock_product", + GetLowestStockedProduct, + { + retry: 0, + } + ); + if (logs.isLoading || products.isLoading || lowest_stock_product.isLoading) { return (
@@ -32,7 +43,7 @@ export default function Dashboard() {
); } - if (logs.error || products.error) { + if (logs.error || products.error || lowest_stock_product.isError) {
@@ -125,14 +136,14 @@ export default function Dashboard() {

- Low Stock + Lowest Stock

- Canned Pagmamahal + {lowest_stock_product.data[0].name}

- In Stock: 3 + In Stock: {lowest_stock_product.data[0].quantity}

{logs.data.slice(0, 5).map((log: ProductLog, index: number) => { - console.log(log); return (