summaryrefslogtreecommitdiffstats
path: root/front/odiparpack/app/redux/modules/ecommerce.js
diff options
context:
space:
mode:
Diffstat (limited to 'front/odiparpack/app/redux/modules/ecommerce.js')
-rw-r--r--front/odiparpack/app/redux/modules/ecommerce.js94
1 files changed, 94 insertions, 0 deletions
diff --git a/front/odiparpack/app/redux/modules/ecommerce.js b/front/odiparpack/app/redux/modules/ecommerce.js
new file mode 100644
index 0000000..a294bae
--- /dev/null
+++ b/front/odiparpack/app/redux/modules/ecommerce.js
@@ -0,0 +1,94 @@
+import { fromJS, List } from 'immutable';
+import notif from 'ba-api/notifMessage';
+import {
+ FETCH_PRODUCT_DATA,
+ ADD_TO_CART,
+ DELETE_CART_ITEM,
+ CHECKOUT,
+ SHOW_DETAIL_PRODUCT,
+ SEARCH_PRODUCT,
+ CLOSE_NOTIF
+} from 'ba-actions/actionTypes';
+
+const initialState = {
+ productList: List([]),
+ cart: List([]),
+ totalItems: 0,
+ totalPrice: 0,
+ productIndex: 0,
+ keywordValue: '',
+ notifMsg: '',
+};
+
+let itemId = [];
+
+const initialImmutableState = fromJS(initialState);
+export default function reducer(state = initialImmutableState, action = {}) {
+ switch (action.type) {
+ case FETCH_PRODUCT_DATA:
+ return state.withMutations((mutableState) => {
+ const items = fromJS(action.items);
+ mutableState.set('productList', items);
+ });
+ case SEARCH_PRODUCT:
+ return state.withMutations((mutableState) => {
+ action.keyword.persist();
+ const keyword = action.keyword.target.value.toLowerCase();
+ mutableState.set('keywordValue', keyword);
+ });
+ case ADD_TO_CART:
+ return state.withMutations((mutableState) => {
+ const item = fromJS(action.item);
+ const qty = Number(item.get('quantity'));
+ const price = item.get('price');
+ const index = itemId.indexOf(action.item.id);
+ if (index > -1) {
+ // If item already added to cart
+ mutableState.update('cart', cart => cart.setIn(
+ [index, 'quantity'],
+ state.getIn(['cart', index, 'quantity']) + qty
+ ));
+ } else {
+ // item not exist in cart
+ itemId.push(action.item.id);
+ mutableState.update('cart', cart => cart.push(item));
+ }
+ mutableState
+ .set('totalItems', state.get('totalItems') + qty)
+ .set('totalPrice', state.get('totalPrice') + (price * qty))
+ .set('notifMsg', notif.addCart);
+ });
+ case DELETE_CART_ITEM:
+ return state.withMutations((mutableState) => {
+ const index = state.get('cart').indexOf(action.item);
+ const qty = Number(action.item.get('quantity'));
+ const price = action.item.get('price');
+ itemId = itemId.filter(item => item !== action.item.get('id'));
+ mutableState
+ .update('cart', cart => cart.splice(index, 1))
+ .set('totalItems', state.get('totalItems') - qty)
+ .set('totalPrice', state.get('totalPrice') - (price * qty))
+ .set('notifMsg', notif.removed);
+ });
+ case CHECKOUT:
+ itemId = [];
+ return state.withMutations((mutableState) => {
+ mutableState
+ .set('cart', List([]))
+ .set('totalItems', 0)
+ .set('totalPrice', 0)
+ .set('notifMsg', notif.checkout);
+ });
+ case SHOW_DETAIL_PRODUCT:
+ return state.withMutations((mutableState) => {
+ const index = state.get('productList').indexOf(action.item);
+ mutableState.set('productIndex', index);
+ });
+ case CLOSE_NOTIF:
+ return state.withMutations((mutableState) => {
+ mutableState.set('notifMsg', '');
+ });
+ default:
+ return state;
+ }
+}