lodash#findKey JavaScript Examples

The following examples show how to use lodash#findKey. 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: backfills.js    From flatris-LAB_V1 with MIT License 5 votes vote down vote up
export function backfillsReducer(
  state: Backfills = initialState,
  action: Action
): Backfills {
  switch (action.type) {
    case 'START_BACKFILL': {
      const { gameId, backfillId } = action.payload;

      return {
        ...state,
        [gameId]: {
          backfillId,
          queuedActions: []
        }
      };
    }

    case 'END_BACKFILL': {
      const { backfillId } = action.payload;

      const gameId = findKey(state, b => b.backfillId === backfillId);
      if (!gameId) {
        return state;
      }

      return omit(state, gameId);
    }

    case 'QUEUE_GAME_ACTION': {
      const { action: queuedAction } = action.payload;
      const { actionId, gameId } = queuedAction.payload;

      const backfill = state[gameId];
      if (!backfill) {
        console.warn(`Trying to queue action ${actionId} outside backfill`);

        return state;
      }

      const { backfillId, queuedActions } = backfill;

      return {
        ...state,
        [gameId]: {
          backfillId,
          queuedActions: [...queuedActions, queuedAction]
        }
      };
    }

    default:
      return state;
  }
}