All files / publisher/pages/Releases/reducers channelMap.ts

100% Statements 24/24
88.88% Branches 16/18
100% Functions 5/5
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111                                4x   4x         4x         1x   3x     4x               1x         1x 2x     2x       2x       1x               2x 1x     1x 1x   1x                 33x   2x       4x           1x         1x           2x   23x      
import { AVAILABLE } from "../constants";
import {
  INIT_CHANNEL_MAP,
  SELECT_REVISION,
  CLEAR_SELECTED_REVISIONS,
  RELEASE_REVISION_SUCCESS,
  CLOSE_CHANNEL_SUCCESS,
  ChannelMapAction,
} from "../actions/channelMap";
import { ReleasesReduxState, Revision } from "../../../types/releaseTypes";
 
function selectRevision(
  state: ReleasesReduxState["channelMap"],
  revision: Revision,
  toggle: boolean
) {
  const arch = revision.architectures[0];
 
  state = {
    ...state,
    [AVAILABLE]: { ...state[AVAILABLE] },
  };
 
  if (
    toggle &&
    state[AVAILABLE][arch] &&
    state[AVAILABLE][arch].revision === revision.revision
  ) {
    delete state[AVAILABLE][arch];
  } else {
    state[AVAILABLE][arch] = { ...revision };
  }
 
  return state;
}
 
function releaseRevision(
  state: ReleasesReduxState["channelMap"],
  revision: Revision,
  channel: string
) {
  state = {
    ...state,
    [channel]: { ...state[channel] },
  };
 
  revision.architectures.forEach((arch) => {
    const currentlyReleased = state[channel][arch];
 
    // only update revision in channel map if it changed since last time
    Eif (
      !currentlyReleased ||
      currentlyReleased.revision !== revision.revision
    ) {
      state[channel][arch] = { ...revision };
    }
  });
 
  return state;
}
 
function closeChannel(
  state: ReleasesReduxState["channelMap"],
  channel: string
) {
  // if channel is already closed do nothing
  if (!state[channel]) {
    return state;
  }
 
  state = { ...state };
  delete state[channel];
 
  return state;
}
 
// contains channel map for each channel in current track
// also includes 'unassigned' fake channel to show selected unassigned revision
export default function channelMap(
  state: ReleasesReduxState["channelMap"] = {},
  action: ChannelMapAction
) {
  switch (action.type) {
    case INIT_CHANNEL_MAP:
      return {
        ...action.payload.channelMap,
      };
    case SELECT_REVISION:
      return selectRevision(
        state,
        action.payload.revision,
        action.payload.toggle
      );
    case CLEAR_SELECTED_REVISIONS:
      return {
        ...state,
        [AVAILABLE]: {},
      };
    case RELEASE_REVISION_SUCCESS:
      return releaseRevision(
        state,
        action.payload.revision,
        action.payload.channel
      );
    case CLOSE_CHANNEL_SUCCESS:
      return closeChannel(state, action.payload.channel);
    default:
      return state;
  }
}