Skip to content

Instantly share code, notes, and snippets.

View timdeschryver's full-sized avatar
👟

Tim Deschryver timdeschryver

👟
View GitHub Profile
@timdeschryver
timdeschryver / carbon-example.js
Created April 22, 2018 07:14
carbon-example.js
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, [])
// State
export interface State {
customers: Customer[];
selectedCustomer: Customer;
}
// Reducer
export function reducer(state, action) {
switch (action.type) {
// State
export interface State {
customers: { [id: string]: Customer };
selectedCustomerId: string;
}
// Reducer
export function reducer(state, action) {
switch (action.type) {
...
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;
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,
},
};
// for each item in our cart we're storing the amount.
export interface State {
cartItems: { [sku: string]: number };
}
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);
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 }) {}
}
export interface CartStateModel {
cartItems: { [sku: string]: number };
}
@State<CartStateModel>({
name: 'cartItems',
defaults: {
cartItems: {},
},
})
export class CartState {
@Action(AddToCart)
addProduct(ctx: StateContext<CartStateModel>, action: AddToCart) {
const state = ctx.getState();