Commit e272b493 authored by Dmytro Zavgorodniy's avatar Dmytro Zavgorodniy

[RNA-761] Add latest selectors

parent e30ea084
import { connect } from 'react-redux'
import { createSelector } from 'reselect'
import { denormalize } from 'normalizr'
import {
entitiesSelector,
relationsSelector,
metaSelector,
createSearchInventoryListSelector,
createStoreSelector,
createStoreCartOrderSelector,
entitiesSelector,
createStoreCartSelector,
} from 'tipsi_api/selectors'
import { searchInventory, clearPaginationCache } from 'tipsi_api/actions'
import { Schemas } from 'tipsi_api/middleware'
import { withNavigation } from '@expo/ex-navigation'
import { STORE_SEARCH_KEY } from '../actions'
import { createStoreCartStatusSelector } from './storeCarts'
const getStoreId = (state, props) => props.storeId
const selectorsWithStoreId = createSelector(
createStoreSelector(getStoreId),
createStoreCartOrderSelector(getStoreId),
createStoreCartStatusSelector(getStoreId),
({ store }, order = {}, storeCart) => ({ store, order, storeCart })
createStoreCartSelector(getStoreId),
({ store }, storeCart) => ({ store, storeCart })
)
/* eslint-disable import/prefer-default-export */
......@@ -27,20 +30,43 @@ export const drinkListSelector = ({ component, drinkType, listLabel }) => {
selectorsWithStoreId,
createSearchInventoryListSelector((state, props) => mapPropsToKey(props)),
entitiesSelector,
({ state, props }, { store, order, storeCart }, { result, ...rest }, entities) => ({
...rest,
store,
storeCart,
searchQuery: state.searchQueries[STORE_SEARCH_KEY],
storeFilters: state.storeFilters[mapPropsToKey(props)],
orderProducts: entities.orderProducts,
orderProductsInventory: state.relations.orderProductsInventory[order.id],
itemsCount: result.length,
inventoryType: drinkType,
storeDrinks: {
[listLabel]: result,
},
})
relationsSelector,
metaSelector,
(
{ state, props },
{ store, storeCart },
{ result, isLoading, nextPage },
entities,
relations,
meta
) => {
const combinedResult = result.map(({ inventory }) => {
const { id: inventoryId } = inventory
let orderProduct
const orderProductId = relations.storeCartItems.inventory[inventoryId]
if (orderProductId && storeCart.orderProductIds.includes(orderProductId)) {
orderProduct = denormalize(orderProductId, Schemas.ORDER_PRODUCT, entities)
}
const isUpdatingInventory = meta.storeCartInventory.fetchStatus[inventoryId] || false
return { inventory, isUpdatingInventory, orderProduct }
})
return {
isLoading,
nextPage,
store,
searchQuery: state.searchQueries[STORE_SEARCH_KEY],
storeFilters: state.storeFilters[mapPropsToKey(props)],
itemsCount: combinedResult.length,
storeDrinks: {
[listLabel]: combinedResult,
},
isLoadingStoreCart: storeCart.isLoading,
inventoryType: drinkType,
}
}
)
const mapDispatchToProps = (dispatch, props) => ({
......@@ -52,5 +78,5 @@ export const drinkListSelector = ({ component, drinkType, listLabel }) => {
),
})
return connect(mapStateToProps, mapDispatchToProps)(component) //withNavigation
return connect(mapStateToProps, mapDispatchToProps)(withNavigation(component))
}
export * from './storeCarts'
export * from './userCards'
export * from './userAddresses'
export * from './wineListSelector'
export * from './userContacts'
export * from './drinkListSelector'
export * from './promoInventorySelector'
export * from './payments'
export * from './userAgeConfirmation'
export * from './currentLocation'
export * from './storeCarts'
......@@ -2,7 +2,7 @@ import { createSelector } from 'reselect'
export const paymentsSelector = state => state.payments
export const createPaymentSelector = orderIdSelector => createSelector(
[paymentsSelector, orderIdSelector],
(payments, orderId) => ({ ...payments[orderId] })
export const createPaymentSelector = storeIdSelector => createSelector(
[paymentsSelector, storeIdSelector],
(payments, storeId) => ({ ...payments[storeId] })
)
import { createSelector } from 'reselect'
import { denormalize } from 'normalizr'
import { lowerCase, groupBy } from 'lodash'
import {
entitiesSelector,
relationsSelector,
metaSelector,
createSearchInventoryListSelector,
createStoreCartOrderSelector,
createStoreSelector,
entitiesSelector,
userSelector,
createStoreCartSelector,
} from 'tipsi_api/selectors'
import { createStoreCartStatusSelector } from './storeCarts'
import { Schemas } from 'tipsi_api/middleware'
const getPromoId = (state, props) => props.promoId
const getStoreId = (state, props) => props.storeId
const getPromoId = (state, props) => props.promoId
const selectorsWithStoreId = createSelector(
createStoreSelector(getStoreId),
createStoreCartOrderSelector(getStoreId),
createStoreCartStatusSelector(getStoreId),
createSearchInventoryListSelector(getPromoId),
({ store }, order = {}, storeCart, { result, nextPage, isLoading }) => ({
createStoreSelector(getStoreId),
createStoreCartSelector(getStoreId),
({ result, isLoading, nextPage }, { store }, storeCart) => ({
result,
isLoading,
nextPage,
store,
order,
storeCart,
})
)
/* eslint-disable import/prefer-default-export */
export const promoInventorySelector = tabs => createSelector(
[userSelector, selectorsWithStoreId, state => state, entitiesSelector],
({ user }, { result, storeCart, order, ...rest }, state, entities) => {
const promo = groupBy(result, ({ inventory: { wine, drink } }) => (
[state => state, selectorsWithStoreId, entitiesSelector, relationsSelector, metaSelector],
(state, { result, store, storeCart }, entities, relations, meta) => {
const combinedResult = result.map(({ inventory }) => {
const { id: inventoryId } = inventory
let orderProduct
const orderProductId = relations.storeCartItems.inventory[inventoryId]
if (orderProductId && storeCart.orderProductIds.includes(orderProductId)) {
orderProduct = denormalize(orderProductId, Schemas.ORDER_PRODUCT, entities)
}
const isUpdatingInventory = !!meta.storeCartInventory.fetchStatus[inventoryId]
return { inventory, isUpdatingInventory, orderProduct }
})
const promo = groupBy(combinedResult, ({ inventory: { wine, drink } }) => (
wine ? 'wine' : drink.drink_type
))
const routes = !result.length ? [] : tabs.filter(route => promo[lowerCase(route.key)])
const routes = !combinedResult.length ? [] : tabs.filter(route => promo[lowerCase(route.key)])
return {
...rest,
user,
order,
store,
routes,
storeCart,
orderProducts: entities.orderProducts,
orderProductsInventory: state.relations.orderProductsInventory[order.id],
promoInventory: promo,
isLoadingStoreCart: storeCart.isLoading,
}
}
)
import { createSelector } from 'reselect'
export const storeCartsSelector = state => state.storeCarts
const storeCartsSelector = state => state.storeCarts
export const defaultInventoryCartStatuses = {
isAddingToCart: false,
isIncreasingCount: false,
isDecreasingCount: false,
isRemovingFromCart: false,
}
export const createStoreCartStatusSelector = getStoreId => createSelector(
[getStoreId, storeCartsSelector],
(storeId, storeCarts) => storeCarts[storeId] || { isLoadingCart: false }
)
export const createInventoryCartStatusSelector = (
storeIdSelector,
inventoryIdSelector
) => createSelector(
inventoryIdSelector,
createStoreCartStatusSelector(storeIdSelector),
(inventoryId, storeCart) => storeCart[inventoryId] || defaultInventoryCartStatuses
)
export const createCartStatusSelector = (storeIdSelector, inventoryIdSelector) => createSelector(
createStoreCartStatusSelector(storeIdSelector),
createInventoryCartStatusSelector(storeIdSelector, inventoryIdSelector),
(storeCart, inventoryCartStatus) => ({ ...storeCart, ...inventoryCartStatus })
/* eslint-disable import/prefer-default-export */
export const createModifiedStoreCartSelector = storeIdSelector => createSelector(
[storeIdSelector, storeCartsSelector],
(storeId, storeCarts) => storeCarts[storeId] || {}
)
import { createSelector } from 'reselect'
export const userContactsSelector = state => state.userContacts
export const createSelectedContactSelector = createSelector(
userContactsSelector,
({ byId, selected }) => ({
contact: byId[selected],
hasContacts: !!Object.keys(byId).length,
})
)
import { createSelector } from 'reselect'
import {
createSearchInventoryListSelector,
createStoreSelector,
createStoreCartOrderSelector,
entitiesSelector,
} from 'tipsi_api/selectors'
import { STORE_SEARCH_KEY } from '../actions'
import { createStoreCartStatusSelector } from './storeCarts'
const getStoreId = (state, props) => props.storeId
const selectorsWithStoreId = createSelector(
createSearchInventoryListSelector((state, props) => `${props.storeId}/wines`),
createStoreSelector(getStoreId),
createStoreCartOrderSelector(getStoreId),
createStoreCartStatusSelector(getStoreId),
({ result, isLoading, nextPage }, { store }, order = {}, storeCart) => ({
result,
isLoading,
nextPage,
store,
order,
storeCart,
})
)
/* eslint-disable import/prefer-default-export */
export const wineListSelector = createSelector(
[(state, props) => ({ state, props }), selectorsWithStoreId, entitiesSelector],
({ state, props }, { result, storeCart, order, ...rest }, entities) => ({
...rest,
storeCart,
searchQuery: state.searchQueries[STORE_SEARCH_KEY],
storeFilters: state.storeFilters[`${props.storeId}/wines`],
orderProducts: entities.orderProducts,
orderProductsInventory: state.relations.orderProductsInventory[order.id],
itemsCount: result.length,
storeWines: {
'Store Wines': result,
},
})
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment