Skip to content

Commit

Permalink
Sort right holders list (#997)
Browse files Browse the repository at this point in the history
* Sort right holders list

* Sort users by name, ignore roles for now

* fix pageEntries filtering issue

* fix
  • Loading branch information
sonwit authored Aug 7, 2024
1 parent 654c872 commit b5892a1
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/features/amUI/users/useFilteredRightHolders.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getTotalNumOfPages, getArrayPage } from '@/resources/utils';
import type { RightHolder } from '@/rtk/features/userInfo/userInfoApi';
import { useGetRightHoldersQuery } from '@/rtk/features/userInfo/userInfoApi';
import { useState, useEffect } from 'react';
import { useState, useEffect, useMemo } from 'react';

const isSearchMatch = (searchString: string, rightHolder: RightHolder): boolean => {
const isNameMatch = rightHolder.name.toLowerCase().indexOf(searchString.toLowerCase()) > -1;
Expand All @@ -10,6 +10,20 @@ const isSearchMatch = (searchString: string, rightHolder: RightHolder): boolean
return isNameMatch || isPersonIdMatch || isOrgNrMatch;
};

const sortRightHolders = (rightHolders: RightHolder[]): RightHolder[] =>
[...rightHolders].sort((a, b) => a.name.localeCompare(b.name));

export const sortRightholderList = (list: RightHolder[]): RightHolder[] =>
sortRightHolders(list).map((rightHolder) => {
if (rightHolder.inheritingRightHolders) {
return {
...rightHolder,
inheritingRightHolders: sortRightHolders(rightHolder.inheritingRightHolders),
};
}
return rightHolder;
});

const computePageEntries = (
searchString: string,
currentPage: number,
Expand Down Expand Up @@ -56,16 +70,20 @@ export const useFilteredRightHolders = (
pageSize: number,
) => {
const { data: rightHolders } = useGetRightHoldersQuery();

const [pageEntries, setPageEntries] = useState<RightHolder[]>([]);
const [numOfPages, setNumOfPages] = useState<number>(1);
const [searchResultLength, setSearchResultLength] = useState<number>(0);

// Sorting the list of rightHolders only when the list changes
const sortedRightHolders = useMemo(() => sortRightholderList(rightHolders || []), [rightHolders]);

useEffect(() => {
const { pageEntries, numOfPages, searchResultLength } = computePageEntries(
searchString,
currentPage,
pageSize,
rightHolders,
sortedRightHolders,
);
setPageEntries(pageEntries);
setNumOfPages(numOfPages);
Expand Down

0 comments on commit b5892a1

Please sign in to comment.