@@ -37,14 +37,15 @@ import {
3737 FormHelperText ,
3838 Divider
3939} from '@mui/material' ;
40+ import AddIcon from '@mui/icons-material/Add' ;
41+ import EditIcon from '@mui/icons-material/Edit' ;
4042import PersonAddIcon from '@mui/icons-material/PersonAdd' ;
4143import SearchIcon from '@mui/icons-material/Search' ;
42- import AddIcon from '@mui/icons-material/Add' ;
4344
44- import { isArrayPresent } from './../../../helpers/checks' ;
45+ import { isArrayPresent } from '../../../helpers/checks' ;
46+ import { useQueryParameters } from '../../../helpers/query-parameters' ;
4547
4648import './Roles.css' ;
47- import EditIcon from '@mui/icons-material/Edit' ;
4849
4950const Roles = ( ) => {
5051 const { state, dispatch } = useContext ( AppContext ) ;
@@ -62,29 +63,27 @@ const Roles = () => {
6263
6364 memberProfiles ?. sort ( ( a , b ) => a . name . localeCompare ( b . name ) ) ;
6465
65- useEffect ( ( ) => {
66- const url = new URL ( location . href ) ;
67- const selectedRoles = url . searchParams . get ( 'roles' ) ;
68- if ( selectedRoles ?. length > 0 ) {
69- // Select only the roles specified in the URL.
70- setSelectedRoles ( selectedRoles . split ( ',' ) ) ;
71- } else {
72- // Select all possible roles.
73- setSelectedRoles ( roles . map ( r => r . role ) ) ;
66+ if ( ! roles ) console . error ( 'Roles.jsx: state.roles is not set!' ) ;
67+ const allRoles = roles . map ( r => r . role ) . sort ( ) ;
68+ useQueryParameters ( [
69+ {
70+ name : 'roles' ,
71+ default : allRoles ,
72+ value : selectedRoles ,
73+ setter ( value ) {
74+ setSelectedRoles ( isArrayPresent ( value ) ? value . sort ( ) : allRoles ) ;
75+ } ,
76+ toQP ( ) {
77+ return selectedRoles . join ( ',' ) ;
78+ }
79+ } ,
80+ {
81+ name : 'search' ,
82+ default : '' ,
83+ value : searchText ,
84+ setter : setSearchText
7485 }
75- setSearchText ( url . searchParams . get ( 'search' ) ?? '' ) ;
76- } , [ ] ) ;
77-
78- useEffect ( ( ) => {
79- const url = new URL ( location . href ) ;
80- const params = {
81- roles : selectedRoles . join ( ',' ) ,
82- search : searchText
83- } ;
84- const q = new URLSearchParams ( params ) . toString ( ) ;
85- const newUrl = url . origin + url . pathname + '?' + q ;
86- history . replaceState ( params , '' , newUrl ) ;
87- } , [ searchText , selectedRoles ] ) ;
86+ ] ) ;
8887
8988 useEffect ( ( ) => {
9089 const memberMap = { } ;
@@ -227,9 +226,7 @@ const Roles = () => {
227226 value = { selectedRoles }
228227 onChange = { event => {
229228 const value = event . target . value ;
230- setSelectedRoles (
231- typeof value === 'string' ? value . split ( ',' ) : value
232- ) ;
229+ setSelectedRoles ( value . sort ( ) ) ;
233230 } }
234231 input = { < OutlinedInput label = "Roles" /> }
235232 renderValue = { selected => selected . join ( ', ' ) }
0 commit comments