implement ComboPaysSearch

This commit is contained in:
Christophe Robillard 2020-11-25 15:17:59 +01:00 committed by LeSim (Rebase PR Action)
parent 0012fd3aa5
commit 12d1a5bece
6 changed files with 884 additions and 7 deletions

View file

@ -0,0 +1,21 @@
import React from 'react';
import { ReactQueryCacheProvider } from 'react-query';
import ComboSearch from './ComboSearch';
import { queryCache } from './shared/queryCache';
function ComboPaysSearch(params) {
return (
<ReactQueryCacheProvider queryCache={queryCache}>
<ComboSearch
required={params.mandatory}
hiddenFieldId={params.hiddenFieldId}
scope="pays"
minimumInputLength={0}
transformResult={({ nom }) => [nom, nom]}
/>
</ReactQueryCacheProvider>
);
}
export default ComboPaysSearch;

View file

@ -1,5 +1,6 @@
import { QueryCache } from 'react-query';
import { isNumeric } from '@utils';
import matchSorter from 'match-sorter';
const { api_geo_url, api_adresse_url } = gon.autocomplete || {};
@ -31,6 +32,11 @@ function buildOptions() {
}
async function defaultQueryFn(scope, term) {
if (scope == 'pays') {
const pays = await fetch('/pays.json').then((response) => response.json());
return matchSorter(pays, term, { keys: ['nom'] });
}
const url = buildURL(scope, term);
const [options, controller] = buildOptions();
const promise = fetch(url, options).then((response) => response.json());