diff --git a/src/App.tsx b/src/App.tsx index c32ec4b..483aec2 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -11,7 +11,10 @@ import Inventory from "./Routes/Inventory/Inventory"; import Login from "./Routes/Login/Login"; import Product from "./Routes/Product/Product"; import Activation from "./Routes/Activation/Activation"; -import AddProduct from "./Routes/AddProduct/AddProduct"; +import { QueryClient, QueryClientProvider } from "react-query"; +import NewProduct from "./Routes/NewProduct/NewProduct"; + +const queryClient = new QueryClient(); const router = createBrowserRouter([ { @@ -76,10 +79,10 @@ const router = createBrowserRouter([ ), }, { - path: "/AddProduct", + path: "/NewProduct", element: ( - + ), }, @@ -88,7 +91,9 @@ const router = createBrowserRouter([ export default function App() { return ( - + + + ); } diff --git a/src/Components/Api/Api.tsx b/src/Components/Api/Api.tsx index bddfe1f..23696c3 100644 --- a/src/Components/Api/Api.tsx +++ b/src/Components/Api/Api.tsx @@ -6,11 +6,7 @@ import { LoginParams, RegistrationParams, } from "../../Interfaces/Interfaces"; -import { useDispatch } from "react-redux"; -import { SetUser } from "../../Features/Redux/Slices/LoggedInUserSlice/LoggedInUserSlice"; -import { toggle_login } from "../../Features/Redux/Slices/Login/LoginSlice"; - -// Note APIs +// Product APIs export function GetProducts() { const token = JSON.parse(localStorage.getItem("token") || "{}"); @@ -25,7 +21,7 @@ export function GetProducts() { }); } -export function GetNote(id: number) { +export function GetProduct(id: number) { const token = JSON.parse(localStorage.getItem("token") || "{}"); return axios .get("http://localhost:8000/api/v1/products/" + id + "/", { @@ -156,7 +152,8 @@ export async function CheckSavedSession() { console.log("Previous session found"); return true; } else { - console.log("Previous session found but expired"); + console.log("Previous session found but expired. Clearing token"); + localStorage.removeItem("token"); return false; } } diff --git a/src/Interfaces/Interfaces.tsx b/src/Interfaces/Interfaces.tsx index a95cf5e..403b08f 100644 --- a/src/Interfaces/Interfaces.tsx +++ b/src/Interfaces/Interfaces.tsx @@ -52,7 +52,6 @@ export interface ActivationParams { export interface AddProductParams { name: string; - quantity: string; } export interface UpdateProductParams { diff --git a/src/Routes/AddProduct/AddProduct.tsx b/src/Routes/AddProduct/AddProduct.tsx deleted file mode 100644 index 6e2c15e..0000000 --- a/src/Routes/AddProduct/AddProduct.tsx +++ /dev/null @@ -1,44 +0,0 @@ -import React, { useEffect } from "react"; -import styles from "../../styles"; -import { useNavigate } from "react-router-dom"; -import ProductsIcon from "../../Components/Icons/ProductsIcon/ProductsIcon"; -import AddIcon from "../../Components/Icons/AddIcon/AddIcon"; -import { Button } from "@mui/material"; -import { Navigate } from "react-router-dom"; -import { LoginState } from "../../Interfaces/Interfaces"; -import { useSelector } from "react-redux"; - -export default function AddProduct() { - const navigate = useNavigate(); - const logged_in = useSelector((state: LoginState) => state.logged_in.value); - if (!logged_in) { - return ; - } - return ( -
-
-
-
- -

- Add Product -

-
-
-
-
- -
- ); -} diff --git a/src/Routes/NewProduct/NewProduct.tsx b/src/Routes/NewProduct/NewProduct.tsx new file mode 100644 index 0000000..7ca4c90 --- /dev/null +++ b/src/Routes/NewProduct/NewProduct.tsx @@ -0,0 +1,76 @@ +import React, { useEffect, useState } from "react"; +import styles from "../../styles"; +import { useNavigate } from "react-router-dom"; +import AddIcon from "../../Components/Icons/AddIcon/AddIcon"; +import { Button } from "@mui/material"; +import { Navigate } from "react-router-dom"; +import { LoginState } from "../../Interfaces/Interfaces"; +import { useSelector } from "react-redux"; +import { AddProduct } from "../../Components/Api/Api"; +import { useMutation, useQueryClient } from "react-query"; + +export default function NewProduct() { + const navigate = useNavigate(); + const logged_in = useSelector((state: LoginState) => state.logged_in.value); + const [product, setProduct] = useState(""); + const queryClient = useQueryClient(); + const mutation = useMutation({ + mutationFn: AddProduct, + onSuccess: () => { + queryClient.invalidateQueries("products"); + }, + }); + if (!logged_in) { + return ; + } + return ( +
+
+
+ +

+ Add Product +

+
+
+
+
+
+

+ Product Name +

+ { + setProduct(e.target.value); + }} + maxLength={20} + /> +
+
+ +
+
+
+ ); +} diff --git a/src/Routes/Products/Products.tsx b/src/Routes/Products/Products.tsx index 9c37588..27af1d0 100644 --- a/src/Routes/Products/Products.tsx +++ b/src/Routes/Products/Products.tsx @@ -1,4 +1,4 @@ -import React, { useEffect } from "react"; +import React from "react"; import styles from "../../styles"; import { useNavigate } from "react-router-dom"; import ProductsIcon from "../../Components/Icons/ProductsIcon/ProductsIcon"; @@ -9,13 +9,57 @@ import ViewManager from "../../Components/ProductsPage/ViewManager"; import { Navigate } from "react-router-dom"; import { LoginState } from "../../Interfaces/Interfaces"; import { useSelector } from "react-redux"; +import { useQuery } from "react-query"; +import { GetProducts } from "../../Components/Api/Api"; export default function Products() { const navigate = useNavigate(); const logged_in = useSelector((state: LoginState) => state.logged_in.value); + const { + data: products, + isLoading, + error, + } = useQuery("products", GetProducts, { retry: 0 }); if (!logged_in) { return ; } + if (isLoading) { + return ( +
+
+
+
+ +

+ Products +

+
+
+

+ Loading Products... +

+
+
+ ); + } else if (error) { + return ( +
+
+
+
+ +

+ Products +

+
+
+

+ Error loading products +

+
+
+ ); + } return (
@@ -33,7 +77,7 @@ export default function Products() { }} >
- + ); } diff --git a/src/styles.tsx b/src/styles.tsx index 5331d0e..1031cfd 100644 --- a/src/styles.tsx +++ b/src/styles.tsx @@ -152,6 +152,7 @@ const styles: { [key: string]: React.CSSProperties } = { }, content_center: { display: "flex", + flexDirection: "column", justifyContent: "center", textAlign: "center", },