Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
F
final-exam
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nawasan Wisitsingkhon
final-exam
Commits
452287af
Commit
452287af
authored
Oct 02, 2023
by
Nawasan Wisitsingkhon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
finish delete user by admin
parent
568e03d1
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
164 additions
and
6 deletions
+164
-6
AdminUserController.js
app/controllers/AdminUserController.js
+21
-0
admin.js
app/routes/admin.js
+2
-0
DeleteMember.js
src/components/member/DeleteMember.js
+64
-0
member.js
src/pages/admin/member.js
+77
-6
No files found.
app/controllers/AdminUserController.js
View file @
452287af
...
@@ -11,6 +11,27 @@ const AdminUserController = {
...
@@ -11,6 +11,27 @@ const AdminUserController = {
db
.
$disconnect
();
db
.
$disconnect
();
res
.
json
(
userAll
);
res
.
json
(
userAll
);
},
},
/**
*
* @param {Request} req
* @param {Response} res
*/
async
delete
(
req
,
res
)
{
try
{
const
{
id
}
=
req
.
query
;
if
(
!
id
)
throw
400
;
await
db
.
user
.
delete
({
where
:
{
id
:
Number
(
id
)
}
});
res
.
json
({
status
:
401
,
message
:
"delete success"
,
});
}
catch
(
err
)
{
res
.
json
({
status
:
400
,
message
:
"delete failed with some error on server"
,
});
}
},
};
};
export
default
AdminUserController
;
export
default
AdminUserController
;
app/routes/admin.js
View file @
452287af
...
@@ -5,6 +5,8 @@ import CategoryController from "../controllers/CategoryController";
...
@@ -5,6 +5,8 @@ import CategoryController from "../controllers/CategoryController";
const
adminRouter
=
express
.
Router
();
const
adminRouter
=
express
.
Router
();
adminRouter
.
get
(
"/user"
,
AdminUserController
.
index
);
adminRouter
.
get
(
"/user"
,
AdminUserController
.
index
);
adminRouter
.
delete
(
"/user"
,
AdminUserController
.
delete
);
adminRouter
.
post
(
"/product"
,
AdminProductController
.
create
);
adminRouter
.
post
(
"/product"
,
AdminProductController
.
create
);
adminRouter
.
put
(
"/product"
,
AdminProductController
.
update
);
adminRouter
.
put
(
"/product"
,
AdminProductController
.
update
);
adminRouter
.
delete
(
"/product"
,
AdminProductController
.
delete
);
adminRouter
.
delete
(
"/product"
,
AdminProductController
.
delete
);
...
...
src/components/member/DeleteMember.js
0 → 100644
View file @
452287af
import
{
useState
,
useContext
}
from
"react"
;
import
Button
from
"@mui/material/Button"
;
import
Dialog
from
"@mui/material/Dialog"
;
import
DialogActions
from
"@mui/material/DialogActions"
;
import
DialogContent
from
"@mui/material/DialogContent"
;
import
DialogTitle
from
"@mui/material/DialogTitle"
;
import
{
Alert
,
Snackbar
}
from
"@mui/material"
;
import
axios
from
"axios"
;
import
{
UserContext
}
from
"@/pages/_app"
;
export
default
function
DeleteMember
({
open
,
handleClose
,
id
})
{
const
user
=
useContext
(
UserContext
);
const
[
message
,
setMessage
]
=
useState
({
message
:
""
,
error
:
false
});
async
function
onDeleteMember
()
{
try
{
let
response
=
await
axios
.
delete
(
`/api/admin/user?id=
${
id
}
`
,
{
headers
:
{
token
:
user
.
value
.
token
},
});
console
.
log
(
response
.
data
);
if
(
response
.
data
.
status
===
401
)
{
setMessage
({
error
:
false
,
message
:
"สบสำเร็จ!"
});
setTimeout
(()
=>
{
handleClose
()
setMessage
({
error
:
false
,
message
:
""
});
},
1000
);
}
}
catch
(
err
)
{
setMessage
({
error
:
true
,
message
:
"พบข้อผิดพลาด ไม่สามารถลบได้"
});
setTimeout
(()
=>
{
setMessage
({
error
:
false
,
message
:
""
});
},
3000
);
}
}
return
(
<>
<
Snackbar
anchorOrigin
=
{{
horizontal
:
"center"
,
vertical
:
"top"
}}
ContentProps
=
{{
className
:
message
.
error
?
"bg-red-500"
:
"bg-green-500"
,
}}
open
=
{
!!
message
.
message
.
length
}
message
=
{
message
.
message
}
>
<
Alert
severity
=
{
message
.
error
?
"error"
:
"success"
}
>
{
message
.
message
}
<
/Alert
>
<
/Snackbar
>
<
Dialog
open
=
{
open
}
onClose
=
{
handleClose
}
>
<
DialogTitle
>
ยืนยันการลบ
<
/DialogTitle
>
<
DialogContent
className
=
"text-center"
>
เมื่อกดลบแล้วจะไม่สามารถกู้คืนได้
<
/DialogContent
>
<
DialogActions
>
<
Button
color
=
"error"
onClick
=
{
handleClose
}
>
ยกเลิก
<
/Button
>
<
Button
onClick
=
{
onDeleteMember
}
>
ยืนยัน
<
/Button
>
<
/DialogActions
>
<
/Dialog
>
<
/
>
);
}
src/pages/admin/member.js
View file @
452287af
import
{
useContext
,
useEffect
,
useState
}
from
"react"
;
import
{
useContext
,
useEffect
,
useState
}
from
"react"
;
import
{
UserContext
}
from
"../_app"
;
import
{
UserContext
}
from
"../_app"
;
import
Head
from
"next/head"
;
import
axios
from
"axios"
;
import
axios
from
"axios"
;
import
{
Button
,
Paper
,
Switch
,
Table
,
TableBody
,
TableCell
,
TableHead
,
TableRow
,
}
from
"@mui/material"
;
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
[
deleteState
,
setDeleteState
]
=
useState
({
open
:
false
,
id
:
-
1
});
async
function
fetchApi
()
{
async
function
fetchApi
()
{
try
{
try
{
...
@@ -17,15 +30,73 @@ export default function Member() {
...
@@ -17,15 +30,73 @@ export default function Member() {
}
}
useEffect
(()
=>
{
useEffect
(()
=>
{
fetchApi
();
fetchApi
();
},
[]);
},
[
deleteState
]);
return
(
return
(
<
div
>
<
div
>
{
userAll
.
map
((
usr
,
idx
)
=>
(
<
Head
>
<
div
key
=
{
idx
}
>
<
title
>
รายชื่อสมาชิก
|
admin
<
/title
>
{
usr
.
name
}@{
usr
.
username
}
{
usr
.
email
}
<
/Head
>
<
/div
>
<
Paper
sx
=
{{
p
:
2
}}
>
<
Table
>
<
TableHead
>
<
TableRow
>
{[
"id"
,
"รูป"
,
"ชื่อ"
,
"username"
,
"เบอร์โทร"
,
"อีเมล"
,
"rank"
,
"ลบ"
,
].
map
((
label
,
idx
)
=>
(
<
TableCell
sx
=
{{
textAlign
:
"center"
}}
key
=
{
idx
}
>
{
label
}
<
/TableCell
>
))}
<
/TableRow
>
<
/TableHead
>
<
TableBody
>
{
userAll
.
map
((
user
,
idx
)
=>
(
<
TableRow
key
=
{
idx
}
>
<
TableCell
>
{
user
.
id
}
<
/TableCell
>
<
TableCell
>
<
img
width
=
{
70
}
src
=
{
user
?.
photo
?.
length
?
user
.
photo
:
"/empty.jpg"
}
alt
=
"profile"
/>
<
/TableCell
>
<
TableCell
>
{
user
.
name
}
<
/TableCell
>
<
TableCell
>
@{
user
.
username
}
<
/TableCell
>
<
TableCell
>
{
user
.
phone
}
<
/TableCell
>
<
TableCell
>
{
user
.
email
}
<
/TableCell
>
<
TableCell
sx
=
{{
textAlign
:
"center"
}}
>
<
Switch
checked
=
{
!!
user
.
rank
}
onChange
=
{()
=>
alert
(
"change rank"
)}
/
>
<
/TableCell
>
<
TableCell
sx
=
{{
textAlign
:
"center"
}}
>
<
Button
onClick
=
{()
=>
setDeleteState
({
open
:
true
,
id
:
user
.
id
})}
color
=
"error"
>
ลบ
<
/Button
>
<
/TableCell
>
<
/TableRow
>
))}
))}
<
button
onClick
=
{
fetchApi
}
>
submit
<
/button
>
<
/TableBody
>
<
/Table
>
<
/Paper
>
{
deleteState
.
open
&&
(
<
DeleteMember
open
=
{
deleteState
.
open
}
handleClose
=
{()
=>
setDeleteState
({
open
:
false
,
id
:
-
1
})}
id
=
{
deleteState
.
id
}
/
>
)}
<
/div
>
<
/div
>
);
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment