From 201822b271f04fa0898b74f85ee129e19ceb0449 Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Sat, 2 Dec 2023 21:31:15 +0800 Subject: [PATCH] Made SKU and item adding fully functional --- src/Components/API/API.tsx | 21 +- src/Components/AddItemModal/AddItemModal.tsx | 235 +++++++++++++++++++ src/Components/AddSKUModal/AddSKUModal.tsx | 34 +-- src/Components/Types/Types.tsx | 6 + src/Pages/DashboardPage/DashboardPage.tsx | 26 +- 5 files changed, 304 insertions(+), 18 deletions(-) create mode 100644 src/Components/AddItemModal/AddItemModal.tsx diff --git a/src/Components/API/API.tsx b/src/Components/API/API.tsx index 63d6866..667ad12 100644 --- a/src/Components/API/API.tsx +++ b/src/Components/API/API.tsx @@ -9,6 +9,8 @@ import { EquipmentInstanceListType, EquipmentType, AddEquipmentType, + AddEquipmentInstanceType, + EquipmentInstanceType, } from "../Types/Types"; const instance = axios.create({ @@ -56,7 +58,7 @@ export function ParseError(error: { response: { data: string } }) { .replace(/\(/g, " ") .replace(/\)/g, " ") .replace(/"/g, " ") - .replace(/,/g, " ") + .replace(/,/g, ",") .replace(/\[/g, "") .replace(/\]/g, "") .replace(/\./g, "") @@ -186,6 +188,8 @@ export async function EquipmentCreateAPI(equipment: AddEquipmentType) { }); } +// Equipment Instances APIs + export async function EquipmentInstancesAPI() { const config = await GetConfig(); return instance @@ -197,3 +201,18 @@ export async function EquipmentInstancesAPI() { console.log("Error retrieving equipments"); }); } + +export async function EquipmentInstanceCreateAPI( + equipment_instance: AddEquipmentInstanceType +) { + const config = await GetConfig(); + return instance + .post("api/v1/equipments/equipment_instances/", equipment_instance, config) + .then((response) => { + return [true, response.data as EquipmentInstanceType]; + }) + .catch((error) => { + console.log("Error creating equipment instance"); + return [false, ParseError(error)]; + }); +} diff --git a/src/Components/AddItemModal/AddItemModal.tsx b/src/Components/AddItemModal/AddItemModal.tsx new file mode 100644 index 0000000..76ea5b6 --- /dev/null +++ b/src/Components/AddItemModal/AddItemModal.tsx @@ -0,0 +1,235 @@ +import { useEffect, useState } from "react"; +import styles from "../../styles"; +import { colors } from "../../styles"; +import TextField from "@mui/material/TextField"; +import AddToQueueIcon from "@mui/icons-material/AddToQueue"; +import Button from "../Button/Button"; +import { toast } from "react-toastify"; +import { EquipmentInstanceCreateAPI, EquipmentsAPI } from "../API/API"; +import RadioGroup from "@mui/material/RadioGroup"; +import FormControlLabel from "@mui/material/FormControlLabel"; +import FormControl from "@mui/material/FormControl"; +import FormLabel from "@mui/material/FormLabel"; +import Radio from "@mui/material/Radio"; +import { useQueryClient } from "@tanstack/react-query"; +import { useQuery } from "@tanstack/react-query"; +import { CircularProgress } from "@mui/material"; +import React from "react"; + +export default function AddItemModal() { + const queryClient = useQueryClient(); + const [item, setItem] = useState({ + equipment: 0, + remarks: "", + status: "WORKING", + }); + const [error, setError] = useState(""); + + const equipments = useQuery({ + queryKey: ["equipments"], + queryFn: EquipmentsAPI, + }); + + useEffect(() => { + if (equipments.data) { + setItem({ ...item, equipment: equipments.data[0].id }); + } + }, [equipments.data, item]); + if (equipments.isLoading) { + return ( +
+ +

+ Loading +

+
+ ); + } + return ( + <> +
+ +

Add Item

+
+ +
+ + + Select Associated SKU + + ) => { + setItem({ ...item, equipment: Number(e.target.value) }); + setError(""); + }} + > +
+ {equipments.data ? ( + equipments.data.map((equipment) => ( + + } + label={equipment.name} + style={styles.text_dark} + /> + + )) + ) : ( + <> + )} +
+
+
+ + + Item Status + + ) => { + setItem({ ...item, status: e.target.value }); + setError(""); + }} + > +
+ } + label="Working" + style={styles.text_dark} + /> + } + label="Broken" + style={styles.text_dark} + /> + } + label="Under Maintenance" + style={styles.text_dark} + /> + } + label="Decomissioned" + style={styles.text_dark} + /> +
+
+
+ ) => { + setItem({ ...item, remarks: e.target.value }); + setError(""); + }} + value={item.remarks} + placeholder={"Optionally add a brief description of the item"} + /> +
+

{error}

+
+