import { useEffect, useState } from "react"; import styles from "../../styles"; import { colors } from "../../styles"; import TextField from "@mui/material/TextField"; import EditIcon from "@mui/icons-material/Edit"; import Button from "../Button/Button"; import { toast } from "react-toastify"; import { EquipmentInstanceAPI, EquipmentInstanceRemoveAPI, EquipmentInstanceUpdateAPI, } 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, useQueryClient } from "@tanstack/react-query"; import { useQuery } from "@tanstack/react-query"; import { CircularProgress } from "@mui/material"; import React from "react"; export default function EditItemInstanceModal(props: { id: number; setOpen: React.Dispatch>; }) { const queryClient = useQueryClient(); const [item, setItem] = useState({ remarks: "", status: "", }); const [error, setError] = useState(""); const equipment = useQuery({ queryKey: ["equipment_instance", props.id], queryFn: () => EquipmentInstanceAPI(Number(props.id)), }); useEffect(() => { if (equipment.data) { setItem({ ...item, remarks: equipment.data.remarks, status: equipment.data.status, }); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [equipment.data]); const update_mutation = useMutation({ mutationFn: async () => { const data = await EquipmentInstanceUpdateAPI(item, props.id); if (data[0] != true) { return Promise.reject(new Error(JSON.stringify(data[1]))); } return data; }, onSuccess: (data) => { queryClient.invalidateQueries({ queryKey: ["equipment_instances"] }); queryClient.invalidateQueries({ queryKey: ["equipment_instance", props.id], }); setError("Updated successfully"); toast( `Item 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") { setItem({ ...item, remarks: data[1].remarks, status: data[1].status, }); } }, onError: (error) => { setError(JSON.stringify(error)); }, }); const delete_mutation = useMutation({ mutationFn: async () => { const data = await EquipmentInstanceRemoveAPI(props.id); if (data[0] != true) { return Promise.reject(new Error(JSON.stringify(data[1]))); } return data; }, onSuccess: (data) => { queryClient.invalidateQueries({ queryKey: ["equipment_instances"] }); queryClient.invalidateQueries({ queryKey: ["equipment_instance", props.id], }); setError("Deleted successfully"); toast("Item deleted successfuly", { position: "top-right", autoClose: 2000, hideProgressBar: false, closeOnClick: true, pauseOnHover: true, draggable: true, progress: undefined, theme: "light", }); props.setOpen(false); if (typeof data[1] == "object") { setItem({ ...item, remarks: data[1].remarks, status: data[1].status, }); } }, onError: (error) => { setError(JSON.stringify(error)); }, }); if (equipment.isLoading) { return (

Loading

); } return ( <>

Edit Item

Associated SKU:

{equipment.data?.equipment_name} {" (SKU #" + equipment.data?.equipment + ")"}

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}

); }