Merge pull request #6349 from tchak/fix-communes-autocomplete
Get more results from communes API and use local matcher
This commit is contained in:
commit
b7c0a42fd5
2 changed files with 25 additions and 13 deletions
|
@ -3,7 +3,12 @@ import { QueryClientProvider } from 'react-query';
|
||||||
import { matchSorter } from 'match-sorter';
|
import { matchSorter } from 'match-sorter';
|
||||||
|
|
||||||
import ComboSearch from './ComboSearch';
|
import ComboSearch from './ComboSearch';
|
||||||
import { queryClient, searchResultsLimit } from './shared/queryClient';
|
import { queryClient } from './shared/queryClient';
|
||||||
|
|
||||||
|
// Avoid hiding similar matches for precise queries (like "Sainte Marie")
|
||||||
|
function searchResultsLimit(term) {
|
||||||
|
return term.length > 5 ? 10 : 5;
|
||||||
|
}
|
||||||
|
|
||||||
function expandResultsWithMultiplePostalCodes(term, results) {
|
function expandResultsWithMultiplePostalCodes(term, results) {
|
||||||
// A single result may have several associated postal codes.
|
// A single result may have several associated postal codes.
|
||||||
|
|
|
@ -2,6 +2,19 @@ import { QueryClient } from 'react-query';
|
||||||
import { isNumeric } from '@utils';
|
import { isNumeric } from '@utils';
|
||||||
import { matchSorter } from 'match-sorter';
|
import { matchSorter } from 'match-sorter';
|
||||||
|
|
||||||
|
const API_EDUCATION_QUERY_LIMIT = 5;
|
||||||
|
const API_GEO_QUERY_LIMIT = 5;
|
||||||
|
const API_ADRESSE_QUERY_LIMIT = 5;
|
||||||
|
|
||||||
|
// When searching for short strings like "mer", le exact match shows up quite far in
|
||||||
|
// the ordering (~50).
|
||||||
|
//
|
||||||
|
// That's why we deliberately fetch a lot of results, and then let the local matching
|
||||||
|
// (match-sorter) do the work.
|
||||||
|
//
|
||||||
|
// NB: 60 is arbitrary, we may add more if needed.
|
||||||
|
const API_GEO_COMMUNES_QUERY_LIMIT = 60;
|
||||||
|
|
||||||
const { api_geo_url, api_adresse_url, api_education_url } =
|
const { api_geo_url, api_adresse_url, api_education_url } =
|
||||||
gon.autocomplete || {};
|
gon.autocomplete || {};
|
||||||
|
|
||||||
|
@ -13,28 +26,22 @@ export const queryClient = new QueryClient({
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Avoid hiding similar matches for precise queries (like "Sainte Marie")
|
|
||||||
export function searchResultsLimit(term) {
|
|
||||||
return term.length > 5 ? 10 : 5;
|
|
||||||
}
|
|
||||||
|
|
||||||
function buildURL(scope, term) {
|
function buildURL(scope, term) {
|
||||||
term = encodeURIComponent(term.replace(/\(|\)/g, ''));
|
term = encodeURIComponent(term.replace(/\(|\)/g, ''));
|
||||||
if (scope === 'adresse') {
|
if (scope === 'adresse') {
|
||||||
return `${api_adresse_url}/search?q=${term}&limit=5`;
|
return `${api_adresse_url}/search?q=${term}&limit=${API_ADRESSE_QUERY_LIMIT}`;
|
||||||
} else if (scope === 'annuaire-education') {
|
} else if (scope === 'annuaire-education') {
|
||||||
return `${api_education_url}/search?dataset=fr-en-annuaire-education&q=${term}&rows=5`;
|
return `${api_education_url}/search?dataset=fr-en-annuaire-education&q=${term}&rows=${API_EDUCATION_QUERY_LIMIT}`;
|
||||||
} else if (scope === 'communes') {
|
} else if (scope === 'communes') {
|
||||||
if (isNumeric(term)) {
|
if (isNumeric(term)) {
|
||||||
return `${api_geo_url}/communes?codePostal=${term}&limit=5`;
|
return `${api_geo_url}/communes?codePostal=${term}&limit=${API_GEO_COMMUNES_QUERY_LIMIT}`;
|
||||||
}
|
}
|
||||||
const limit = searchResultsLimit(term);
|
return `${api_geo_url}/communes?nom=${term}&boost=population&limit=${API_GEO_COMMUNES_QUERY_LIMIT}`;
|
||||||
return `${api_geo_url}/communes?nom=${term}&boost=population&limit=${limit}`;
|
|
||||||
} else if (isNumeric(term)) {
|
} else if (isNumeric(term)) {
|
||||||
const code = term.padStart(2, '0');
|
const code = term.padStart(2, '0');
|
||||||
return `${api_geo_url}/${scope}?code=${code}&limit=5`;
|
return `${api_geo_url}/${scope}?code=${code}&limit=${API_GEO_QUERY_LIMIT}`;
|
||||||
}
|
}
|
||||||
return `${api_geo_url}/${scope}?nom=${term}&limit=5`;
|
return `${api_geo_url}/${scope}?nom=${term}&limit=${API_GEO_QUERY_LIMIT}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildOptions() {
|
function buildOptions() {
|
||||||
|
|
Loading…
Reference in a new issue