expand commune searches with multiple post codes

This commit is contained in:
Paul Chavard 2021-06-15 14:36:37 +02:00
parent c7a2a5025b
commit 7f28c49887
3 changed files with 29 additions and 7 deletions

View file

@ -1,9 +1,28 @@
import React from 'react';
import { QueryClientProvider } from 'react-query';
import { matchSorter } from 'match-sorter';
import ComboSearch from './ComboSearch';
import { queryClient } from './shared/queryClient';
function expandResultsWithMultiplePostalCodes(term, results) {
const expandedResults = results.flatMap((result) =>
result.codesPostaux.map((codePostal) => ({
...result,
codesPostaux: [codePostal]
}))
);
const limit = term.length > 5 ? 10 : 5;
if (expandedResults.length > limit) {
return matchSorter(expandedResults, term, {
keys: [(item) => `${item.nom} (${item.codesPostaux[0]})`, 'code'],
sorter: (rankedItems) => rankedItems
}).slice(0, limit + 1);
}
return expandedResults;
}
function ComboCommunesSearch(params) {
return (
<QueryClientProvider client={queryClient}>
@ -16,6 +35,7 @@ function ComboCommunesSearch(params) {
code,
`${nom} (${codesPostaux[0]})`
]}
transformResults={expandResultsWithMultiplePostalCodes}
/>
</QueryClientProvider>
);