-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Stanislav
committed
Jun 1, 2020
1 parent
de9a7af
commit 9ec259c
Showing
5 changed files
with
115 additions
and
90 deletions.
There are no files selected for viewing
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
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,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 |
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 |
---|---|---|
@@ -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; |