Commit d93bc232 authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

alert when change rank success or failed

parent 587d26bd
...@@ -3,8 +3,10 @@ import { UserContext } from "../_app"; ...@@ -3,8 +3,10 @@ import { UserContext } from "../_app";
import Head from "next/head"; import Head from "next/head";
import axios from "axios"; import axios from "axios";
import { import {
Alert,
Button, Button,
Paper, Paper,
Snackbar,
Switch, Switch,
Table, Table,
TableBody, TableBody,
...@@ -17,15 +19,29 @@ import DeleteMember from "@/components/member/DeleteMember"; ...@@ -17,15 +19,29 @@ import DeleteMember from "@/components/member/DeleteMember";
export default function Member() { export default function Member() {
const user = useContext(UserContext); const user = useContext(UserContext);
const [userAll, setUserAll] = useState([]); const [userAll, setUserAll] = useState([]);
const [message, setMessage] = useState({ error: false, message: "" });
const [deleteState, setDeleteState] = useState({ open: false, id: -1 }); const [deleteState, setDeleteState] = useState({ open: false, id: -1 });
async function changeRank(id, rank) { async function changeRank(id, rank) {
try {
let response = await axios.put( let response = await axios.put(
"/api/admin/user/rank", "/api/admin/user/rank",
{ id, rank }, { id, rank },
{ headers: { token: user.value.token } } { headers: { token: user.value.token } }
); );
if (response.data.status === 301) {
fetchApi(); fetchApi();
setMessage({ message: "เปลี่ยนสิทธิ์สำเร็จ", error: false });
setTimeout(() => {
setMessage({ message: "", error: false });
}, 2000);
}
} catch (err) {
setMessage({ message: "พบข้อผิดพลาด กรุณาลองใหม่อีกครั้ง", error: true });
setTimeout(() => {
setMessage({ message: "", error: false });
}, 3000);
}
} }
async function fetchApi() { async function fetchApi() {
try { try {
...@@ -36,6 +52,7 @@ export default function Member() { ...@@ -36,6 +52,7 @@ export default function Member() {
} catch (err) {} } catch (err) {}
} }
useEffect(() => { useEffect(() => {
// console.log();
fetchApi(); fetchApi();
}, [deleteState]); }, [deleteState]);
return ( return (
...@@ -43,6 +60,14 @@ export default function Member() { ...@@ -43,6 +60,14 @@ export default function Member() {
<Head> <Head>
<title>รายชื่อสมาชิก | admin</title> <title>รายชื่อสมาชิก | admin</title>
</Head> </Head>
<Snackbar
anchorOrigin={{ horizontal: "center", vertical: "top" }}
open={!!message.message.length}
>
<Alert color={message.error ? "error" : "success"}>
{message.message}
</Alert>
</Snackbar>
<Paper sx={{ p: 2 }}> <Paper sx={{ p: 2 }}>
<Table> <Table>
<TableHead> <TableHead>
......
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