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
const pluckDeep = key => obj => key.split('.').reduce((accum, key) => accum[key], obj) | |
const compose = (...fns) => res => fns.reduce((accum, next) => next(accum), res) | |
const unfold = (f, seed) => { | |
const go = (f, seed, acc) => { | |
const res = f(seed) | |
return res ? go(f, res[1], acc.concat([res[0]])) : acc | |
} | |
return go(f, seed, []) |
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
// State | |
export interface State { | |
customers: Customer[]; | |
selectedCustomer: Customer; | |
} | |
// Reducer | |
export function reducer(state, action) { | |
switch (action.type) { |
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
// State | |
export interface State { | |
customers: { [id: string]: Customer }; | |
selectedCustomerId: string; | |
} | |
// Reducer | |
export function reducer(state, action) { | |
switch (action.type) { | |
... |
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
export const reducer = produce<State, CartActions>((draft, action) => { | |
switch (action.type) { | |
case CartActionTypes.AddToCart: | |
draft.cartItems[action.payload.sku] = (draft.cartItems[action.payload.sku] || 0) + 1; | |
return; | |
case CartActionTypes.RemoveFromCart: | |
const newAmount = draft.cartItems[action.payload.sku] - 1; | |
if (newAmount > 0) { | |
draft.cartItems[action.payload.sku] = newAmount; |
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
export function reducer(state = initialState, action: CartActions) { | |
switch (action.type) { | |
case CartActionTypes.AddToCart: | |
return { | |
...state, | |
cartItems: { | |
...state.cartItems, | |
[action.payload.sku]: (state.cartItems[action.payload.sku] || 0) + 1, | |
}, | |
}; |
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
// for each item in our cart we're storing the amount. | |
export interface State { | |
cartItems: { [sku: string]: number }; | |
} |
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
export const reducer = produce<State, CatalogActions>((draft, action) => { | |
switch (action.type) { | |
case CatalogActionTypes.Load: | |
action.payload.products.forEach(product => { | |
draft.products[product.sku] = product; | |
draft.productSkus.push(product.sku); | |
}); | |
} | |
}, initialState); |
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
export class AddToCart { | |
static readonly type = '[Product List] Add to cart'; | |
constructor(public payload: { sku: string }) {} | |
} | |
export class RemoveFromCart { | |
static readonly type = '[Product List] Remove from cart'; | |
constructor(public payload: { sku: string }) {} | |
} |
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
export interface CartStateModel { | |
cartItems: { [sku: string]: number }; | |
} |
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
@State<CartStateModel>({ | |
name: 'cartItems', | |
defaults: { | |
cartItems: {}, | |
}, | |
}) | |
export class CartState { | |
@Action(AddToCart) | |
addProduct(ctx: StateContext<CartStateModel>, action: AddToCart) { | |
const state = ctx.getState(); |
OlderNewer