export function reducer()

in web/frontend/src/app/pages/streams/store/streams-list/streams.reducer.ts [42:217]


export function reducer(state = initialState, action: StreamsActions): State {
  let selectedStream,
    selectedStreamIndex /*, tabs: TabModel[], selectedTab: TabModel, selectedTabIndex: number*/;
  switch (action.type) {
    case StreamsActionTypes.SET_STREAMS:
      return {
        ...state,
        streams: action.payload.streams.sort(streamSorter),
      };

    case StreamsActionTypes.SET_STREAM_DESCRIBE:
      return {
        ...state,
        lasStreamDescribe: action.payload.describe,
      };

    case StreamsActionTypes.DELETE_STREAM:
      if (!state.streams) {
        return {...state};
      }
      selectedStreamIndex = state.streams?.findIndex(
        (stream) => stream.key === action.payload.streamKey,
      );
      if (selectedStreamIndex > -1) {
        if (!action.payload.spaceName) {
          state.streams.splice(selectedStreamIndex, 1);
        } else if (state.streams[selectedStreamIndex]._spacesList) {
          state.streams[selectedStreamIndex]._spacesList = state.streams[
            selectedStreamIndex
          ]._spacesList.filter((space) => space.name !== action.payload.spaceName);
        }
      }
      return {
        ...state,
        streams: [...state.streams] /*.sort(streamSorter)*/,
      };

    case StreamsActionTypes.RENAME_STREAM:
      selectedStreamIndex = state.streams?.findIndex(
        (stream) => stream.key === action.payload.streamId,
      );
      if (selectedStreamIndex > -1) {
        if (!action.payload.spaceName) {
          state.streams[selectedStreamIndex] = {
            ...state.streams[selectedStreamIndex],
            key: action.payload.newName,
            name: action.payload.newName,
          };
        } else if (state.streams[selectedStreamIndex]._spacesList) {
          const SPACE_INDEX = state.streams[selectedStreamIndex]._spacesList.findIndex(
            (space) => space.name === action.payload.spaceName,
          );
          if (SPACE_INDEX > -1) {
            state.streams[selectedStreamIndex]._spacesList[SPACE_INDEX] = {
              ...state.streams[selectedStreamIndex]._spacesList[SPACE_INDEX],
              name: action.payload.newName,
            };
          }
        }
      }
      return {
        ...state,
        streams: [...(state.streams || [])].sort(streamSorter),
      };

    case StreamsActionTypes.RENAME_SYMBOL:
      selectedStreamIndex = state.streams.findIndex(
        (stream) => stream.key === action.payload.streamId,
      );
      if (
        selectedStreamIndex > -1 &&
        state.streams[selectedStreamIndex]._symbolsList &&
        state.streams[selectedStreamIndex]._symbolsList.length
      ) {
        const symbolIndex = state.streams[selectedStreamIndex]._symbolsList.findIndex(
          (symbol) => symbol === action.payload.oldSymbolName,
        );
        if (symbolIndex > -1) {
          state.streams[selectedStreamIndex]._symbolsList.splice(
            symbolIndex,
            1,
            action.payload.newSymbolName,
          );
          state.streams[selectedStreamIndex] = {
            ...state.streams[selectedStreamIndex],
            _symbolsList: [...state.streams[selectedStreamIndex]._symbolsList],
          };
        }
      }
      return {
        ...state,
        streams: [...state.streams].sort(streamSorter),
      };

    case StreamsActionTypes.SET_SYMBOLS:
      selectedStream = state.streams.find((stream) => stream.key === action.payload.streamKey);
      const symbolList = action.payload.symbols.sort();
      if (selectedStream) {
        if (typeof action.payload.spaceName === 'string') {
          const currentSpace = selectedStream._spacesList.find(
            (space) => space.name === action.payload.spaceName,
          );
          if (currentSpace) {
            currentSpace._symbolsList = symbolList;
            currentSpace._shown = true;
          }
          delete selectedStream._symbolsList;
        } else {
          selectedStream._symbolsList = symbolList;
          selectedStream._shown = selectedStream._active = true;
          delete selectedStream._spacesList;
        }
      }
      return {
        ...state,
        streams: [...state.streams],
      };

    case StreamsActionTypes.SET_SPACES:
      selectedStream = state.streams.find((stream) => stream.key === action.payload.streamKey);
      if (selectedStream) {
        delete selectedStream._symbolsList;
        selectedStream._spacesList = [...action.payload.spaces];
        selectedStream._shown = selectedStream._active = true;
      }
      return {
        ...state,
        streams: [...state.streams],
      };

    case StreamsActionTypes.SET_STREAM_STATE:
      selectedStreamIndex = state.streams.findIndex(
        (stream) => stream.key === action.payload.stream.key,
      );
      if (selectedStreamIndex > -1) {
        const STREAM = state.streams[selectedStreamIndex];
        if (typeof action.payload.spaceName === 'string' && STREAM._spacesList) {
          const CURRENT_SPACE_IDX = STREAM._spacesList.findIndex(
            (space) => space.name === action.payload.spaceName,
          );
          if (CURRENT_SPACE_IDX > -1) {
            STREAM._spacesList[CURRENT_SPACE_IDX] = {
              ...STREAM._spacesList[CURRENT_SPACE_IDX],
              ...action.payload.props,
            };
            STREAM._shown = STREAM._active = true;
          }
          state.streams[selectedStreamIndex] = {...STREAM};
        } else {
          state.streams[selectedStreamIndex] = {
            ...STREAM,
            ...action.payload.props,
          };
        }
      }
      return {
        ...state,
        streams: [...state.streams],
      };

    case StreamsActionTypes.SET_NAVIGATION_STATE:
      return {
        ...state,
        ...(action.payload ? action.payload : {}),
      };

    case StreamsActionTypes.SET_STREAM_STATES_SUBSCRIPTION:
      return {
        ...state,
        dbState: action.payload.dbState,
      };

    default:
      return state;
  }
}