Sha256: ea8b070ed1b8ac9acc45c4dfc04287bb0be5f7031a16a0327b3c13d7be1f0e19

Contents?: true

Size: 1.98 KB

Versions: 34

Compression:

Stored size: 1.98 KB

Contents

import { differenceBy, slice, includes, uniq } from 'lodash';
import Immutable from 'seamless-immutable';
import { createSelector } from 'reselect';

const compare = (a, b) => {
  if (a.name < b.name) {
    return -1;
  }
  if (a.name > b.name) {
    return 1;
  }
  return 0;
};

const switcherState = state => state.foremanAnsible.ansibleRolesSwitcher;

const markInheritedRoles = (roles, inheritedRoleIds) =>
  roles.map(role =>
    includes(inheritedRoleIds, role.id) ? { ...role, inherited: true } : role
  );

export const selectResults = state =>
  Immutable(
    Immutable.asMutable(uniq(switcherState(state).results)).sort(compare)
  );

export const selectItemCount = state => switcherState(state).itemCount;

export const selectAssignedRoles = state =>
  Immutable.asMutable(
    markInheritedRoles(
      switcherState(state).assignedRoles,
      switcherState(state).inheritedRoleIds
    )
  ).sort(compare);

export const selectAssignedRolesCount = state =>
  selectAssignedRoles(state).length;
export const selectLoading = state => switcherState(state).loading;
export const selectAssignedPagination = state =>
  switcherState(state).assignedPagination;
export const selectError = state => switcherState(state).error;
export const selectPagination = state => switcherState(state).pagination;

export const selectPaginationMemoized = createSelector(
  selectPagination,
  selectResults,
  (pagination, results) =>
    results.length > pagination.perPage
      ? { ...pagination, perPage: results.length }
      : pagination
);

export const selectUnassignedRoles = createSelector(
  selectResults,
  selectAssignedRoles,
  (results, assignedRoles) => differenceBy(results, assignedRoles, 'id')
);

export const selectAssignedRolesPage = createSelector(
  selectAssignedPagination,
  selectAssignedRoles,
  (assignedPagination, assignedRoles) => {
    const offset = (assignedPagination.page - 1) * assignedPagination.perPage;

    return slice(assignedRoles, offset, offset + assignedPagination.perPage);
  }
);

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
foreman_ansible-4.0.2 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-4.0.0 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-3.0.9 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-3.0.8 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-3.0.7 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-3.0.6 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-3.0.5 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-3.0.4 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-3.0.3 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-2.3.4 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-3.0.2 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-3.0.1 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-3.0.0 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-2.3.3 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js