diff --git a/day-12/bs-store/src/adminpages/books/UpdateBook.js b/day-12/bs-store/src/adminpages/books/UpdateBook.js new file mode 100644 index 0000000..c977515 --- /dev/null +++ b/day-12/bs-store/src/adminpages/books/UpdateBook.js @@ -0,0 +1,176 @@ +import { + Box, + Button, + ButtonGroup, + FormControl, + Grid, + InputLabel, + MenuItem, + Stack, + TextField, +} from "@mui/material"; +import Select from "@mui/material/Select"; +import { useFormik } from "formik"; +import React, { useState, useEffect } from "react"; +import AuthorService from "../../services/AuthorService"; +import CategoryService from "../../services/CategoryService"; + +import Radio from "@mui/material/Radio"; +import RadioGroup from "@mui/material/RadioGroup"; +import FormControlLabel from "@mui/material/FormControlLabel"; +import FormLabel from "@mui/material/FormLabel"; +import BookService from "../../services/BookService"; +import { useNavigate,useParams } from "react-router-dom"; +import { useSelector, useDispatch } from "react-redux"; +import { putOneBook } from "../../store/actions/bookActions"; +import { showSnackbar } from "../../store/actions/settingActions"; +import { validationSchema } from "./AddBookValidation"; +import SimpleFab from "../../components/fab/SimpleFab"; + +const UpdateBook = () => { + + const navigate = useNavigate(); + + const selector = useSelector((state) => state.book); + const bookDispatch = useDispatch(); + + const [authors, setAuthors] = useState([]); + const [categories, setCategories] = useState([]); + + const authorService = new AuthorService(); + const categoryService = new CategoryService(); + const bookService = new BookService(); + const { id } = useParams(); + const { handleSubmit, handleChange, handleBlur, values, errors, touched } = + useFormik({ + initialValues: { + authorIds: [], + categoryId: "", + title: "", + price: 0, + publisher: "" + }, + onSubmit: async (values) => { + console.log(values); + bookDispatch(putOneBook(id,values)); + bookDispatch(showSnackbar({ + message:"Book has been updated.", + severity:"info" + })); + navigate("/admin/books/list"); + }, + }); + + useEffect(() => { + authorService.getAllAuthors().then((resp) => setAuthors(resp.data)); + categoryService.getAllCategories().then((resp) => setCategories(resp.data)); + }, []); + + return ( +
+ ) +} + +export default UpdateBook \ No newline at end of file