New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Redux setup #2
Merged
Merged
Redux setup #2
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b5ade7c
Finish installing redux dependency
Micky373 a67e042
Finish folder structuring
Micky373 1d9b91e
Finish book.js
Micky373 99e23a3
Finish catagories.js
Micky373 70b88d5
Finish configureStore.js
Micky373 956428a
Fix linter errors
Micky373 ac3deb0
Fix the CR's issues
Micky373 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,7 @@ nav { | |
height: 30rem; | ||
} | ||
|
||
.add_book { | ||
.add_book form { | ||
display: flex; | ||
gap: 1rem; | ||
margin-top: 3rem; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,63 @@ | ||
import React from 'react'; | ||
|
||
const AddBook = () => ( | ||
<div className="add_book"> | ||
<input className="author" placeholder="Author name" /> | ||
<input className="title" placeholder="Book title" /> | ||
<button className="add_button" type="button">ADD BOOK</button> | ||
</div> | ||
); | ||
// import React from 'react'; | ||
|
||
// const AddBook = () => ( | ||
// <div className="add_book"> | ||
// <input className="author" placeholder="Author name" /> | ||
// <input className="title" placeholder="Book title" /> | ||
// <button className="add_button" type="button">ADD BOOK</button> | ||
// </div> | ||
// ); | ||
|
||
// export default AddBook; | ||
|
||
import { useState } from 'react'; | ||
import { useDispatch } from 'react-redux'; | ||
import { addBook } from '../redux/books/books'; | ||
import bookStore from '../redux/configureStore'; | ||
|
||
const AddBook = () => { | ||
const [form, setForm] = useState({ | ||
title: '', | ||
author: '', | ||
}); | ||
|
||
const titleChange = (e) => { | ||
setForm({ | ||
...form, | ||
title: e.target.value, | ||
}); | ||
}; | ||
|
||
const authorChange = (e) => { | ||
setForm({ | ||
...form, | ||
author: e.target.value, | ||
}); | ||
}; | ||
|
||
const dispatch = useDispatch(); | ||
|
||
const submitBook = (e) => { | ||
e.preventDefault(); | ||
const theBook = { | ||
id: bookStore.getState().books.length, | ||
author: form.author, | ||
title: form.title, | ||
}; | ||
dispatch(addBook(theBook)); | ||
form.author = ''; | ||
form.title = ''; | ||
}; | ||
|
||
return ( | ||
<div className="add_book"> | ||
<form onSubmit={(e) => submitBook(e)}> | ||
<input className="author" placeholder="Author name" value={form.author} onChange={(e) => authorChange(e)} required /> | ||
<input className="title" placeholder="Book title" value={form.title} onChange={(e) => titleChange(e)} required /> | ||
<button className="add_button" type="submit">ADD BOOK</button> | ||
</form> | ||
</div> | ||
); | ||
}; | ||
|
||
export default AddBook; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,27 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { useDispatch } from 'react-redux'; | ||
import { removeBook } from '../redux/books/books'; | ||
|
||
const Book = (props) => { | ||
const { title, author } = props; | ||
const { title, author, id } = props; | ||
const dispatch = useDispatch(); | ||
const remove = (bookToBeRemoved) => { | ||
dispatch(removeBook({ id: bookToBeRemoved })); | ||
}; | ||
|
||
return ( | ||
<div className="book"> | ||
<h1 className="book_title">{title}</h1> | ||
<h1 className="book_author">{author}</h1> | ||
<button className="remove_button" type="button">Remove</button> | ||
<h1 className="book_title">{title}</h1> | ||
<button className="remove_button" type="button" id={id} onClick={(e) => remove(e.target.id)}>Remove</button> | ||
</div> | ||
); | ||
}; | ||
|
||
Book.propTypes = { title: PropTypes.string.isRequired, author: PropTypes.string.isRequired }; | ||
Book.propTypes = { | ||
title: PropTypes.string.isRequired, | ||
author: PropTypes.string.isRequired, | ||
id: PropTypes.number.isRequired, | ||
}; | ||
export default Book; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { Provider } from 'react-redux'; | ||
import bookStore from './redux/configureStore'; | ||
import App from './App'; | ||
|
||
ReactDOM.render( | ||
<React.StrictMode> | ||
<App /> | ||
<Provider store={bookStore}> | ||
<App /> | ||
</Provider> | ||
</React.StrictMode>, | ||
document.getElementById('root'), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const ADD_BOOK = 'bookstore/books/ADD_BOOK'; | ||
const REMOVE_BOOK = 'bookstore/books/REMOVE_BOOK'; | ||
const initialState = []; | ||
|
||
export const addBook = (book) => ({ | ||
type: ADD_BOOK, | ||
payload: book, | ||
}); | ||
|
||
export const removeBook = (id) => ({ | ||
type: REMOVE_BOOK, | ||
payload: id, | ||
}); | ||
|
||
const bookReducer = (state = initialState, action) => { | ||
switch (action.type) { | ||
case ADD_BOOK: return [ | ||
...state, | ||
{ | ||
id: state.length + 1, | ||
title: action.payload.title, | ||
author: action.payload.author, | ||
}, | ||
]; | ||
case REMOVE_BOOK: return state.filter((book) => (book.id !== action.payload.id)); | ||
default: return state; | ||
} | ||
}; | ||
|
||
export default bookReducer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const CHECK_STATUS = 'bookstore/categories/CHECK_STATUS'; | ||
const initialState = []; | ||
|
||
export const checkStatus = (id) => ({ | ||
type: CHECK_STATUS, | ||
payload: id, | ||
}); | ||
|
||
const checkStatusReducer = (state = initialState, action) => { | ||
switch (action.type) { | ||
case CHECK_STATUS: return 'UNDER CONSTRUCTION'; | ||
default: return state; | ||
} | ||
}; | ||
|
||
export default checkStatusReducer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { combineReducers, createStore } from 'redux'; | ||
import checkStatusReducer from './catagories/catagories'; | ||
import bookReducer from './books/books'; | ||
|
||
const rootReducer = combineReducers({ | ||
books: bookReducer, | ||
status: checkStatusReducer, | ||
}); | ||
|
||
const bookStore = createStore(rootReducer); | ||
|
||
export default bookStore; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your application is not performing as expected, I cannot add books. When I click on the
add book
button nothing happens. Kindly fix this. As a result of this, I couldn't test yourremove book
function.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello reviewer, That is the functionality that should be added in the next milestone when we link the react and redux.
We are not supposed to complete that action at this branch.