Commit f147ee7d authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

admin can update sending status

parent a499bc97
...@@ -15,6 +15,26 @@ const AdminOrderController = { ...@@ -15,6 +15,26 @@ const AdminOrderController = {
res.status(403).json({ status: 403, message: "error something" }); res.status(403).json({ status: 403, message: "error something" });
} }
}, },
/**
*
* @param {Request} req
* @param {Response} res
*/
async sending(req, res) {
try {
const { id, sending } = req.body;
if (!id) throw 300;
await db.order.update({
where: { id: Number(id) },
data: { send_status: Number(sending) },
});
await db.$disconnect();
res.json({ status: 301, message: "update sending success" });
} catch (err) {
console.log(err);
res.json({ status: 300, message: "Error with server" });
}
},
}; };
export default AdminOrderController; export default AdminOrderController;
...@@ -18,5 +18,6 @@ adminRouter.put("/category", CategoryController.update); ...@@ -18,5 +18,6 @@ adminRouter.put("/category", CategoryController.update);
adminRouter.delete("/category", CategoryController.delete); adminRouter.delete("/category", CategoryController.delete);
adminRouter.get("/order", AdminOrderController.index); adminRouter.get("/order", AdminOrderController.index);
adminRouter.put("/order/sending", AdminOrderController.sending);
export default adminRouter; export default adminRouter;
...@@ -14,17 +14,41 @@ import { ...@@ -14,17 +14,41 @@ import {
TableRow, TableRow,
TableCell, TableCell,
TableBody, TableBody,
FormControl,
Select,
MenuItem,
} from "@mui/material"; } from "@mui/material";
export default function Order() { export default function Order() {
const user = useContext(UserContext); const user = useContext(UserContext);
const adminOrder = useContext(AdminOrderContext); const adminOrder = useContext(AdminOrderContext);
const [message, setMessage] = useState({ message: "", error: false }); const [message, setMessage] = useState({ message: "", error: false });
useEffect(() => { const onSendingChange = async (id, sending) => {
console.log(adminOrder.value); try {
}, [user]); let response = await axios.put(
"/api/admin/order/sending",
{ id, sending },
{ headers: { token: user.value?.token } }
);
if (response.data.status === 301) {
setMessage({ message: "อัพเดทสถานะการจัดส่งสำเร็จ", error: false });
setTimeout(() => {
setMessage({ message: "", error: false });
}, 2000);
adminOrder.fetch();
}
} catch (err) {
console.error(err);
setMessage({
message: "เกิดข้อผิดพลาด กรุณาลองใหม่อีกครั้ง",
error: true,
});
setTimeout(() => {
setMessage({ message: "", error: true });
}, 2000);
}
};
return ( return (
<> <>
...@@ -76,17 +100,33 @@ export default function Order() { ...@@ -76,17 +100,33 @@ export default function Order() {
<TableCell>{order.shipping_price}</TableCell> <TableCell>{order.shipping_price}</TableCell>
<TableCell> <TableCell>
<Box color={order.pay_status ? "green" : "red"}> <Box color={order.pay_status ? "green" : "red"}>
{order.pay_status ? ( {order.pay_status
"ชำระเงินแล้ว" ? "ชำระเงินแล้ว"
) : ( : "ยังไม่ชำระเงิน"}
"ยังไม่ชำระเงิน"
)}
</Box> </Box>
</TableCell> </TableCell>
<TableCell> <TableCell>
<Box color={order.send_status ? "green" : "red"}> <FormControl variant="standard">
{order.send_status ? "ส่งแล้ว" : "ยังไม่จัดส่ง"} <Select
</Box> value={Number(order.send_status)}
onChange={(e) => {
onSendingChange(
Number(order.id),
Number(e.target.value)
);
}}
>
<MenuItem value={0}>
<Box color="red">ยังไม่จัดส่ง</Box>
</MenuItem>
<MenuItem value={1}>
<Box color="orangered">กำลังจัดส่ง</Box>
</MenuItem>
<MenuItem value={2}>
<Box color="green">จัดส่งแล้ว</Box>
</MenuItem>
</Select>
</FormControl>
</TableCell> </TableCell>
</TableRow> </TableRow>
) )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment