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}