From e5287a2e641c413e129cbdeabbb9b5fc731a5541 Mon Sep 17 00:00:00 2001 From: Priyanka Date: Wed, 10 Jan 2024 17:56:30 +0530 Subject: [PATCH] UIPFU-77 - refine complexity --- src/utils.js | 98 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 34 deletions(-) diff --git a/src/utils.js b/src/utils.js index e352d8c..423657e 100644 --- a/src/utils.js +++ b/src/utils.js @@ -41,47 +41,77 @@ export const updateResourceData = (rData) => { return newRData; }; +const filterUsersList = (filterString, initialSelectedUsers, users, filterCheck) => { + let usersList; + if (filterString === `${ASSIGNED}`) { + const assignedUsers = Object.values(initialSelectedUsers); + if (filterCheck) { + usersList = assignedUsers.filter(u => filterCheck(u)); + } else { + usersList = assignedUsers; + } + } else if (filterString === `${UNASSIGNED}`) { + const assignedUserIds = Object.keys(initialSelectedUsers); + if (filterCheck) { + usersList = users.filter(u => !assignedUserIds.includes(u.id) && (filterCheck(u))); + } else { + 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); - if (uasFilterValue[0] === `${ASSIGNED}`) { - // when ONLY "Assigned" filter is selected - const assignedUsers = Object.values(initialSelectedUsers); - if (Object.keys(activeFilterState).length === 1) { - return assignedUsers; - } - // several filters are selected - // filter users based on the filter group values in place + // if (uasFilterValue[0] === `${ASSIGNED}`) { + // // when ONLY "Assigned" filter is selected + // const assignedUsers = Object.values(initialSelectedUsers); + // if (Object.keys(activeFilterState).length === 1) { + // return assignedUsers; + // } + // // several filters are selected + // // filter users based on the filter group values in place - // when "Assigned" from "User Assignment Status" filter group along with some other filter in one of the other filter groups - if (Object.keys(activeFilterState).length === 2) { - const filteredAssignedUsers = assignedUsers.filter(u => condForOneOfTheFilters(u)); - return filteredAssignedUsers; - } + // // when "Assigned" from "User Assignment Status" filter group along with some other filter in one of the other filter groups + // if (Object.keys(activeFilterState).length === 2) { + // const filteredAssignedUsers = assignedUsers.filter(u => condForOneOfTheFilters(u)); + // return filteredAssignedUsers; + // } - // when filters from all the filter groups are selected - const filteredAssignedUsers = assignedUsers.filter(u => condForBothTheFilters(u)); - return filteredAssignedUsers; - } - if (uasFilterValue[0] === `${UNASSIGNED}`) { - // when ONLY "Unassigned" filter is selected - const assignedUserIds = Object.keys(initialSelectedUsers); - if (Object.keys(activeFilterState).length === 1) { - const filteredUsers = users.filter(u => !assignedUserIds.includes(u.id)); - return filteredUsers; - } - // several filters are selected - // filter users based on the filter group values in place + // // when filters from all the filter groups are selected + // const filteredAssignedUsers = assignedUsers.filter(u => condForBothTheFilters(u)); + // return filteredAssignedUsers; + // } + // if (uasFilterValue[0] === `${UNASSIGNED}`) { + // // when ONLY "Unassigned" filter is selected + // const assignedUserIds = Object.keys(initialSelectedUsers); + // if (Object.keys(activeFilterState).length === 1) { + // const filteredUsers = users.filter(u => !assignedUserIds.includes(u.id)); + // return filteredUsers; + // } + // // several filters are selected + // // filter users based on the filter group values in place - // when "UnAssigned" from "User Assignment Status" filter group along with some other filter in one of the other filter groups - if (Object.keys(activeFilterState).length === 2) { - const filteredAssignedUsers = users.filter(u => !assignedUserIds.includes(u.id) && (condForOneOfTheFilters(u))); - return filteredAssignedUsers; - } + // // when "UnAssigned" from "User Assignment Status" filter group along with some other filter in one of the other filter groups + // if (Object.keys(activeFilterState).length === 2) { + // const filteredAssignedUsers = users.filter(u => !assignedUserIds.includes(u.id) && (condForOneOfTheFilters(u))); + // return filteredAssignedUsers; + // } + + // // when filters from all the filter groups are selected + // const filteredAssignedUsers = users.filter(u => !assignedUserIds.includes(u.id) && (condForBothTheFilters(u))); + // return filteredAssignedUsers; + // } - // when filters from all the filter groups are selected - const filteredAssignedUsers = users.filter(u => !assignedUserIds.includes(u.id) && (condForBothTheFilters(u))); - return filteredAssignedUsers; + 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; };