lodash#nth TypeScript Examples

The following examples show how to use lodash#nth. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: createTableHook.ts    From advocacy-maps with MIT License 4 votes vote down vote up
export function createTableHook<Item, Refinement, PageKey>({
  name,
  getPageKey,
  getItems
}: Config<Item, Refinement, PageKey>) {
  function reducer(
    state: State<Refinement, PageKey>,
    action: Action<Item, Refinement>
  ): State<Refinement, PageKey> {
    if (action.type === "nextPage" || action.type === "previousPage") {
      const next = state.currentPage + (action.type === "nextPage" ? 1 : -1)
      if (next >= 0 && next < state.pageKeys.length) {
        return {
          ...state,
          currentPage: next,
          currentPageKey: state.pageKeys[next],
          ...adjacentKeys(state.pageKeys, next)
        }
      } else {
        return state
      }
    } else if (action.type === "onSuccess") {
      const keys = [...state.pageKeys]
      const item = nth(action.page, state.itemsPerPage - 1)
      if (item !== undefined)
        keys[state.currentPage + 1] = getPageKey(item, state.refinement)
      return {
        ...state,
        pageKeys: keys,
        ...adjacentKeys(keys, state.currentPage)
      }
    } else if (action.type === "error") {
      console.warn(`Error in ${name} table hook`, action.error)
      return { ...state, error: action.error }
    } else if (action.type === "refine") {
      return {
        ...state,
        refinement: { ...state.refinement, ...action.refinement },
        ...initialPage
      }
    }
    return state
  }

  return (initialRefinement: Refinement) => {
    const [
      {
        refinement,
        itemsPerPage,
        currentPageKey,
        currentPage,
        nextKey,
        previousKey
      },
      dispatch
    ] = useReducer(reducer, {
      ...initialPage,
      itemsPerPage: 10,
      refinement: initialRefinement,
      error: null
    })

    const items = useAsync(
      () => {
        return getItems(refinement, itemsPerPage, currentPageKey)
      },
      [currentPageKey, refinement, itemsPerPage],
      {
        onSuccess: page => dispatch({ type: "onSuccess", page }),
        onError: error => dispatch({ type: "error", error })
      }
    )

    const pagination = useMemo(
      () => ({
        itemsPerPage,
        currentPage: currentPage + 1,
        nextPage: () => dispatch({ type: "nextPage" }),
        previousPage: () => dispatch({ type: "previousPage" }),
        hasNextPage: nextKey !== undefined,
        hasPreviousPage: previousKey !== undefined
      }),
      [currentPage, itemsPerPage, nextKey, previousKey]
    )

    return useMemo(
      () => ({
        pagination,
        items,
        refinement,
        refine: (refinement: Refinement) =>
          dispatch({ type: "refine", refinement })
      }),
      [pagination, items, refinement]
    )
  }
}