All files / publisher/release/actions availableRevisionsSelect.js

100% Statements 26/26
100% Branches 12/12
100% Functions 7/7
100% Lines 23/23

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                          4x     19x             16x 16x 16x   16x     16x 30x     16x       3x   3x   2x   6x       16x 3x   3x 2x   6x         16x 42x   42x 42x 28x          
import {
  AVAILABLE_REVISIONS_SELECT_RECENT,
  AVAILABLE_REVISIONS_SELECT_LAUNCHPAD,
  AVAILABLE_REVISIONS_SELECT_ALL,
} from "../constants";
import { selectRevision, clearSelectedRevisions } from "./channelMap";
import {
  getArchitectures,
  getFilteredAvailableRevisions,
  getFilteredAvailableRevisionsForArch,
} from "../selectors";
import { getBuildId } from "../helpers";
 
export const SET_AVAILABLE_REVISIONS_SELECT = "SET_AVAILABLE_REVISIONS_SELECT";
 
export function setAvailableRevisionsSelect(value) {
  return {
    type: SET_AVAILABLE_REVISIONS_SELECT,
    payload: { value },
  };
}
 
export function selectAvailableRevisions(value) {
  return (dispatch, getState) => {
    dispatch(setAvailableRevisionsSelect(value));
    dispatch(clearSelectedRevisions());
 
    const state = getState();
 
    // for each architecture
    const archs = getArchitectures(state);
    let revisionsFilter = () => true;
 
    // for Recent select only revisions from most recent uploaded version
    if (
      value === AVAILABLE_REVISIONS_SELECT_RECENT ||
      value === AVAILABLE_REVISIONS_SELECT_ALL
    ) {
      const recentRevisions = getFilteredAvailableRevisions(state);
 
      if (recentRevisions.length > 0) {
        // find most recent version
        const recentVersion = recentRevisions[0].version;
        // filter most recent revision with given version
        revisionsFilter = (revision) => revision.version === recentVersion;
      }
    }
 
    if (value === AVAILABLE_REVISIONS_SELECT_LAUNCHPAD) {
      const lpRevisions = getFilteredAvailableRevisions(state);
 
      if (lpRevisions.length > 0) {
        const recentBuild = getBuildId(lpRevisions[0]);
 
        revisionsFilter = (revision) => getBuildId(revision) === recentBuild;
      }
    }
 
    // get latest revision to select
    archs.forEach((arch) => {
      const revisions = getFilteredAvailableRevisionsForArch(state, arch);
 
      const revisionToSelect = revisions.filter(revisionsFilter)[0];
      if (revisionToSelect) {
        dispatch(selectRevision(revisionToSelect));
      }
    });
  };
}