@@ -37,8 +37,10 @@ const Contacts = () => {
3737 const [ processing , setProcessing ] = useState ( false ) ;
3838 const [ selectedContactToDelete , setSelectedContactToDelete ] = useState ( null ) ;
3939 const [ deleteViaEdit , setDeleteViaEdit ] = useState ( false ) ;
40+ const [ sortValue , setSortValue ] = useState ( 'Sort by:' ) ;
41+ const [ sortedData , setSortedData ] = useState ( [ ] ) ;
4042 const {
41- data,
43+ data = [ ] ,
4244 isLoading,
4345 isError,
4446 error,
@@ -47,12 +49,42 @@ const Contacts = () => {
4749 delete : deleteContact
4850 } = useContactsList ( ) ;
4951 const { addNotification } = useNotification ( ) ;
50- const [ fieldType , setFieldType ] = useState ( 'First Name' ) ;
52+
53+ const sortData = ( value ) => {
54+ const sorted = [ ...data ] . sort ( ( a , b ) => {
55+ if ( value === 'Default' ) {
56+ return data ;
57+ }
58+ if ( value === 'First Name' ) {
59+ return a . givenName . localeCompare ( b . givenName ) ;
60+ }
61+ if ( value === 'Last Name' ) {
62+ return a . familyName . localeCompare ( b . familyName ) ;
63+ }
64+ if ( value === 'Web ID' ) {
65+ return a . webId . localeCompare ( b . webId ) ;
66+ }
67+ return 0 ;
68+ } ) ;
69+ setSortedData ( sorted ) ;
70+ } ;
71+
72+ const handleSortChange = ( event ) => {
73+ const { value } = event . target ;
74+ setSortValue ( value ) ;
75+ sortData ( value ) ;
76+ } ;
5177
5278 useEffect ( ( ) => {
5379 refetch ( ) ;
5480 } , [ ] ) ;
5581
82+ useEffect ( ( ) => {
83+ if ( data . length > 0 ) {
84+ setSortedData ( data ) ;
85+ }
86+ } , [ data ] ) ;
87+
5688 const getContactDisplayName = ( contact ) => {
5789 if ( ! contact ) {
5890 return 'Unknown Contact' ;
@@ -104,14 +136,13 @@ const Contacts = () => {
104136 >
105137 < Box >
106138 < Box sx = { { display : 'flex' , justifyContent : 'space-between' } } >
107- { /* TODO: Make sorting options functional */ }
108139 { isSmallScreen && (
109140 < FormControl sx = { { minWidth : 120 } } size = "small" >
110141 < Select
111142 id = "contact-select-field-small"
112- value = { fieldType }
113143 defaultValue = "First Name"
114- onChange = { ( e ) => setFieldType ( e . target . value ) }
144+ value = { sortValue }
145+ onChange = { handleSortChange }
115146 sx = { {
116147 borderRadius : '8px' ,
117148 color : 'primary.main' ,
@@ -127,6 +158,11 @@ const Contacts = () => {
127158 } }
128159 IconComponent = { KeyboardArrowDownIcon }
129160 >
161+ { ' ' }
162+ < MenuItem value = "Sort by:" disabled >
163+ Sort by:
164+ </ MenuItem >
165+ < MenuItem value = "Default" > Default</ MenuItem >
130166 < MenuItem value = "First Name" > First Name</ MenuItem >
131167 < MenuItem value = "Last Name" > Last Name</ MenuItem >
132168 < MenuItem value = "Web ID" > Web ID</ MenuItem >
@@ -145,7 +181,7 @@ const Contacts = () => {
145181 </ Box >
146182 { data . length > 0 ? (
147183 < ContactListTable
148- contacts = { data }
184+ contacts = { isSmallScreen ? sortedData : data }
149185 deleteContact = { ( contact ) => handleSelectDeleteContact ( contact ) }
150186 handleDeleteContact = { handleDeleteContact }
151187 addContact = { addContact }
0 commit comments