Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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'
}),
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(
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,
),
}),
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;
}
}),
});
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)
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
}),
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) => {
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 }) =>
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')
),
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 }) => {