Skip to content

Commit

Permalink
feat(store): add cart state
Browse files Browse the repository at this point in the history
create cart reducer, action, actionType
  • Loading branch information
aneurysmjs committed Aug 20, 2019
1 parent fbb491c commit 64eac60
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/app/store/ActionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ export const GET_PRODUCTS_SUCCESS = 'GET_PRODUCTS_SUCCESS';
export const GET_PRODUCTS_FAILURE = 'GET_PRODUCTS_FAILURE';

export const FOOTER_DATA = 'FOOTER_DATA';

export const CART_DATA = 'CART_DATA';
6 changes: 6 additions & 0 deletions src/app/store/actions/cartAction/cartAction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @flow strict
import * as types from '@/store/ActionTypes';

import makeActionCreator from '@/store/actions/makeActionCreator';

export default makeActionCreator(types.CART_DATA, 'cart');
11 changes: 11 additions & 0 deletions src/app/store/actions/cartAction/cartAction.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @flow strict

import cartAction from './cartAction';

describe('cartAction action', () => {
it('should return the quantity', () => {
const quantity = 0;
const expectedData = { type: 'CART_DATA', cart: { quantity } };
expect(cartAction({ quantity })).toEqual(expectedData);
});
});
3 changes: 3 additions & 0 deletions src/app/store/actions/cartAction/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @flow strict
// eslint-disable-next-line import/prefer-default-export
export { default as cartAction } from './cartAction';
19 changes: 19 additions & 0 deletions src/app/store/reducers/cart/cart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// @flow strict

import type { CartType, CartActionType } from '@/store/types/CartType';

import { CART_DATA } from '@/store/ActionTypes';

import createReducer from '../createReducer';

const initialState = {
quantity: 0,
};

export default createReducer<CartType, CartActionType>(initialState, {
[CART_DATA](state) {
return {
...state,
};
},
});
14 changes: 14 additions & 0 deletions src/app/store/reducers/cart/cart.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @flow strict
import cart from './cart';

describe('cart reducer', () => {
const expectedState = {
quantity: 0,
};

it('should return initial state', () => {
const initialState = cart(undefined, { type: '', quantity: 0 });

expect(initialState).toStrictEqual(expectedState);
});
});
9 changes: 9 additions & 0 deletions src/app/store/reducers/cart/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @flow strict

import type { State } from '@/store/types/State';

import cart from './cart';

export default cart;

export const cartFooter = (state: State) => state.cart;
6 changes: 4 additions & 2 deletions src/app/store/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import { combineReducers } from 'redux';

import type { Actions } from '@/store/types/Actions';

import products from './products';
import cart from './cart';
import footer from './footer';
import products from './products';

export default combineReducers<{}, Actions>({
products,
cart,
footer,
products,
});
8 changes: 5 additions & 3 deletions src/app/store/types/Actions.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// @flow strict

import type { ProductActionType } from '@/store/types/ProductsType';
import type { FooterActionType } from '@/store/types/FooterType';
import type { CartActionType } from '@/store/types/CartType';
import type { ProductActionType } from '@/store/types/ProductsType';

export type Actions =
ProductActionType |
FooterActionType;
CartActionType |
FooterActionType |
ProductActionType;

export type AsyncAction<S> = {
types?: Array<string>,
Expand Down
11 changes: 11 additions & 0 deletions src/app/store/types/CartType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @flow strict
import type { Action } from 'redux';

export type CartType = {
quantity: number
};

export type CartActionType = {
...$Exact<Action<string>>,
...$Exact<CartType>
};
6 changes: 4 additions & 2 deletions src/app/store/types/State.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// @flow strict

import type { ProductsType } from '@/store/types/ProductsType';
import type { CartType } from '@/store/types/CartType';
import type { FooterType } from '@/store/types/FooterType';
import type { ProductsType } from '@/store/types/ProductsType';

export type State = {
cart: CartType,
footer: FooterType,
products: ProductsType,
footer: FooterType
};

0 comments on commit 64eac60

Please sign in to comment.