Commit f147ee7d authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

admin can update sending status

parent a499bc97
......@@ -15,6 +15,26 @@ const AdminOrderController = {
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;
......@@ -18,5 +18,6 @@ adminRouter.put("/category", CategoryController.update);
adminRouter.delete("/category", CategoryController.delete);
adminRouter.get("/order", AdminOrderController.index);
adminRouter.put("/order/sending", AdminOrderController.sending);
export default adminRouter;
......@@ -14,17 +14,41 @@ import {
TableRow,
TableCell,
TableBody,
FormControl,
Select,
MenuItem,
} from "@mui/material";
export default function Order() {
const user = useContext(UserContext);
const adminOrder = useContext(AdminOrderContext);
const [message, setMessage] = useState({ message: "", error: false });
useEffect(() => {
console.log(adminOrder.value);
}, [user]);
const onSendingChange = async (id, sending) => {
try {
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 (
<>
......@@ -76,17 +100,33 @@ export default function Order() {
<TableCell>{order.shipping_price}</TableCell>
<TableCell>
<Box color={order.pay_status ? "green" : "red"}>
{order.pay_status ? (
"ชำระเงินแล้ว"
) : (
"ยังไม่ชำระเงิน"
)}
{order.pay_status
? "ชำระเงินแล้ว"
: "ยังไม่ชำระเงิน"}
</Box>
</TableCell>
<TableCell>
<Box color={order.send_status ? "green" : "red"}>
{order.send_status ? "ส่งแล้ว" : "ยังไม่จัดส่ง"}
</Box>
<FormControl variant="standard">
<Select
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>
</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