-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add first version of user search * refactor search * rename "entrees" to "entries" * Add live region * Update user search placeholder text and move filter logick to hook * Move user search feedback text * Add hideLabel prop to Search component and update user search placeholder text * Refactor List component to use TypeScript interface for props * chore: Update UsersList component to useFilteredRightHolders hook and fix search functionality * Refactor user search functionality and update nn text * fix typo --------- Co-authored-by: Sondre Wittek <sondrewittek@gmail.com>
- Loading branch information
Showing
9 changed files
with
162 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,7 @@ | |
justify-content: center; | ||
padding-top: 1rem; | ||
} | ||
|
||
.searchBar { | ||
max-width: 27rem; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
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'; | ||
|
||
const isSearchMatch = (searchString: string, rightHolder: RightHolder): boolean => { | ||
const isNameMatch = rightHolder.name.toLowerCase().indexOf(searchString.toLowerCase()) > -1; | ||
const isPersonIdMatch = rightHolder.personId === searchString; | ||
const isOrgNrMatch = rightHolder.organizationNumber === searchString; | ||
return isNameMatch || isPersonIdMatch || isOrgNrMatch; | ||
}; | ||
|
||
const computePageEntries = ( | ||
searchString: string, | ||
currentPage: number, | ||
pageSize: number, | ||
rightHolders?: RightHolder[], | ||
) => { | ||
if (!rightHolders) { | ||
return { | ||
pageEntries: [], | ||
numOfPages: 1, | ||
searchResultLength: 0, | ||
}; | ||
} | ||
|
||
const searchResult: RightHolder[] = []; | ||
|
||
rightHolders.forEach((rightHolder) => { | ||
if (isSearchMatch(searchString, rightHolder)) { | ||
searchResult.push(rightHolder); | ||
} else if (rightHolder.inheritingRightHolders?.length > 0) { | ||
// check for searchString matches in inheritingRightHolders | ||
const matchingInheritingItems = rightHolder.inheritingRightHolders.filter( | ||
(inheritRightHolder) => isSearchMatch(searchString, inheritRightHolder), | ||
); | ||
if (matchingInheritingItems.length > 0) { | ||
searchResult.push({ | ||
...rightHolder, | ||
inheritingRightHolders: matchingInheritingItems, | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
return { | ||
pageEntries: getArrayPage(searchResult, currentPage, pageSize), | ||
numOfPages: getTotalNumOfPages(searchResult, pageSize), | ||
searchResultLength: searchResult.length, | ||
}; | ||
}; | ||
|
||
export const useFilteredRightHolders = ( | ||
searchString: string, | ||
currentPage: number, | ||
pageSize: number, | ||
) => { | ||
const { data: rightHolders } = useGetRightHoldersQuery(); | ||
const [pageEntries, setPageEntries] = useState<RightHolder[]>([]); | ||
const [numOfPages, setNumOfPages] = useState<number>(1); | ||
const [searchResultLength, setSearchResultLength] = useState<number>(0); | ||
|
||
useEffect(() => { | ||
const { pageEntries, numOfPages, searchResultLength } = computePageEntries( | ||
searchString, | ||
currentPage, | ||
pageSize, | ||
rightHolders, | ||
); | ||
setPageEntries(pageEntries); | ||
setNumOfPages(numOfPages); | ||
setSearchResultLength(searchResultLength); | ||
}, [searchString, currentPage, rightHolders]); | ||
|
||
return { pageEntries, numOfPages, searchResultLength }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters