expand commune searches with multiple post codes
This commit is contained in:
parent
c7a2a5025b
commit
7f28c49887
3 changed files with 29 additions and 7 deletions
|
@ -1,9 +1,28 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { QueryClientProvider } from 'react-query';
|
import { QueryClientProvider } from 'react-query';
|
||||||
|
import { matchSorter } from 'match-sorter';
|
||||||
|
|
||||||
import ComboSearch from './ComboSearch';
|
import ComboSearch from './ComboSearch';
|
||||||
import { queryClient } from './shared/queryClient';
|
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) {
|
function ComboCommunesSearch(params) {
|
||||||
return (
|
return (
|
||||||
<QueryClientProvider client={queryClient}>
|
<QueryClientProvider client={queryClient}>
|
||||||
|
@ -16,6 +35,7 @@ function ComboCommunesSearch(params) {
|
||||||
code,
|
code,
|
||||||
`${nom} (${codesPostaux[0]})`
|
`${nom} (${codesPostaux[0]})`
|
||||||
]}
|
]}
|
||||||
|
transformResults={expandResultsWithMultiplePostalCodes}
|
||||||
/>
|
/>
|
||||||
</QueryClientProvider>
|
</QueryClientProvider>
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import React, { useCallback } from 'react';
|
import React from 'react';
|
||||||
import { QueryClientProvider } from 'react-query';
|
import { QueryClientProvider } from 'react-query';
|
||||||
import { matchSorter } from 'match-sorter';
|
import { matchSorter } from 'match-sorter';
|
||||||
|
|
||||||
|
@ -7,14 +7,16 @@ import { queryClient } from './shared/queryClient';
|
||||||
|
|
||||||
const extraTerms = [{ code: '99', nom: 'Etranger' }];
|
const extraTerms = [{ code: '99', nom: 'Etranger' }];
|
||||||
|
|
||||||
function ComboDepartementsSearch(params) {
|
function expandResultsWithForeignDepartement(term, results) {
|
||||||
const transformResults = useCallback((term, results) => [
|
return [
|
||||||
...results,
|
...results,
|
||||||
...matchSorter(extraTerms, term, {
|
...matchSorter(extraTerms, term, {
|
||||||
keys: ['nom', 'code']
|
keys: ['nom', 'code']
|
||||||
})
|
})
|
||||||
]);
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
function ComboDepartementsSearch(params) {
|
||||||
return (
|
return (
|
||||||
<QueryClientProvider client={queryClient}>
|
<QueryClientProvider client={queryClient}>
|
||||||
<ComboSearch
|
<ComboSearch
|
||||||
|
@ -23,7 +25,7 @@ function ComboDepartementsSearch(params) {
|
||||||
scope="departements"
|
scope="departements"
|
||||||
minimumInputLength={1}
|
minimumInputLength={1}
|
||||||
transformResult={({ code, nom }) => [code, `${code} - ${nom}`]}
|
transformResult={({ code, nom }) => [code, `${code} - ${nom}`]}
|
||||||
transformResults={transformResults}
|
transformResults={expandResultsWithForeignDepartement}
|
||||||
/>
|
/>
|
||||||
</QueryClientProvider>
|
</QueryClientProvider>
|
||||||
);
|
);
|
||||||
|
|
|
@ -125,12 +125,12 @@ function ComboSearch({
|
||||||
<ComboboxPopover className="shadow-popup">
|
<ComboboxPopover className="shadow-popup">
|
||||||
{results.length > 0 ? (
|
{results.length > 0 ? (
|
||||||
<ComboboxList>
|
<ComboboxList>
|
||||||
{results.map((result) => {
|
{results.map((result, index) => {
|
||||||
const [key, str] = transformResult(result);
|
const [key, str] = transformResult(result);
|
||||||
resultsMap.current[str] = [key, result];
|
resultsMap.current[str] = [key, result];
|
||||||
return (
|
return (
|
||||||
<ComboboxOption
|
<ComboboxOption
|
||||||
key={key}
|
key={`${key}-${index}`}
|
||||||
value={str}
|
value={str}
|
||||||
data-option-value={str}
|
data-option-value={str}
|
||||||
/>
|
/>
|
||||||
|
|
Loading…
Add table
Reference in a new issue