Skip to content
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

Bookstore: add reducers and actions #2

Merged
merged 4 commits into from Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
151 changes: 151 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -8,8 +8,10 @@
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-redux": "^8.0.2",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
"redux": "^4.2.0",
"web-vitals": "^2.1.4"
},
"scripts": {
Expand Down
26 changes: 26 additions & 0 deletions src/redux/books/books.js
@@ -0,0 +1,26 @@
const ADD_BOOK = 'ADD_BOOK';
const DELETE_BOOK = 'DELETE_BOOK';

// Actions

export const addBook = (book) => ({
type: ADD_BOOK,
payload: book,
});

export const deleteBook = () => ({
type: DELETE_BOOK,
});

// Reducer
const bookReducer = (state = [], action) => {
switch (action.type) {
case 'ADD_BOOK':
return [...state, action.payload];
case 'DELETE_BOOK':
return [...state.filter((book) => book !== action.payload)];
default: return state;
}
};

export default bookReducer;
18 changes: 18 additions & 0 deletions src/redux/categories/categories.js
@@ -0,0 +1,18 @@
const CHECK_STATUS = 'CHECK_STATUS';

// Action
export const checkStatus = () => ({
type: CHECK_STATUS,
});

// Reducer
const data = 'Under construction';
const statusReducer = (state = [], action) => {
switch (action.type) {
case 'CHECK_STATUS':
return data;
default: return state;
}
};

export default statusReducer;
11 changes: 11 additions & 0 deletions src/redux/configureStore.js
@@ -0,0 +1,11 @@
import { combineReducers, createStore } from 'redux';
import statusReducer from './categories/categories';
import bookReducer from './books/books';

const rootReducer = combineReducers({
books: bookReducer,
status: statusReducer,
});

const store = createStore(rootReducer);
export default store;