Skip to content

Commit 34f665a

Browse files
committed
chore: refactored useFilterServiceClients to smaller helper functions
Signed-off-by: Mac Deluca <Mac.Deluca@quartech.com>
1 parent dc0c875 commit 34f665a

File tree

1 file changed

+44
-20
lines changed

1 file changed

+44
-20
lines changed

app/src/bcsc-theme/features/services/hooks/useFilterServiceClients.ts

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -110,30 +110,54 @@ export const useFilterServiceClients = (filter: ServiceClientsFilter): ClientMet
110110
}
111111

112112
// Sort services by their listing order, then alphabetically by name
113-
return serviceClientsCopy.sort((a, b) => {
114-
const orderA = a.service_listing_sort_order ?? Number.MAX_SAFE_INTEGER
115-
const orderB = b.service_listing_sort_order ?? Number.MAX_SAFE_INTEGER
116-
117-
if (orderA !== orderB) {
118-
return orderA - orderB
119-
}
120-
121-
return a.client_name.localeCompare(b.client_name)
122-
})
113+
return _sortServiceClients(serviceClientsCopy)
123114
}, [serviceClients, filter.cardProcessFilter, filter.requireBCAddressFilter, filter.serviceClientIdsFilter])
124115

125116
// Further filter services based on the partial name filter
126-
const queriedServiceClients = useMemo(() => {
127-
// Return all supported services when there's no search text
128-
if (!filter.partialNameFilter || filter.partialNameFilter.trim() === '') {
129-
return filteredServiceClients
130-
}
117+
const queriedServiceClients = useMemo(
118+
() => _queryServiceClientsByName(filteredServiceClients, filter.partialNameFilter),
119+
[filteredServiceClients, filter]
120+
)
131121

132-
// Filter supported services based on the search text (case insensitive)
133-
const query = filter.partialNameFilter.toLowerCase()
122+
return queriedServiceClients
123+
}
134124

135-
return filteredServiceClients.filter((service) => service.client_name.toLowerCase().includes(query))
136-
}, [filteredServiceClients, filter])
125+
/**
126+
* Filters the given list of service clients by their name using a case-insensitive partial match.
127+
*
128+
* @param {ClientMetadata[]} serviceClients - The list of service clients to filter.
129+
* @param {string} [query] - The partial name to filter by.
130+
* @returns {*} {ClientMetadata[]} The filtered list of service clients whose names match the query.
131+
*/
132+
function _queryServiceClientsByName(serviceClients: ClientMetadata[], query?: string): ClientMetadata[] {
133+
const caseInsensitiveQuery = query?.toLowerCase().trim()
137134

138-
return queriedServiceClients
135+
// Return all supported services when there's no search text
136+
if (!caseInsensitiveQuery) {
137+
return serviceClients
138+
}
139+
140+
return serviceClients.filter((service) => service.client_name.toLowerCase().includes(caseInsensitiveQuery))
141+
}
142+
143+
/**
144+
* Sorts the given list of service clients first by their numeric service listing sort order (ascending),
145+
* and then alphabetically by their name (A-Z) for services with the same sort order.
146+
*
147+
* Services without a defined sort order are placed at the end of the list.
148+
*
149+
* @param {ClientMetadata[]} serviceClients - The list of service clients to sort.
150+
* @returns {*} {ClientMetadata[]} The sorted list of service clients.
151+
*/
152+
function _sortServiceClients(serviceClients: ClientMetadata[]): ClientMetadata[] {
153+
return serviceClients.sort((a, b) => {
154+
const orderA = a.service_listing_sort_order ?? Number.MAX_SAFE_INTEGER
155+
const orderB = b.service_listing_sort_order ?? Number.MAX_SAFE_INTEGER
156+
157+
if (orderA !== orderB) {
158+
return orderA - orderB
159+
}
160+
161+
return a.client_name.localeCompare(b.client_name)
162+
})
139163
}

0 commit comments

Comments
 (0)