Skip to content

Commit da991eb

Browse files
committed
Simplifying sorting function
1 parent 2a8aa54 commit da991eb

File tree

3 files changed

+54
-19
lines changed

3 files changed

+54
-19
lines changed

src/components/Contacts/ContactListTable.jsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ import ContactListTableMobile from './ContactListTableMobile';
2626
* @param {Function} Props.addContact - from Contacts page
2727
* @returns {React.JSX.Element} The ContactListTable Component
2828
*/
29-
const ContactListTable = ({ contacts, deleteContact, handleDeleteContact, addContact }) => {
29+
const ContactListTable = ({ contacts = [], deleteContact, handleDeleteContact, addContact }) => {
3030
const [showMessageModal, setShowMessageModal] = useState(false);
3131
const [messageToField, setMessageToField] = useState('');
32-
const theme = useTheme();
33-
const isSmallScreen = useMediaQuery(theme.breakpoints.down('sm'));
34-
3532
const [showAddContactModal, setShowAddContactModal] = useState(false);
3633
const [contactToEdit, setContactToEdit] = useState(null);
3734
const [isEditing, setIsEditing] = useState(false);
35+
const contactWebIds = contacts.map(({ webId }) => webId);
36+
const theme = useTheme();
37+
const isSmallScreen = useMediaQuery(theme.breakpoints.down('sm'));
3838

3939
const handleSendMessage = (contactId) => {
4040
setShowMessageModal(!showMessageModal);
@@ -47,14 +47,12 @@ const ContactListTable = ({ contacts, deleteContact, handleDeleteContact, addCon
4747
setIsEditing(true);
4848
};
4949

50-
const contactWebIds = contacts.map(({ webId }) => webId);
51-
5250
return (
5351
<Box
5452
sx={{
5553
margin: '20px 0',
5654
width: '95vw',
57-
height: '500px'
55+
minHeight: '500px'
5856
}}
5957
>
6058
{isSmallScreen ? (

src/hooks/useTableSort.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
// hooks/useSortedData.js
21
import { useState, useEffect } from 'react';
32

4-
const useSortedData = (initialData, getFieldValue, initialFieldType) => {
3+
const useSortedData = (getFieldValue, initialFieldType, initialData = []) => {
54
const [fieldType, setFieldType] = useState(initialFieldType);
65
const [sortedData, setSortedData] = useState([]);
76

87
useEffect(() => {
9-
const sortData = (dataToSort, field) =>
10-
[...dataToSort].sort((a, b) => {
11-
const fieldA = getFieldValue(a, field);
12-
const fieldB = getFieldValue(b, field);
8+
const sortData = (field, dataToSort = []) => {
9+
if (!Array.isArray(dataToSort)) return [];
10+
return [...dataToSort].sort((a, b) => {
11+
const fieldA = getFieldValue(a, field) || '';
12+
const fieldB = getFieldValue(b, field) || '';
1313
return fieldA.localeCompare(fieldB);
1414
});
15+
};
1516

1617
setSortedData(sortData(initialData, fieldType));
1718
}, [fieldType, initialData, getFieldValue]);

src/pages/Contacts.jsx

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)