diff --git a/src/components/Book.js b/src/components/Book.js index 794c432..09a9b68 100644 --- a/src/components/Book.js +++ b/src/components/Book.js @@ -1,22 +1,32 @@ /* eslint-disable no-unused-vars */ import React from 'react'; import PropTypes from 'prop-types'; -import Button from './Button'; +import { useDispatch } from 'react-redux'; +import { RemoveBook } from '../redux/book/bookSlice'; -const Book = ({ title, author }) => ( -
-

{title}

-

- By - {author} -

-
-); +const Book = ({ id, title, author }) => { + const dispatch = useDispatch(); + + const handleRemove = (id) => { + dispatch(RemoveBook(id)); + }; + + return ( +
+

{title}

+

+ By + {author} +

+ +
+ ); +}; Book.propTypes = { title: PropTypes.string, author: PropTypes.string, + id: PropTypes.string.isRequired, }; Book.defaultProps = { diff --git a/src/components/Books.js b/src/components/Books.js index b89f1d2..4d22019 100644 --- a/src/components/Books.js +++ b/src/components/Books.js @@ -1,16 +1,15 @@ import React from 'react'; +import { useSelector } from 'react-redux'; import Book from './Book'; import Form from './Form'; const Books = () => { - const books = [ - { id: 1, title: 'Book 1', author: 'Author 1' }, - { id: 2, title: 'Book 2', author: 'Author 2' }, - ]; + const { books } = useSelector((state) => state.book); + return (
{books.map((book) => ( - + ))}
diff --git a/src/components/Button.js b/src/components/Button.js index 75ca972..b53ff13 100644 --- a/src/components/Button.js +++ b/src/components/Button.js @@ -1,7 +1,11 @@ import React from 'react'; import PropTypes from 'prop-types'; -const Button = ({ name }) => ; +const Button = ({ name }) => ( + +); Button.propTypes = { name: PropTypes.string, diff --git a/src/components/Form.js b/src/components/Form.js index ecb8265..c8a25e7 100644 --- a/src/components/Form.js +++ b/src/components/Form.js @@ -1,13 +1,57 @@ -import React from 'react'; -import Button from './Button'; - -const Form = () => ( - -

ADD NEW BOOK

- - - + + ); +}; export default Form; diff --git a/src/index.js b/src/index.js index dac4ede..11867ce 100644 --- a/src/index.js +++ b/src/index.js @@ -1,11 +1,15 @@ import React from 'react'; import ReactDOM from 'react-dom/client'; import './index.css'; +import { Provider } from 'react-redux'; import App from './App'; +import store from './redux/store'; const root = ReactDOM.createRoot(document.getElementById('root')); root.render( - + + + , ); diff --git a/src/redux/book/bookSlice.js b/src/redux/book/bookSlice.js index 536cbac..acf2d5b 100644 --- a/src/redux/book/bookSlice.js +++ b/src/redux/book/bookSlice.js @@ -1,7 +1,26 @@ import { createReducer } from '@reduxjs/toolkit'; const initialState = { - books: [], + books: [ + { + item_id: 'item1', + title: 'The Great Gatsby', + author: 'John Smith', + category: 'Fiction', + }, + { + item_id: 'item2', + title: 'Anna Karenina', + author: 'Leo Tolstoy', + category: 'Fiction', + }, + { + item_id: 'item3', + title: 'The Selfish Gene', + author: 'Richard Dawkins', + category: 'Nonfiction', + }, + ], }; // Reducers @@ -10,21 +29,23 @@ export const bookReducer = createReducer(initialState, { state.books.push(action.payload); }, remove: (state, action) => { - const { id } = action.payload; - const newState = state.books.filter((i) => i.id !== id); + const id = action.payload; + const newState = state.books.filter((i) => i.item_id !== id); state.books = newState; }, }); // Actions -export const AddBook = (dispatch) => { +export const AddBook = (book) => (dispatch) => { dispatch({ type: 'add', + payload: book, }); }; -export const RemoveBook = (dispatch) => { +export const RemoveBook = (id) => (dispatch) => { dispatch({ type: 'remove', + payload: id, }); }; diff --git a/src/redux/category/categorySlice.js b/src/redux/category/categorySlice.js index e1c2339..5e33250 100644 --- a/src/redux/category/categorySlice.js +++ b/src/redux/category/categorySlice.js @@ -6,8 +6,7 @@ const initialState = { }; // Reducers -export const categoriesReducer = createReducer({ - initialState, +export const categoriesReducer = createReducer(initialState, { checkstatus: (state) => ({ ...state, status: 'Under Construction' }), });