Skip to content

Commit

Permalink
refactoring reducer
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislav committed Jun 1, 2020
1 parent de9a7af commit 9ec259c
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 90 deletions.
2 changes: 1 addition & 1 deletion src/components/book-list/book-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class BookListContainer extends Component {
}
};

const mapStateToProps = ({ books, loading, error }) => {
const mapStateToProps = ({bookList: { books, loading, error }}) => {
return { books, loading, error }
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/shopping-cart-table/shopping-cart-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const ShoppingCartTable = ({ items, total, onIncrease, onDecrease, onDelete }) =
)
};

const mapStateToProps = ({ cartItems, orderTotal }) => {
const mapStateToProps = ({ shoppingCart: { cartItems, orderTotal } }) => {
return {
items: cartItems,
total: orderTotal
Expand Down
38 changes: 38 additions & 0 deletions src/reducers/book-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

const updateBookList = (state, action) => {

if (state === undefined) {
return {
books: [],
loading: true,
error: null
};
}

switch (action.type) {
case 'FETCH_BOOKS_REQUEST':
return {
books: [],
loading: true,
error: null
};

case 'FETCH_BOOKS_SUCCESS':
return {
books: action.payload,
loading: false,
error: null
};

case 'FETCH_BOOKS_FAILURE':
return {
books: [],
loading: false,
error: action.payload
};
default:
return state.bookList;
}
};

export default updateBookList
93 changes: 5 additions & 88 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,94 +1,11 @@

const initialState = {
books: [],
loading: true,
error: null,
cartItems: [],
orderTotal: 590
};

const updateCartItems = (cartItems, item, idx) => {
if (item.count === 0) {
return [
...cartItems.slice(0, idx),
...cartItems.slice(idx + 1)
]
};
if (idx === -1) {
return [
...cartItems,
item
]
};
return [
...cartItems.slice(0, idx),
item,
...cartItems.slice(idx + 1)
];
};

const updateCartItem = (book, item = {}, quantity) => {
const { id = book.id, count = 0, title = book.title, total = 0 } = item;
return {
id,
title,
count: count + quantity,
total: total + quantity * book.price
}
};

const updateOrder = (state, bookId, quantity) => {
const { books, cartItems } = state;
const book = books.find(({ id }) => id === bookId);
const itemIndex = cartItems.findIndex(({ id }) => id === bookId);
const item = cartItems[itemIndex];
import updateBookList from './book-list';
import updateShoppingCart from './shopping-cart';

const newItem = updateCartItem(book, item, quantity);
const reducer = (state, action) => {
return {
...state,
cartItems: updateCartItems(cartItems, newItem, itemIndex)
};
};

const reducer = (state = initialState, action) => {
//console.log(action.type);
switch (action.type) {
case 'FETCH_BOOKS_REQUEST':
return {
...state,
books: [],
loading: true,
error: null
};

case 'FETCH_BOOKS_SUCCESS':
return {
...state,
books: action.payload,
loading: false,
error: null
};

case 'FETCH_BOOKS_FAILURE':
return {
...state,
books: [],
loading: false,
error: action.payload
};

case 'BOOK_ADDED_TO_CART':
return updateOrder(state, action.payload, 1);

case 'BOOK_REMOVE_FROM_CART':
return updateOrder(state, action.payload, -1);

case 'ALL_BOOKS_REMOVE_FROM_CART':
const item = state.cartItems.find(({ id }) => id === action.payload);
return updateOrder(state, action.payload, -item.count);

default:
return state;
bookList: updateBookList(state, action),
shoppingCart: updateShoppingCart(state, action)
}
};

Expand Down
70 changes: 70 additions & 0 deletions src/reducers/shopping-cart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

const updateCartItems = (cartItems, item, idx) => {
if (item.count === 0) {
return [
...cartItems.slice(0, idx),
...cartItems.slice(idx + 1)
]
};
if (idx === -1) {
return [
...cartItems,
item
]
};
return [
...cartItems.slice(0, idx),
item,
...cartItems.slice(idx + 1)
];
};

const updateCartItem = (book, item = {}, quantity) => {
const { id = book.id, count = 0, title = book.title, total = 0 } = item;
return {
id,
title,
count: count + quantity,
total: total + quantity * book.price
}
};

const updateOrder = (state, bookId, quantity) => {
const { bookList: { books }, shoppingCart: { cartItems } } = state;
const book = books.find(({ id }) => id === bookId);
const itemIndex = cartItems.findIndex(({ id }) => id === bookId);
const item = cartItems[itemIndex];

const newItem = updateCartItem(book, item, quantity);
return {
orderTotal: 0,
cartItems: updateCartItems(cartItems, newItem, itemIndex)
};
};

const updateShoppingCart = (state, action) => {

if (state === undefined) {
return {
cartItems: [],
orderTotal: 0
}
}

switch (action.type) {
case 'BOOK_ADDED_TO_CART':
return updateOrder(state, action.payload, 1);

case 'BOOK_REMOVE_FROM_CART':
return updateOrder(state, action.payload, -1);

case 'ALL_BOOKS_REMOVE_FROM_CART':
const item = state.shoppingCart.cartItems.find(({ id }) => id === action.payload);
return updateOrder(state, action.payload, -item.count);

default:
return state.shoppingCart;
}
};

export default updateShoppingCart;

0 comments on commit 9ec259c

Please sign in to comment.