Skip to content

Commit

Permalink
UIPFU-77 - fix review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Terala-Priyanka committed Jan 11, 2024
1 parent 5ad35b6 commit a016d97
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 637 deletions.
51 changes: 48 additions & 3 deletions src/UserSearchContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {

import filterConfig, { filterConfigWithUserAssignedStatus } from './filterConfig';
import { updateResourceData } from './utils';
import { UAS } from './constants';
import {
ASSIGNED_FILTER_KEY,
UNASSIGNED_FILTER_KEY,
UAS,
} from './constants';

const INITIAL_RESULT_COUNT = 30;
const RESULT_COUNT_INCREMENT = 30;
Expand All @@ -36,7 +40,7 @@ const compileQuery = template(

export function buildQuery(queryParams, pathComponents, resourceData, logger, props) {
const filters = props.initialSelectedUsers ? filterConfigWithUserAssignedStatus : filterConfig;
const updatedResourceData = props.initialSelectedUsers && resourceData?.query?.filters?.substring(`${UAS}`) ? updateResourceData(resourceData) : resourceData;
const updatedResourceData = props.initialSelectedUsers && resourceData?.query?.filters?.includes(`${UAS}`) ? updateResourceData(resourceData) : resourceData;

return makeQueryFunction(
'cql.allRecords=1',
Expand Down Expand Up @@ -120,6 +124,7 @@ class UserSearchContainer extends React.Component {
*/
// eslint-disable-next-line react/no-unused-prop-types
tenantId: PropTypes.string.isRequired,
initialSelectedUsers: PropTypes.object,
}

constructor(props) {
Expand Down Expand Up @@ -175,6 +180,46 @@ class UserSearchContainer extends React.Component {
return get(this.props.resources, 'query', {});
}

getUsers = () => {
const {
resources,
initialSelectedUsers,
} = this.props;
const fetchedUsers = get(resources, 'records.records', []);
const activeFilters = get(resources, 'query.filters', '');

if (activeFilters.includes(`${UAS}`)) {
const assignedUsers = Object.values(initialSelectedUsers);
const assignedUserIds = Object.keys(initialSelectedUsers);
const hasBothUASFilters = activeFilters.includes(`${ASSIGNED_FILTER_KEY}`) && activeFilters.includes(`${UNASSIGNED_FILTER_KEY}`);
const hasNoneOfUASFilters = !activeFilters.includes(`${ASSIGNED_FILTER_KEY}`) && !activeFilters.includes(`${UNASSIGNED_FILTER_KEY}`);

if (hasBothUASFilters || hasNoneOfUASFilters) {
return fetchedUsers;
}
const uasFilterValue = activeFilters.split(',').filter(f => f.includes(`${UAS}`))[0].split('.')[1];

let otherFilterGroups = activeFilters.split(',').filter(f => !f.includes(`${UAS}`)).map(f => f.split('.')[0]);
if (otherFilterGroups.indexOf('pg') !== -1) {
otherFilterGroups = otherFilterGroups.with(otherFilterGroups.indexOf('pg'), 'patronGroup');
}

let otherFilterValues = activeFilters.split(',').filter(f => !f.includes(`${UAS}`)).map(f => f.split('.')[1]);
if (otherFilterValues.indexOf('active') !== -1) {
otherFilterValues = otherFilterValues.with(otherFilterValues.indexOf('active'), true);
}

if (uasFilterValue === 'Assigned') {
if (!otherFilterGroups.length) return assignedUsers;
return assignedUsers.filter(u => otherFilterGroups.every((g, i) => u[g] === otherFilterValues[i]));
}
const unAssignedUsers = fetchedUsers.filter(u => !assignedUserIds.includes(u.id));
if (!otherFilterGroups.length) return unAssignedUsers;
return unAssignedUsers.filter(u => otherFilterGroups.every((g, i) => u[g] === otherFilterValues[i]));
}
return fetchedUsers;
}

render() {
const {
resources,
Expand All @@ -195,7 +240,7 @@ class UserSearchContainer extends React.Component {
resultOffset,
data: {
patronGroups: (resources.patronGroups || {}).records || [],
users: get(resources, 'records.records', []),
users: this.getUsers(),
},
});
}
Expand Down
18 changes: 2 additions & 16 deletions src/UserSearchView.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import {
SearchAndSortSearchButton as FilterPaneToggle,
} from '@folio/stripes/smart-components';

import { UAS } from './constants';
import { getUsersBasedOnAssignmentStatus } from './utils';
import filterConfig, { filterConfigWithUserAssignedStatus } from './filterConfig';
import Filters from './Filters';

Expand Down Expand Up @@ -295,19 +293,6 @@ class UserSearchView extends React.Component {
return true;
};

const getContentData = () => {
const activeFilterState = activeFilters?.state;
const isUasFilterGroupActive = Object.hasOwn(activeFilterState, UAS);
const uasFilterValue = activeFilterState.uas;

if (isUasFilterGroupActive && uasFilterValue.length === 1) {
const filteredUsers = getUsersBasedOnAssignmentStatus(activeFilterState, uasFilterValue, initialSelectedUsers, users);
return filteredUsers;
}
// when both 'Assigned' and 'Unassigned' filters are applied or both are not applied
return users;
};

return (
<IntlConsumer>
{intl => (
Expand Down Expand Up @@ -377,7 +362,8 @@ class UserSearchView extends React.Component {
<MultiColumnList
visibleColumns={builtVisibleColumns}
isSelected={this.isSelected}
contentData={getContentData()}
// contentData={getContentData()}
contentData={users}
totalCount={count}
id="list-plugin-find-user"
columnMapping={{
Expand Down
8 changes: 4 additions & 4 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export const UAS = 'uas';
export const ASSIGNED_FILTER_KEY = 'uas.Assigned';
export const UNASSIGNED_FILTER_KEY = 'uas.Unassigned';
export const ASSIGNED = 'Assigned';
export const UNASSIGNED = 'Unassigned';
export const ACTIVE = 'active';
export const INACTIVE = 'inactive';
// export const ASSIGNED = 'Assigned';
// export const UNASSIGNED = 'Unassigned';
// export const ACTIVE = 'active';
// export const INACTIVE = 'inactive';
55 changes: 5 additions & 50 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import cloneDeep from 'lodash/cloneDeep';
import {
ASSIGNED_FILTER_KEY,
UNASSIGNED_FILTER_KEY,
ASSIGNED,
UNASSIGNED,
UAS,
ACTIVE,
INACTIVE
} from './constants';

// eslint-disable-next-line import/prefer-default-export
Expand All @@ -15,7 +11,7 @@ export const updateResourceData = (rData) => {
const newRData = cloneDeep(rData);
if (filterString === `${UNASSIGNED_FILTER_KEY}` || filterString === `${ASSIGNED_FILTER_KEY},${UNASSIGNED_FILTER_KEY}` || filterString === `${UNASSIGNED_FILTER_KEY},${ASSIGNED_FILTER_KEY}`) {
/*
* When Unassigned filter is selected on 'User assigbment Status' filter group, with no other filter from other groups,
* When Unassigned filter is selected on 'User assignment Status' filter group, with no other filter from other groups,
* fetch all the user records. The filter string is adjusted to include both active and inactive status filters. This will result in (cql.allRecords=1)
*
* The same applies when both Assigned and Unassigned are selected in any sequential order.
Expand All @@ -24,60 +20,19 @@ export const updateResourceData = (rData) => {
newRData.query.filters = alteredfilters;
} else if (filterString.includes(`${UNASSIGNED_FILTER_KEY}`)) {
/*
* When UnAssigned filter is selected incombination with any other filters(in other filter groups),
* filter astring for Unassigned is removed and th erest of the filter string is propagated to makeQueryFunction.
* When UnAssigned filter is selected in combination with any other filters,
* filter a string for Unassigned is removed and the rest of the filter string is propagated to makeQueryFunction.
*/
const alteredfilters = newRData.query.filters.split(',').filter(str => !str.startsWith(`${UAS}`)).join(',');
newRData.query.filters = alteredfilters;
} else if (filterString.includes(`${ASSIGNED_FILTER_KEY}`)) {
/*
* When Assigned filter is selected on 'User assigbment Status' filter group, in any combination of filters in other filter groups,
* When Assigned filter is selected on 'User assignment Status' filter group, in any combination of filters in other filter groups,
* cql formation is not needed.
* hence remove aus filter before propagating it further to makeQueryFunction
* hence remove uas filter before propagating it further to makeQueryFunction
*/
const alteredfilters = '';
newRData.query.filters = alteredfilters;
}
return newRData;
};

const filterUsersList = (filterString, initialSelectedUsers, users, filterCheck) => {
let usersList;
if (filterString === `${ASSIGNED}`) {
const assignedUsers = Object.values(initialSelectedUsers);
if (filterCheck) {
// "Assigned" filter along with one or more other filters from other filter groups are selected
usersList = assignedUsers.filter(u => filterCheck(u));
} else {
// when ONLY "Assigned" filter is selected
usersList = assignedUsers;
}
} else if (filterString === `${UNASSIGNED}`) {
// when ONLY "Unassigned" filter is selected
const assignedUserIds = Object.keys(initialSelectedUsers);
if (filterCheck) {
// "Unassigned" filter along with one or more other filters from other filter groups are selected
usersList = users.filter(u => !assignedUserIds.includes(u.id) && (filterCheck(u)));
} else {
// when ONLY "Unassigned" filter is selected
usersList = users.filter(u => !assignedUserIds.includes(u.id));
}
}
return usersList;
};

// eslint-disable-next-line consistent-return
export const getUsersBasedOnAssignmentStatus = (activeFilterState, uasFilterValue, initialSelectedUsers, users) => {
const condForOneOfTheFilters = (u) => activeFilterState?.active?.includes(u.active ? `${ACTIVE}` : `${INACTIVE}`) || activeFilterState?.pg?.includes(u.patronGroup);
const condForBothTheFilters = (u) => activeFilterState?.active?.includes(u.active ? `${ACTIVE}` : `${INACTIVE}`) && activeFilterState?.pg?.includes(u.patronGroup);

let usersList;
if (Object.keys(activeFilterState).length === 1) {
usersList = filterUsersList(uasFilterValue[0], initialSelectedUsers, users);
} else if (Object.keys(activeFilterState).length === 2) {
usersList = filterUsersList(uasFilterValue[0], initialSelectedUsers, users, condForOneOfTheFilters);
} else {
usersList = filterUsersList(uasFilterValue[0], initialSelectedUsers, users, condForBothTheFilters);
}
return usersList;
};
Loading

0 comments on commit a016d97

Please sign in to comment.