From 64eac604abe1dce16370139f6573761d55bfa9e6 Mon Sep 17 00:00:00 2001 From: jero Date: Mon, 19 Aug 2019 23:39:40 -0500 Subject: [PATCH] feat(store): add cart state create cart reducer, action, actionType --- src/app/store/ActionTypes.js | 2 ++ .../store/actions/cartAction/cartAction.js | 6 ++++++ .../actions/cartAction/cartAction.test.js | 11 +++++++++++ src/app/store/actions/cartAction/index.js | 3 +++ src/app/store/reducers/cart/cart.js | 19 +++++++++++++++++++ src/app/store/reducers/cart/cart.test.js | 14 ++++++++++++++ src/app/store/reducers/cart/index.js | 9 +++++++++ src/app/store/reducers/index.js | 6 ++++-- src/app/store/types/Actions.js | 8 +++++--- src/app/store/types/CartType.js | 11 +++++++++++ src/app/store/types/State.js | 6 ++++-- 11 files changed, 88 insertions(+), 7 deletions(-) create mode 100644 src/app/store/actions/cartAction/cartAction.js create mode 100644 src/app/store/actions/cartAction/cartAction.test.js create mode 100644 src/app/store/actions/cartAction/index.js create mode 100644 src/app/store/reducers/cart/cart.js create mode 100644 src/app/store/reducers/cart/cart.test.js create mode 100644 src/app/store/reducers/cart/index.js create mode 100644 src/app/store/types/CartType.js diff --git a/src/app/store/ActionTypes.js b/src/app/store/ActionTypes.js index 7a4b6a90..8e578e3f 100644 --- a/src/app/store/ActionTypes.js +++ b/src/app/store/ActionTypes.js @@ -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'; diff --git a/src/app/store/actions/cartAction/cartAction.js b/src/app/store/actions/cartAction/cartAction.js new file mode 100644 index 00000000..ecdfd728 --- /dev/null +++ b/src/app/store/actions/cartAction/cartAction.js @@ -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'); diff --git a/src/app/store/actions/cartAction/cartAction.test.js b/src/app/store/actions/cartAction/cartAction.test.js new file mode 100644 index 00000000..ca817cac --- /dev/null +++ b/src/app/store/actions/cartAction/cartAction.test.js @@ -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); + }); +}); diff --git a/src/app/store/actions/cartAction/index.js b/src/app/store/actions/cartAction/index.js new file mode 100644 index 00000000..ba836fb4 --- /dev/null +++ b/src/app/store/actions/cartAction/index.js @@ -0,0 +1,3 @@ +// @flow strict +// eslint-disable-next-line import/prefer-default-export +export { default as cartAction } from './cartAction'; diff --git a/src/app/store/reducers/cart/cart.js b/src/app/store/reducers/cart/cart.js new file mode 100644 index 00000000..ec0e3426 --- /dev/null +++ b/src/app/store/reducers/cart/cart.js @@ -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(initialState, { + [CART_DATA](state) { + return { + ...state, + }; + }, +}); diff --git a/src/app/store/reducers/cart/cart.test.js b/src/app/store/reducers/cart/cart.test.js new file mode 100644 index 00000000..40475d10 --- /dev/null +++ b/src/app/store/reducers/cart/cart.test.js @@ -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); + }); +}); diff --git a/src/app/store/reducers/cart/index.js b/src/app/store/reducers/cart/index.js new file mode 100644 index 00000000..7879b5f7 --- /dev/null +++ b/src/app/store/reducers/cart/index.js @@ -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; diff --git a/src/app/store/reducers/index.js b/src/app/store/reducers/index.js index 9f779684..e7f19c47 100644 --- a/src/app/store/reducers/index.js +++ b/src/app/store/reducers/index.js @@ -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, }); diff --git a/src/app/store/types/Actions.js b/src/app/store/types/Actions.js index 1b624b57..ba8464c9 100644 --- a/src/app/store/types/Actions.js +++ b/src/app/store/types/Actions.js @@ -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 = { types?: Array, diff --git a/src/app/store/types/CartType.js b/src/app/store/types/CartType.js new file mode 100644 index 00000000..044ac04f --- /dev/null +++ b/src/app/store/types/CartType.js @@ -0,0 +1,11 @@ +// @flow strict +import type { Action } from 'redux'; + +export type CartType = { + quantity: number +}; + +export type CartActionType = { + ...$Exact>, + ...$Exact +}; diff --git a/src/app/store/types/State.js b/src/app/store/types/State.js index c0c3cedb..156d9edc 100644 --- a/src/app/store/types/State.js +++ b/src/app/store/types/State.js @@ -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 };