import { createSelector as createMemoizedSelector } from "reselect"; import * as _ from "lodash"; import type { ItemMap, SubstateItemData, SubstateListData } from "./types"; /** * The options type for newAsyncDataSelectors and newAsyncListSelectors. Currently the only option * is for specifying the default item value. * * @template I The item type for this group of selectors */ export type SelectorOptions = { itemDefault: I | null; }; /** * @template [S=any] The state that will be passed to this selector. This can be the type for the whole store or * an interface that defines only the slice of the store state needed by these selectors. * @template [I=any] The type for an item returned by these selectors. * @template [E=any] The error type for an item's failed load. */ export const newAsyncDataSelectors = ( fieldPath: string, options: SelectorOptions = { itemDefault: null }, ) => { const itemState = (state: S): SubstateItemData => _.get(state, fieldPath, {} as SubstateItemData); return { itemLoading: (state: S): boolean => !!itemState(state).isLoading, itemLoadError: (state: S): E | undefined => itemState(state).error, itemLoaded: (state: S): boolean => !!itemState(state).lastUpdated, itemLastUpdated: (state: S): Date | undefined => itemState(state).lastUpdated, item: (state: S): I => itemState(state).data || options.itemDefault, }; }; /** * @template [S=any] The state that will be passed to this selector. This can be the type for the whole store or * an interface that defines only the slice of the store state needed by these selectors. * @template [I=any] The type for an item returned by these selectors. * @template [E=any] The error type for an item's failed load. * @template [LE=E] The error for failing to load a collection of items. */ export const newAsyncListSelectors = ( subKeyPath: string, options: SelectorOptions = { itemDefault: null }, ) => { const listState = (state: S): SubstateListData => _.get(state, subKeyPath, {}) as SubstateListData; const listData = (state: S): ItemMap => listState(state).data || {}; const itemState = (state: S, id: string): SubstateItemData => listData(state)[id] || {}; const itemLoaded = (state: S, id: string): boolean => !!itemState(state, id).lastUpdated; return { listLoading: (state: S): boolean => !!listState(state).isLoading, listLoadError: (state: S): LE => listState(state).error, listLoaded: (state: S): boolean => !!listState(state).lastUpdated, listLastUpdated: (state: S): Date | undefined => listState(state).lastUpdated, list: createMemoizedSelector(listData, (data: ItemMap): I[] => _.map(data, (value) => value.data || options.itemDefault), ), itemsByID: createMemoizedSelector(listData, (data: ItemMap): { [key: string]: I } => _.mapValues(data, (value) => value.data || options.itemDefault), ), itemLoading: (state: S, id: string): boolean => !!itemState(state, id).isLoading, itemLoadError: (state: S, id: string): E | undefined => itemState(state, id).error, itemLoaded: (state: S, id: string): boolean => itemLoaded(state, id), itemLastUpdated: (state: S, id: string): Date | undefined => itemState(state, id).lastUpdated, itemsLoaded: (state: S, ids: string[]): boolean => _.reduce(ids, (loaded, id) => loaded && itemLoaded(state, id), true), item: (state: S, id: string): I => itemState(state, id).data || options.itemDefault, }; };