How to use the easy-peasy.select function in easy-peasy

To help you get started, we’ve selected a few easy-peasy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github moltin / shopkit / src / model / modal.js View on Github external
import { action, select, thunk } from 'easy-peasy'

import { changeRoute } from '../utils'

export default {
  route: 'cart',
  // route: 'billing',
  open: false,
  // open: true,

  checkingOut: select(({ route }) => ['shipping', 'billing'].includes(route)),

  goToCart: changeRoute('cart'),
  goToShipping: changeRoute('shipping'),
  goToBilling: changeRoute('billing'),
  goToConfirmation: changeRoute('confirmation'),
  goToOrders: changeRoute('orders'),
  goToLogin: changeRoute('login'),

  toggle: action(state => {
    state.open = !state.open
  }),

  openCart: action(state => {
    state.open = true
    state.route = 'cart'
  }),
github moltin / shopkit / src / model / user.js View on Github external
import { action, thunk, select } from 'easy-peasy'

export default {
  id: null,
  token: null,
  orders: [],

  loggedIn: select(({ id, token }) => id && token),

  setCustomerId: action((state, customerId) => {
    state.id = customerId
  }),

  setCustomerToken: action((state, customerToken) => {
    state.token = customerToken
  }),

  // getUser: thunk(async (actions, {customerId, customerToken}, { injections: { api } }) => {
  //   const payload = await api.get(`carts/${id}/items`)

  //   actions.setCart(payload)
  // }),

  getOrders: thunk(
github ctrlplusb / easy-peasy / src / __tests__ / typescript / implementation.tsx View on Github external
body: JSON.stringify(payload),
      headers: {
        'Content-Type': 'application/json',
      },
    });
    const { token } = await response.json();
    actions.loggedIn(token);
  }),
};
const store = createStore({
  audit: {
    logs: [],
    set: action((state, payload) => {
      state.logs.push(payload);
    }),
    getNLog: select(state => (n: number) =>
      state.logs.length > n ? state.logs[n] : undefined,
    ),
    newUserListeners: listen(on => {
      on(
        userModel.login,
        thunk((actions, payload) => {
          actions.set(`Logging in ${payload.username}`);
        }),
      );
    }),
  },
  todos: {
    items: [],
    firstItem: select(state =>
      state.items.length > 0 ? state.items[0] : undefined,
    ),
github ctrlplusb / easy-peasy / src / __tests__ / typescript / implementation.tsx View on Github external
}),
    getNLog: select(state => (n: number) =>
      state.logs.length > n ? state.logs[n] : undefined,
    ),
    newUserListeners: listen(on => {
      on(
        userModel.login,
        thunk((actions, payload) => {
          actions.set(`Logging in ${payload.username}`);
        }),
      );
    }),
  },
  todos: {
    items: [],
    firstItem: select(state =>
      state.items.length > 0 ? state.items[0] : undefined,
    ),
    addTodo: action((state, payload) => {
      state.items.push(payload);
    }),
  },
  user: userModel,
  counter: reducer((state = 0, action) => {
    switch (action.type) {
      case 'COUNTER_INCREMENT':
        return state + 1;
      default:
        return state;
    }
  }),
});
github moltin / shopkit / src / model / cart.js View on Github external
items.reduce((sum, { quantity }) => sum + quantity, 0)
  ),

  cartItems: select(({ items }) =>
    items.filter(({ type }) => type === 'cart_item' || type === 'custom_item')
  ),

  taxItems: select(({ items }) =>
    items.filter(({ type }) => type === 'tax_item')
  ),

  promotionItems: select(({ items }) =>
    items.filter(({ type }) => type === 'promotion_item')
  ),

  subTotal: select(({ meta }) =>
    meta ? meta.display_price.without_tax.formatted : 0
  ),

  setCartId: action((state, cartId) => {
    state.id = cartId
  }),

  setCart: action((state, { data, meta }) => {
    state.items = data
    state.meta = meta
  }),

  getCart: thunk(async (actions, id, { injections: { api } }) => {
    const payload = await api.get(`carts/${id}/items`)

    actions.setCart(payload)
github moltin / shopkit / src / model / cart.js View on Github external
isEmpty: select(({ items }) => items.length === 0),

  count: select(({ items }) =>
    items.reduce((sum, { quantity }) => sum + quantity, 0)
  ),

  cartItems: select(({ items }) =>
    items.filter(({ type }) => type === 'cart_item' || type === 'custom_item')
  ),

  taxItems: select(({ items }) =>
    items.filter(({ type }) => type === 'tax_item')
  ),

  promotionItems: select(({ items }) =>
    items.filter(({ type }) => type === 'promotion_item')
  ),

  subTotal: select(({ meta }) =>
    meta ? meta.display_price.without_tax.formatted : 0
  ),

  setCartId: action((state, cartId) => {
    state.id = cartId
  }),

  setCart: action((state, { data, meta }) => {
    state.items = data
    state.meta = meta
  }),
github moltin / shopkit / src / model / cart.js View on Github external
import { action, thunk, select } from 'easy-peasy'

export default {
  id: null,
  meta: null,
  items: [],

  isEmpty: select(({ items }) => items.length === 0),

  count: select(({ items }) =>
    items.reduce((sum, { quantity }) => sum + quantity, 0)
  ),

  cartItems: select(({ items }) =>
    items.filter(({ type }) => type === 'cart_item' || type === 'custom_item')
  ),

  taxItems: select(({ items }) =>
    items.filter(({ type }) => type === 'tax_item')
  ),

  promotionItems: select(({ items }) =>
    items.filter(({ type }) => type === 'promotion_item')
  ),

  subTotal: select(({ meta }) =>
    meta ? meta.display_price.without_tax.formatted : 0
  ),

  setCartId: action((state, cartId) => {
github moltin / shopkit / src / model / cart.js View on Github external
import { action, thunk, select } from 'easy-peasy'

export default {
  id: null,
  meta: null,
  items: [],

  isEmpty: select(({ items }) => items.length === 0),

  count: select(({ items }) =>
    items.reduce((sum, { quantity }) => sum + quantity, 0)
  ),

  cartItems: select(({ items }) =>
    items.filter(({ type }) => type === 'cart_item' || type === 'custom_item')
  ),

  taxItems: select(({ items }) =>
    items.filter(({ type }) => type === 'tax_item')
  ),

  promotionItems: select(({ items }) =>
    items.filter(({ type }) => type === 'promotion_item')
  ),

  subTotal: select(({ meta }) =>
github moltin / shopkit / src / model / cart.js View on Github external
import { action, thunk, select } from 'easy-peasy'

export default {
  id: null,
  meta: null,
  items: [],

  isEmpty: select(({ items }) => items.length === 0),

  count: select(({ items }) =>
    items.reduce((sum, { quantity }) => sum + quantity, 0)
  ),

  cartItems: select(({ items }) =>
    items.filter(({ type }) => type === 'cart_item' || type === 'custom_item')
  ),

  taxItems: select(({ items }) =>
    items.filter(({ type }) => type === 'tax_item')
  ),

  promotionItems: select(({ items }) =>
    items.filter(({ type }) => type === 'promotion_item')
  ),
github moltin / shopkit / src / model / cart.js View on Github external
export default {
  id: null,
  meta: null,
  items: [],

  isEmpty: select(({ items }) => items.length === 0),

  count: select(({ items }) =>
    items.reduce((sum, { quantity }) => sum + quantity, 0)
  ),

  cartItems: select(({ items }) =>
    items.filter(({ type }) => type === 'cart_item' || type === 'custom_item')
  ),

  taxItems: select(({ items }) =>
    items.filter(({ type }) => type === 'tax_item')
  ),

  promotionItems: select(({ items }) =>
    items.filter(({ type }) => type === 'promotion_item')
  ),

  subTotal: select(({ meta }) =>
    meta ? meta.display_price.without_tax.formatted : 0
  ),

  setCartId: action((state, cartId) => {
    state.id = cartId
  }),

  setCart: action((state, { data, meta }) => {