diff --git a/src/Components/API/API.tsx b/src/Components/API/API.tsx index d033501..59131c7 100644 --- a/src/Components/API/API.tsx +++ b/src/Components/API/API.tsx @@ -17,6 +17,8 @@ import { EquipmentInstanceLogListType, UserType, TransactionListType, + TransactionUpdateType, + TransactionType, } from "../Types/Types"; const debug = true; @@ -356,3 +358,31 @@ export async function TransactionsAPI() { console.log("Error retrieving transactions"); }); } + +export async function TransactionAPI(id: number) { + const config = await GetConfig(); + return instance + .get(`api/v1/transactions/${id}/`, config) + .then((response) => { + return response.data as TransactionType; + }) + .catch(() => { + console.log("Error retrieving transaction"); + }); +} + +export async function TransactionUpdateAPI( + transaction: TransactionUpdateType, + id: number +) { + const config = await GetConfig(); + return instance + .patch(`api/v1/transactions/${id}/`, transaction, config) + .then((response) => { + return [true, response.data as TransactionType]; + }) + .catch((error) => { + console.log("Error updating transaction"); + return [false, ParseError(error)]; + }); +} diff --git a/src/Components/EditItemInstanceModal/EditItemInstanceModal.tsx b/src/Components/EditItemInstanceModal/EditItemInstanceModal.tsx index d582179..c093ebd 100644 --- a/src/Components/EditItemInstanceModal/EditItemInstanceModal.tsx +++ b/src/Components/EditItemInstanceModal/EditItemInstanceModal.tsx @@ -51,6 +51,7 @@ export default function EditItemInstanceModal(props: { mutationFn: async () => { const data = await EquipmentInstanceUpdateAPI(item, props.id); if (data[0] != true) { + setError(JSON.stringify(data[1])); return Promise.reject(new Error(JSON.stringify(data[1]))); } return data; @@ -84,9 +85,6 @@ export default function EditItemInstanceModal(props: { }); } }, - onError: (error) => { - setError(JSON.stringify(error)); - }, }); const delete_mutation = useMutation({ diff --git a/src/Components/EditSKUModal/EditSKUModal.tsx b/src/Components/EditSKUModal/EditSKUModal.tsx index 363f19a..098e201 100644 --- a/src/Components/EditSKUModal/EditSKUModal.tsx +++ b/src/Components/EditSKUModal/EditSKUModal.tsx @@ -53,6 +53,7 @@ export default function EditSKUModal(props: { mutationFn: async () => { const data = await EquipmentUpdateAPI(item, props.id); if (data[0] != true) { + setError(JSON.stringify(data[1])); return Promise.reject(new Error(JSON.stringify(data[1]))); } return data; @@ -87,9 +88,6 @@ export default function EditSKUModal(props: { }); } }, - onError: (error) => { - setError(JSON.stringify(error)); - }, }); const delete_mutation = useMutation({ diff --git a/src/Components/EditTransactionModal/EditTransactionModal.tsx b/src/Components/EditTransactionModal/EditTransactionModal.tsx new file mode 100644 index 0000000..30bbcf2 --- /dev/null +++ b/src/Components/EditTransactionModal/EditTransactionModal.tsx @@ -0,0 +1,216 @@ +import { useEffect, useState } from "react"; +import styles from "../../styles"; +import { colors } from "../../styles"; +import EditIcon from "@mui/icons-material/Edit"; +import Button from "../Button/Button"; +import { toast } from "react-toastify"; +import { TransactionAPI, TransactionUpdateAPI } 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 { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; +import { CircularProgress } from "@mui/material"; +import React from "react"; + +export default function EditTransactionModal(props: { + id: number; + setOpen: React.Dispatch>; +}) { + const queryClient = useQueryClient(); + const [status, setStatus] = useState(""); + const [error, setError] = useState(""); + const transaction = useQuery({ + queryKey: ["transaction", props.id], + queryFn: () => TransactionAPI(Number(props.id)), + }); + const update_mutation = useMutation({ + mutationFn: async () => { + const data = await TransactionUpdateAPI( + { transaction_status: status }, + props.id + ); + if (data[0] != true) { + setError(JSON.stringify(data[1])); + return Promise.reject(new Error(JSON.stringify(data[1]))); + } + return data; + }, + onSuccess: (data) => { + queryClient.invalidateQueries({ + queryKey: ["transactions"], + }); + queryClient.invalidateQueries({ + queryKey: ["transactions", props.id], + }); + setError("Updated successfully"); + toast( + `Transaction updated successfuly, ${ + typeof data[1] == "object" ? "ID:" + data[1].id : "" + }`, + { + position: "top-right", + autoClose: 2000, + hideProgressBar: false, + closeOnClick: true, + pauseOnHover: true, + draggable: true, + progress: undefined, + theme: "light", + } + ); + if (typeof data[1] == "object") { + setStatus(data[1].transaction_status); + } + }, + }); + + useEffect(() => { + if (transaction.data) { + setStatus(transaction.data.transaction_status); + } + }, [transaction.data]); + if (transaction.isLoading) { + return ( +
+ +

+ Loading +

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

+ Update Transaction +

+
+ +
+ + + Item Status + + ) => { + setStatus(e.target.value); + setError(""); + }} + > +
+ } + label="Pending Approval" + style={styles.text_dark} + /> + } + label="Approved" + style={styles.text_dark} + /> + } + label="Rejected" + style={styles.text_dark} + /> + } + label="Cancelled" + style={styles.text_dark} + /> + } + label="Borrowed" + style={styles.text_dark} + /> + } + label="Returned: Pending Checking" + style={styles.text_dark} + /> + } + label="With Breakages: Pending Resolution" + style={styles.text_dark} + /> + } + label="Finalized" + style={styles.text_dark} + /> +
+
+
+
+

{error}

+
+