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-6.2.0 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-6.1.1 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-6.0.2 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-6.1.0 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-6.0.1 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-5.1.3 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-6.0.0 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-5.1.2 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-5.1.1 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-5.1.0 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-4.0.3.5 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-4.0.3.4 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-4.0.3.3 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-4.0.3.2 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-5.0.1 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-4.0.5 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-4.0.3.1 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-4.0.4 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-5.0.0 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js
foreman_ansible-4.0.3 webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js