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
74630670
Commit
74630670
authored
Oct 01, 2023
by
Nawasan Wisitsingkhon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update category
parent
9a2899ba
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
141 additions
and
16 deletions
+141
-16
CategoryController.js
app/controllers/CategoryController.js
+29
-0
admin.js
app/routes/admin.js
+1
-0
AddCategory.js
src/components/AddCategory.js
+6
-12
UpdateCategory.js
src/components/UpdateCategory.js
+77
-0
category.js
src/pages/admin/category.js
+28
-4
No files found.
app/controllers/CategoryController.js
View file @
74630670
...
...
@@ -34,6 +34,35 @@ const CategoryController = {
}
}
},
/**
*
* @param {Request} req
* @param {Response} res
*/
async
update
(
req
,
res
)
{
try
{
const
{
id
,
name
}
=
req
.
body
;
console
.
log
(
id
,
name
);
await
db
.
category
.
update
({
where
:
{
id
},
data
:
{
name
}
});
await
db
.
$disconnect
();
res
.
json
({
status
:
301
,
message
:
"update success"
});
}
catch
(
err
)
{
console
.
log
(
err
);
res
.
json
({
status
:
300
,
message
:
"error can't update"
});
}
},
/**
*
* @param {Request} req
* @param {Response} res
*/
async
delete
(
req
,
res
)
{
try
{
res
.
json
({
params
:
req
.
params
})
}
catch
(
err
)
{
res
.
json
({
status
:
400
,
message
:
"server has some error"
})
}
}
};
export
default
CategoryController
;
app/routes/admin.js
View file @
74630670
...
...
@@ -7,5 +7,6 @@ const adminRouter = express.Router();
adminRouter
.
get
(
"/user"
,
AdminUserController
.
index
);
adminRouter
.
post
(
"/product"
,
AdminProductController
.
create
);
adminRouter
.
post
(
"/category"
,
CategoryController
.
create
);
adminRouter
.
put
(
"/category"
,
CategoryController
.
update
);
export
default
adminRouter
;
src/components/AddCategory.js
View file @
74630670
import
{
useState
,
use
Effect
,
use
Context
}
from
"react"
;
import
{
useState
,
useContext
}
from
"react"
;
import
Button
from
"@mui/material/Button"
;
import
TextField
from
"@mui/material/TextField"
;
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
{
FormControl
,
InputLabel
,
MenuItem
,
Select
,
Snackbar
,
}
from
"@mui/material"
;
import
{
Snackbar
}
from
"@mui/material"
;
import
axios
from
"axios"
;
import
{
UserContext
}
from
"@/pages/_app"
;
...
...
@@ -36,10 +30,10 @@ export default function AddCategory({ open, handleClose }) {
setTimeout
(()
=>
{
setMessage
({
message
:
""
,
error
:
false
});
},
3000
);
}
else
if
(
response
.
data
.
status
===
201
)
{
setName
(
""
)
handleClose
()
setMessage
({
message
:
"เพิ่มหมวดหมู่สำเร็จ"
,
error
:
false
})
}
else
if
(
response
.
data
.
status
===
201
)
{
setName
(
""
)
;
handleClose
()
;
setMessage
({
message
:
"เพิ่มหมวดหมู่สำเร็จ"
,
error
:
false
});
setTimeout
(()
=>
{
setMessage
({
message
:
""
,
error
:
false
});
},
3000
);
...
...
src/components/UpdateCategory.js
0 → 100644
View file @
74630670
import
{
useState
,
useEffect
,
useContext
}
from
"react"
;
import
Button
from
"@mui/material/Button"
;
import
TextField
from
"@mui/material/TextField"
;
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
{
Snackbar
}
from
"@mui/material"
;
import
axios
from
"axios"
;
import
{
UserContext
}
from
"@/pages/_app"
;
export
default
function
UpdateCategory
({
open
,
handleClose
,
data
})
{
const
user
=
useContext
(
UserContext
);
const
[
message
,
setMessage
]
=
useState
({
message
:
""
,
error
:
false
});
const
[
name
,
setName
]
=
useState
(
data
.
name
);
/**
*
* @param {FormDataEvent} e
*/
async
function
fetchAddProduct
(
e
)
{
e
.
preventDefault
();
let
response
=
await
axios
.
put
(
"/api/admin/category"
,
{
id
:
data
.
id
,
name
},
{
headers
:
{
token
:
user
.
value
.
token
}
}
);
console
.
log
(
response
.
data
);
if
(
response
.
data
.
status
===
301
)
{
setName
(
""
);
setMessage
({
message
:
"บันทึกหมวดหมู่สำเร็จ"
,
error
:
false
});
setTimeout
(()
=>
{
handleClose
();
setMessage
({
message
:
""
,
error
:
false
});
},
2000
);
}
else
{
setMessage
({
message
:
"มีบางอย่างผิดพลาด"
,
error
:
true
});
setTimeout
(()
=>
{
setMessage
({
message
:
""
,
error
:
false
});
},
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
}
/
>
<
Dialog
open
=
{
open
}
onClose
=
{
handleClose
}
>
<
form
onSubmit
=
{
fetchAddProduct
}
>
<
DialogTitle
>
แก้ไข
<
/DialogTitle
>
<
DialogContent
className
=
"text-center"
>
<
TextField
className
=
"m-1"
label
=
"ชื่อหมวดหมู่"
variant
=
"standard"
value
=
{
name
}
onChange
=
{(
e
)
=>
setName
(
e
.
target
.
value
)}
required
/>
<
/DialogContent
>
<
DialogActions
>
<
Button
color
=
"error"
onClick
=
{
handleClose
}
>
ยกเลิก
<
/Button
>
<
Button
type
=
"submit"
>
บันทึก
<
/Button
>
<
/DialogActions
>
<
/form
>
<
/Dialog
>
<
/
>
);
}
src/pages/admin/category.js
View file @
74630670
...
...
@@ -15,16 +15,21 @@ import AddCategory from "@/components/AddCategory";
import
axios
from
"axios"
;
import
{
Delete
}
from
"@mui/icons-material"
;
import
{
Edit
}
from
"@mui/icons-material"
;
import
UpdateCategory
from
"@/components/UpdateCategory"
;
export
default
function
AdminCategory
()
{
const
[
modal
,
setModal
]
=
useState
(
false
);
const
[
updateState
,
setUpdateState
]
=
useState
({
open
:
false
,
data
:
{
id
:
-
1
,
name
:
""
},
});
const
[
category
,
setCategory
]
=
useState
([]);
useEffect
(()
=>
{
axios
.
get
(
"/api/category"
).
then
((
res
)
=>
{
setCategory
(
res
.
data
);
});
},
[]);
},
[
modal
,
updateState
]);
return
(
<
Box
>
<
Grid
container
>
...
...
@@ -52,11 +57,19 @@ export default function AdminCategory() {
<
/TableHead
>
<
TableBody
>
{
category
.
map
((
cate
,
idx
)
=>
(
<
TableRow
>
<
TableRow
key
=
{
idx
}
>
<
TableCell
>
{
cate
.
id
}
<
/TableCell
>
<
TableCell
>
{
cate
.
name
}
<
/TableCell
>
<
TableCell
sx
=
{{
maxWidth
:
10
}}
>
<
Button
color
=
"warning"
>
<
Button
color
=
"warning"
onClick
=
{()
=>
setUpdateState
({
open
:
true
,
data
:
{
id
:
cate
.
id
,
name
:
cate
.
name
},
})
}
>
<
Edit
/>
<
/Button
>
<
/TableCell
>
...
...
@@ -74,7 +87,18 @@ export default function AdminCategory() {
<
/Grid
>
<
Grid
item
lg
=
{
3
}
md
=
{
0
}
><
/Grid
>
<
/Grid
>
<
AddCategory
open
=
{
modal
}
handleClose
=
{()
=>
setModal
(
false
)}
/
>
{
modal
&&
(
<
AddCategory
open
=
{
modal
}
handleClose
=
{()
=>
setModal
(
false
)}
/
>
)}
{
updateState
.
open
&&
(
<
UpdateCategory
open
=
{
updateState
.
open
}
data
=
{
updateState
.
data
}
handleClose
=
{()
=>
setUpdateState
({
open
:
false
,
data
:
{
id
:
-
1
,
name
:
""
}
})
}
/
>
)}
<
/Box
>
);
}
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