From 12d1a5bece799b8ee4aeef850409d634508cabec Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Wed, 25 Nov 2020 15:17:59 +0100 Subject: [PATCH 1/3] implement ComboPaysSearch --- app/javascript/components/ComboPaysSearch.js | 21 + .../components/shared/queryCache.js | 6 + app/javascript/loaders/ComboPaysSearch.js | 3 + .../dossiers/editable_champs/_pays.html.haml | 8 +- public/pays.json | 848 ++++++++++++++++++ spec/features/users/brouillon_spec.rb | 5 +- 6 files changed, 884 insertions(+), 7 deletions(-) create mode 100644 app/javascript/components/ComboPaysSearch.js create mode 100644 app/javascript/loaders/ComboPaysSearch.js create mode 100644 public/pays.json diff --git a/app/javascript/components/ComboPaysSearch.js b/app/javascript/components/ComboPaysSearch.js new file mode 100644 index 000000000..826b352c9 --- /dev/null +++ b/app/javascript/components/ComboPaysSearch.js @@ -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 ( + + [nom, nom]} + /> + + ); +} + +export default ComboPaysSearch; diff --git a/app/javascript/components/shared/queryCache.js b/app/javascript/components/shared/queryCache.js index 501b6e45a..6809c0473 100644 --- a/app/javascript/components/shared/queryCache.js +++ b/app/javascript/components/shared/queryCache.js @@ -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()); diff --git a/app/javascript/loaders/ComboPaysSearch.js b/app/javascript/loaders/ComboPaysSearch.js new file mode 100644 index 000000000..e4e6ed33d --- /dev/null +++ b/app/javascript/loaders/ComboPaysSearch.js @@ -0,0 +1,3 @@ +import Loadable from '../components/Loadable'; + +export default Loadable(() => import('../components/ComboPaysSearch')); diff --git a/app/views/shared/dossiers/editable_champs/_pays.html.haml b/app/views/shared/dossiers/editable_champs/_pays.html.haml index 0e686fee9..5a9ca2523 100644 --- a/app/views/shared/dossiers/editable_champs/_pays.html.haml +++ b/app/views/shared/dossiers/editable_champs/_pays.html.haml @@ -1,5 +1,3 @@ -= form.select :value, - Champs::PaysChamp.pays, - { disabled: Champs::PaysChamp.disabled_options, include_blank: true }, - required: champ.mandatory?, - class: 'select2 pays' +- hidden_field_id = SecureRandom.uuid += form.hidden_field :value, { data: { uuid: hidden_field_id } } += react_component("ComboPaysSearch", mandatory: champ.mandatory?, hiddenFieldId: hidden_field_id) diff --git a/public/pays.json b/public/pays.json new file mode 100644 index 000000000..a56e16923 --- /dev/null +++ b/public/pays.json @@ -0,0 +1,848 @@ +[ + { + "nom": "FRANCE" + }, + { + "nom": "ACORES, MADERE" + }, + { + "nom": "AFGHANISTAN" + }, + { + "nom": "AFRIQUE DU SUD" + }, + { + "nom": "ALASKA" + }, + { + "nom": "ALBANIE" + }, + { + "nom": "ALGERIE" + }, + { + "nom": "ALLEMAGNE" + }, + { + "nom": "ANDORRE" + }, + { + "nom": "ANGOLA" + }, + { + "nom": "ANGUILLA" + }, + { + "nom": "ANTIGUA-ET-BARBUDA" + }, + { + "nom": "ANTILLES NEERLANDAISES" + }, + { + "nom": "ARABIE SAOUDITE" + }, + { + "nom": "ARGENTINE" + }, + { + "nom": "ARMENIE" + }, + { + "nom": "ARUBA" + }, + { + "nom": "AUSTRALIE" + }, + { + "nom": "AUTRICHE" + }, + { + "nom": "AZERBAIDJAN" + }, + { + "nom": "BAHAMAS" + }, + { + "nom": "BAHREIN" + }, + { + "nom": "BANGLADESH" + }, + { + "nom": "BARBADE" + }, + { + "nom": "BELGIQUE" + }, + { + "nom": "BELIZE" + }, + { + "nom": "BENIN" + }, + { + "nom": "BERMUDES" + }, + { + "nom": "BHOUTAN" + }, + { + "nom": "BIELORUSSIE" + }, + { + "nom": "BIRMANIE" + }, + { + "nom": "BOLIVIE" + }, + { + "nom": "BONAIRE, SAINT EUSTACHE ET SABA" + }, + { + "nom": "BOSNIE-HERZEGOVINE" + }, + { + "nom": "BOTSWANA" + }, + { + "nom": "BOUVET (ILE)" + }, + { + "nom": "BRESIL" + }, + { + "nom": "BRUNEI" + }, + { + "nom": "BULGARIE" + }, + { + "nom": "BURKINA" + }, + { + "nom": "BURUNDI" + }, + { + "nom": "CAIMANES (ILES)" + }, + { + "nom": "CAMBODGE" + }, + { + "nom": "CAMEROUN" + }, + { + "nom": "CAMEROUN ET TOGO" + }, + { + "nom": "CANADA" + }, + { + "nom": "CANARIES (ILES)" + }, + { + "nom": "CAP-VERT" + }, + { + "nom": "CENTRAFRICAINE (REPUBLIQUE)" + }, + { + "nom": "CHILI" + }, + { + "nom": "CHINE" + }, + { + "nom": "CHRISTMAS (ILE)" + }, + { + "nom": "CHYPRE" + }, + { + "nom": "CLIPPERTON (ILE)" + }, + { + "nom": "COCOS ou KEELING (ILES)" + }, + { + "nom": "COLOMBIE" + }, + { + "nom": "COMORES" + }, + { + "nom": "CONGO" + }, + { + "nom": "CONGO (REPUBLIQUE DEMOCRATIQUE)" + }, + { + "nom": "COOK (ILES)" + }, + { + "nom": "COREE" + }, + { + "nom": "COREE (REPUBLIQUE DE)" + }, + { + "nom": "COREE (REPUBLIQUE POPULAIRE DEMOCRATIQUE DE)" + }, + { + "nom": "COSTA RICA" + }, + { + "nom": "COTE D'IVOIRE" + }, + { + "nom": "CROATIE" + }, + { + "nom": "CUBA" + }, + { + "nom": "CURAÇAO" + }, + { + "nom": "DANEMARK" + }, + { + "nom": "DJIBOUTI" + }, + { + "nom": "DOMINICAINE (REPUBLIQUE)" + }, + { + "nom": "DOMINIQUE" + }, + { + "nom": "EGYPTE" + }, + { + "nom": "EL SALVADOR" + }, + { + "nom": "EMIRATS ARABES UNIS" + }, + { + "nom": "EQUATEUR" + }, + { + "nom": "ERYTHREE" + }, + { + "nom": "ESPAGNE" + }, + { + "nom": "ESTONIE" + }, + { + "nom": "ETATS MALAIS NON FEDERES" + }, + { + "nom": "ETATS-UNIS" + }, + { + "nom": "ETHIOPIE" + }, + { + "nom": "FEROE (ILES)" + }, + { + "nom": "FIDJI" + }, + { + "nom": "FINLANDE" + }, + { + "nom": "GABON" + }, + { + "nom": "GAMBIE" + }, + { + "nom": "GEORGIE" + }, + { + "nom": "GEORGIE DU SUD ET LES ILES SANDWICH DU SUD" + }, + { + "nom": "GHANA" + }, + { + "nom": "GIBRALTAR" + }, + { + "nom": "GOA" + }, + { + "nom": "GRECE" + }, + { + "nom": "GRENADE" + }, + { + "nom": "GROENLAND" + }, + { + "nom": "GUADELOUPE" + }, + { + "nom": "GUAM" + }, + { + "nom": "GUATEMALA" + }, + { + "nom": "GUERNESEY" + }, + { + "nom": "GUINEE" + }, + { + "nom": "GUINEE EQUATORIALE" + }, + { + "nom": "GUINEE-BISSAU" + }, + { + "nom": "GUYANA" + }, + { + "nom": "GUYANE" + }, + { + "nom": "HAITI" + }, + { + "nom": "HAWAII (ILES)" + }, + { + "nom": "HEARD ET MACDONALD (ILES)" + }, + { + "nom": "HONDURAS" + }, + { + "nom": "HONG-KONG" + }, + { + "nom": "HONGRIE" + }, + { + "nom": "ILES PORTUGAISES DE L'OCEAN INDIEN" + }, + { + "nom": "INDE" + }, + { + "nom": "INDONESIE" + }, + { + "nom": "IRAN" + }, + { + "nom": "IRAQ" + }, + { + "nom": "IRLANDE, ou EIRE" + }, + { + "nom": "ISLANDE" + }, + { + "nom": "ISRAEL" + }, + { + "nom": "ITALIE" + }, + { + "nom": "JAMAIQUE" + }, + { + "nom": "JAPON" + }, + { + "nom": "JERSEY" + }, + { + "nom": "JORDANIE" + }, + { + "nom": "KAMTCHATKA" + }, + { + "nom": "KAZAKHSTAN" + }, + { + "nom": "KENYA" + }, + { + "nom": "KIRGHIZISTAN" + }, + { + "nom": "KIRIBATI" + }, + { + "nom": "KOSOVO" + }, + { + "nom": "KOWEIT" + }, + { + "nom": "LA REUNION" + }, + { + "nom": "LABRADOR" + }, + { + "nom": "LAOS" + }, + { + "nom": "LESOTHO" + }, + { + "nom": "LETTONIE" + }, + { + "nom": "LIBAN" + }, + { + "nom": "LIBERIA" + }, + { + "nom": "LIBYE" + }, + { + "nom": "LIECHTENSTEIN" + }, + { + "nom": "LITUANIE" + }, + { + "nom": "LUXEMBOURG" + }, + { + "nom": "MACAO" + }, + { + "nom": "MACEDOINE DU NORD (REPUBLIQUE DE)" + }, + { + "nom": "MADAGASCAR" + }, + { + "nom": "MALAISIE" + }, + { + "nom": "MALAWI" + }, + { + "nom": "MALDIVES" + }, + { + "nom": "MALI" + }, + { + "nom": "MALOUINES, OU FALKLAND (ILES)" + }, + { + "nom": "MALTE" + }, + { + "nom": "MAN (ILE)" + }, + { + "nom": "MANDCHOURIE" + }, + { + "nom": "MARIANNES DU NORD (ILES)" + }, + { + "nom": "MAROC" + }, + { + "nom": "MARSHALL (ILES)" + }, + { + "nom": "MARTINIQUE" + }, + { + "nom": "MAURICE" + }, + { + "nom": "MAURITANIE" + }, + { + "nom": "MAYOTTE" + }, + { + "nom": "MEXIQUE" + }, + { + "nom": "MICRONESIE (ETATS FEDERES DE)" + }, + { + "nom": "MOLDAVIE" + }, + { + "nom": "MONACO" + }, + { + "nom": "MONGOLIE" + }, + { + "nom": "MONTENEGRO" + }, + { + "nom": "MONTSERRAT" + }, + { + "nom": "MOZAMBIQUE" + }, + { + "nom": "NAMIBIE" + }, + { + "nom": "NAURU" + }, + { + "nom": "NEPAL" + }, + { + "nom": "NICARAGUA" + }, + { + "nom": "NIGER" + }, + { + "nom": "NIGERIA" + }, + { + "nom": "NIUE" + }, + { + "nom": "NORFOLK (ILE)" + }, + { + "nom": "NORVEGE" + }, + { + "nom": "NOUVELLE-CALEDONIE" + }, + { + "nom": "NOUVELLE-ZELANDE" + }, + { + "nom": "OCEAN INDIEN (TERRITOIRE BRITANNIQUE DE L')" + }, + { + "nom": "OMAN" + }, + { + "nom": "OUGANDA" + }, + { + "nom": "OUZBEKISTAN" + }, + { + "nom": "PAKISTAN" + }, + { + "nom": "PALAOS (ILES)" + }, + { + "nom": "PALESTINE (Etat de)" + }, + { + "nom": "PANAMA" + }, + { + "nom": "PAPOUASIE-NOUVELLE-GUINEE" + }, + { + "nom": "PARAGUAY" + }, + { + "nom": "PAYS-BAS" + }, + { + "nom": "PEROU" + }, + { + "nom": "PHILIPPINES" + }, + { + "nom": "PITCAIRN (ILE)" + }, + { + "nom": "POLOGNE" + }, + { + "nom": "POLYNESIE FRANCAISE" + }, + { + "nom": "PORTO RICO" + }, + { + "nom": "PORTUGAL" + }, + { + "nom": "POSSESSIONS BRITANNIQUES AU PROCHE-ORIENT" + }, + { + "nom": "PRESIDES" + }, + { + "nom": "PROVINCES ESPAGNOLES D'AFRIQUE" + }, + { + "nom": "QATAR" + }, + { + "nom": "REPUBLIQUE DEMOCRATIQUE ALLEMANDE" + }, + { + "nom": "REPUBLIQUE FEDERALE D'ALLEMAGNE" + }, + { + "nom": "ROUMANIE" + }, + { + "nom": "ROYAUME-UNI" + }, + { + "nom": "RUSSIE" + }, + { + "nom": "RWANDA" + }, + { + "nom": "SAHARA OCCIDENTAL" + }, + { + "nom": "SAINT-BARTHELEMY" + }, + { + "nom": "SAINT-CHRISTOPHE-ET-NIEVES" + }, + { + "nom": "SAINT-MARIN" + }, + { + "nom": "SAINT-MARTIN" + }, + { + "nom": "SAINT-MARTIN (PARTIE NEERLANDAISE)" + }, + { + "nom": "SAINT-PIERRE-ET-MIQUELON" + }, + { + "nom": "SAINT-VINCENT-ET-LES GRENADINES" + }, + { + "nom": "SAINTE HELENE, ASCENSION ET TRISTAN DA CUNHA" + }, + { + "nom": "SAINTE-LUCIE" + }, + { + "nom": "SALOMON (ILES)" + }, + { + "nom": "SAMOA AMERICAINES" + }, + { + "nom": "SAMOA OCCIDENTALES" + }, + { + "nom": "SAO TOME-ET-PRINCIPE" + }, + { + "nom": "SENEGAL" + }, + { + "nom": "SERBIE" + }, + { + "nom": "SEYCHELLES" + }, + { + "nom": "SIBERIE" + }, + { + "nom": "SIERRA LEONE" + }, + { + "nom": "SINGAPOUR" + }, + { + "nom": "SLOVAQUIE" + }, + { + "nom": "SLOVENIE" + }, + { + "nom": "SOMALIE" + }, + { + "nom": "SOUDAN" + }, + { + "nom": "SOUDAN ANGLO-EGYPTIEN, KENYA, OUGANDA" + }, + { + "nom": "SOUDAN DU SUD" + }, + { + "nom": "SRI LANKA" + }, + { + "nom": "SUEDE" + }, + { + "nom": "SUISSE" + }, + { + "nom": "SURINAME" + }, + { + "nom": "SVALBARD et ILE JAN MAYEN" + }, + { + "nom": "SWAZILAND" + }, + { + "nom": "SYRIE" + }, + { + "nom": "TADJIKISTAN" + }, + { + "nom": "TAIWAN" + }, + { + "nom": "TANGER" + }, + { + "nom": "TANZANIE" + }, + { + "nom": "TCHAD" + }, + { + "nom": "TCHECOSLOVAQUIE" + }, + { + "nom": "TCHEQUE (REPUBLIQUE)" + }, + { + "nom": "TERR. DES ETATS-UNIS D'AMERIQUE EN AMERIQUE" + }, + { + "nom": "TERR. DES ETATS-UNIS D'AMERIQUE EN OCEANIE" + }, + { + "nom": "TERR. DU ROYAUME-UNI DANS L'ATLANTIQUE SUD" + }, + { + "nom": "TERRE-NEUVE" + }, + { + "nom": "TERRES AUSTRALES FRANCAISES" + }, + { + "nom": "TERRITOIRES DU ROYAUME-UNI AUX ANTILLES" + }, + { + "nom": "THAILANDE" + }, + { + "nom": "TIMOR ORIENTAL" + }, + { + "nom": "TOGO" + }, + { + "nom": "TOKELAU" + }, + { + "nom": "TONGA" + }, + { + "nom": "TRINITE-ET-TOBAGO" + }, + { + "nom": "TUNISIE" + }, + { + "nom": "TURKESTAN RUSSE" + }, + { + "nom": "TURKMENISTAN" + }, + { + "nom": "TURKS ET CAIQUES (ILES)" + }, + { + "nom": "TURQUIE" + }, + { + "nom": "TURQUIE D'EUROPE" + }, + { + "nom": "TUVALU" + }, + { + "nom": "UKRAINE" + }, + { + "nom": "URUGUAY" + }, + { + "nom": "VANUATU" + }, + { + "nom": "VATICAN, ou SAINT-SIEGE" + }, + { + "nom": "VENEZUELA" + }, + { + "nom": "VIERGES BRITANNIQUES (ILES)" + }, + { + "nom": "VIERGES DES ETATS-UNIS (ILES)" + }, + { + "nom": "VIET NAM" + }, + { + "nom": "VIET NAM DU NORD" + }, + { + "nom": "VIET NAM DU SUD" + }, + { + "nom": "WALLIS-ET-FUTUNA" + }, + { + "nom": "YEMEN" + }, + { + "nom": "YEMEN (REPUBLIQUE ARABE DU)" + }, + { + "nom": "YEMEN DEMOCRATIQUE" + }, + { + "nom": "ZAMBIE" + }, + { + "nom": "ZANZIBAR" + }, + { + "nom": "ZIMBABWE" + } +] diff --git a/spec/features/users/brouillon_spec.rb b/spec/features/users/brouillon_spec.rb index f73d69475..39d170fbc 100644 --- a/spec/features/users/brouillon_spec.rb +++ b/spec/features/users/brouillon_spec.rb @@ -27,7 +27,7 @@ feature 'The user' do select('bravo', from: form_id_for('simple_choice_drop_down_list_long')) select('alpha', from: form_id_for('multiple_choice_drop_down_list_long')) select('charly', from: form_id_for('multiple_choice_drop_down_list_long')) - select('AUSTRALIE', from: 'pays') + select_champ_geo('pays', 'aust', 'AUSTRALIE') select_champ_geo('regions', 'Ma', 'Martinique') @@ -40,6 +40,7 @@ feature 'The user' do find('.editable-champ-piece_justificative input[type=file]').attach_file(Rails.root + 'spec/fixtures/files/file.pdf') blur + sleep(0.7) expect(page).to have_css('span', text: 'Brouillon enregistré', visible: true) # check data on the dossier @@ -83,7 +84,7 @@ feature 'The user' do expect(page).to have_checked_field('val3') expect(page).to have_selected_value('simple_choice_drop_down_list_long', selected: 'bravo') expect(page).to have_selected_value('multiple_choice_drop_down_list_long', selected: ['alpha', 'charly']) - expect(page).to have_selected_value('pays', selected: 'AUSTRALIE') + expect(page).to have_hidden_field('pays', with: 'AUSTRALIE') expect(page).to have_hidden_field('regions', with: 'Martinique') expect(page).to have_hidden_field('departements', with: '02 - Aisne') expect(page).to have_hidden_field('communes', with: 'Ambléon (01300)') From a8ed9ef172681b7c88d99c37d7ed551036dd3e63 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Wed, 25 Nov 2020 15:28:27 +0100 Subject: [PATCH 2/3] remove dead code --- app/models/champs/pays_champ.rb | 9 --------- 1 file changed, 9 deletions(-) diff --git a/app/models/champs/pays_champ.rb b/app/models/champs/pays_champ.rb index c1c82081e..d158aa6a9 100644 --- a/app/models/champs/pays_champ.rb +++ b/app/models/champs/pays_champ.rb @@ -15,13 +15,4 @@ # type_de_champ_id :integer # class Champs::PaysChamp < Champs::TextChamp - PAYS = JSON.parse(Rails.root.join('app', 'lib', 'api_geo', 'pays.json').read, symbolize_names: true) - - def self.pays - PAYS.pluck(:nom) - end - - def self.disabled_options - pays.filter { |v| (v =~ /^--.*--$/).present? } - end end From ddd50993a5b9412d59c1d7d6d0f4558b187d49c1 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Mon, 30 Nov 2020 10:53:55 +0100 Subject: [PATCH 3/3] cache pays --- app/javascript/components/shared/queryCache.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/app/javascript/components/shared/queryCache.js b/app/javascript/components/shared/queryCache.js index 6809c0473..2e474a5f4 100644 --- a/app/javascript/components/shared/queryCache.js +++ b/app/javascript/components/shared/queryCache.js @@ -33,8 +33,7 @@ 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'] }); + return matchSorter(await getPays(), term, { keys: ['nom'] }); } const url = buildURL(scope, term); @@ -43,3 +42,11 @@ async function defaultQueryFn(scope, term) { promise.cancel = () => controller && controller.abort(); return promise; } + +let paysCache; +async function getPays() { + if (!paysCache) { + paysCache = await fetch('/pays.json').then((response) => response.json()); + } + return paysCache; +}