From f999ac2407f39c3562bca66d3b7a6425a8d61b60 Mon Sep 17 00:00:00 2001 From: Daniel Jackson Date: Wed, 28 Feb 2024 15:14:21 +0100 Subject: [PATCH 01/11] =?UTF-8?q?Rename=20"Sted"=20to=20"Omr=C3=A5de"=20in?= =?UTF-8?q?=20the=20sidebar,=20to=20make=20it=20clearer=20what=20you=20fil?= =?UTF-8?q?ter=20on?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/locales/nb.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/locales/nb.yml b/config/locales/nb.yml index b9b91461..526fbcbf 100644 --- a/config/locales/nb.yml +++ b/config/locales/nb.yml @@ -83,7 +83,7 @@ nb: one: "Ett lokale passer:" other: "%{count} lokaler passer:" edit_filter: 'Rediger filtre' - location: 'Sted' + location: 'Område' title: 'Navn på lokalet' more_filters: 'Flere filtre' map_reload: 'Søk i området' From e3d70af2775100340a2d07b783a1600487978ea2 Mon Sep 17 00:00:00 2001 From: Daniel Jackson Date: Thu, 29 Feb 2024 09:09:07 +0100 Subject: [PATCH 02/11] Preload a list of all kommuner and fylker for location select --- app/assets/stylesheets/tom_select.css | 14 +- .../controllers/locationsearch_controller.js | 50 +- .../search_and_filter/fylker_and_kommuner.js | 1572 +++++++++++++++++ app/views/spaces/index/_search_form.html.erb | 5 +- config/locales/nb.yml | 3 + 5 files changed, 1619 insertions(+), 25 deletions(-) create mode 100644 app/javascript/controllers/search_and_filter/fylker_and_kommuner.js diff --git a/app/assets/stylesheets/tom_select.css b/app/assets/stylesheets/tom_select.css index 82817833..a71b38fe 100644 --- a/app/assets/stylesheets/tom_select.css +++ b/app/assets/stylesheets/tom_select.css @@ -22,6 +22,10 @@ @apply text-sm; } +.ts-control .item { + @apply text-sm bg-gray-200 px-2 py-0.5; +} + .dropdown { .ts-control { background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3e%3c/svg%3e"); @@ -62,15 +66,16 @@ .plugin-clear_button .clear-button { opacity: 0; position: absolute; - top: 8px; + top: 0; right: calc(8px - 6px); margin-right: 0 !important; background: transparent !important; transition: opacity 0.5s; cursor: pointer; + @apply p-2 text-sm; } .plugin-clear_button.single .clear-button { - right: calc(8px - 6px + 2rem); + right: 8px; } .plugin-clear_button.focus.has-items .clear-button, .plugin-clear_button:hover.has-items .clear-button { opacity: 1; @@ -336,8 +341,9 @@ border-top: 0 none; } .ts-dropdown .optgroup-header { - color: #303030; - background: #fff; + color: theme('colors.gray.500'); + @apply text-sm; + @apply pt-4; cursor: default; } diff --git a/app/javascript/controllers/locationsearch_controller.js b/app/javascript/controllers/locationsearch_controller.js index d7a97ebb..c13b4c96 100644 --- a/app/javascript/controllers/locationsearch_controller.js +++ b/app/javascript/controllers/locationsearch_controller.js @@ -1,5 +1,6 @@ import { Controller } from '@hotwired/stimulus' import TomSelect from 'tom-select' +import fylkerAndKommuner from "./search_and_filter/fylker_and_kommuner"; export default class extends Controller { static values = { @@ -8,29 +9,40 @@ export default class extends Controller { connect() { this.tomselect = new TomSelect('#locationInput', { - valueField: 'text', - labelField: 'text', - searchField: 'text', - allowEmptyOption: true, - create: true, - load: (search, callback) => { - this.searchGeoNorge(search, callback) - }} + maxOptions: null, + valueField: 'kommune', + labelField: 'kommune', + searchField: ['kommune', 'fylke'], + optgroupField: 'fylke', + sortField: ['fylke', 'kommune'], + allowEmptyOption: true, + persist: false, // but do not store user inputted options as dropdown items + plugins: ['clear_button'], // For multiple select + optgroups: this.optGroups, + options: this.options + } ); } - async searchGeoNorge(search, callback) { - if(search === "") - callback([]); - - const url =`https://ws.geonorge.no/stedsnavn/v1/sted?sok=${search}&fuzzy=true` - - const result = await (await fetch(url)).json(); + get optGroups() { + return fylkerAndKommuner.map(fylke => ({ value: fylke.name, label: fylke.name })) + } - const data = result.navn.map((name) => { - return { text: name.stedsnavn[0].skrivemåte } - }); + get options() { + console.log(fylkerAndKommuner) + return fylkerAndKommuner.map(fylke => { + if (!fylke.kommuner) return { + kommune: fylke.name, + fylke: fylke.name + } - callback(data); + return fylke.kommuner.map(kommune => { + return { + kommune: kommune.name, + fylke: fylke.name + } + }) + }).flat() } } + diff --git a/app/javascript/controllers/search_and_filter/fylker_and_kommuner.js b/app/javascript/controllers/search_and_filter/fylker_and_kommuner.js new file mode 100644 index 00000000..e06a050d --- /dev/null +++ b/app/javascript/controllers/search_and_filter/fylker_and_kommuner.js @@ -0,0 +1,1572 @@ +export default [ + { + "name": "Hele Norge", + "id": "hele-landet" + }, + { + "name": "Oslo", + "id": "fylke-03", + "kommuner": [ + { + "name": "Bydel Alna", + "id": "alna" + },{ + "name": "Bydel Bjerke", + "id": "bjerke" + },{ + "name": "Bydel Frogner", + "id": "frogner" + },{ + "name": "Bydel Gamle Oslo", + "id": "gamle-oslo" + },{ + "name": "Bydel Grorud", + "id": "grorud" + },{ + "name": "Bydel Grünerløkka", + "id": "grunderlokka" + },{ + "name": "Bydel Nordre Aker", + "id": "norde-aker" + },{ + "name": "Bydel Nordstrand", + "id": "nordstrand" + },{ + "name": "Bydel Sagene", + "id": "sagene" + }, + { + "name": "Bydel St. Hanshaugen", + "id": "sthanshaugen" + }, + { + "name": "Bydel Stovner", + "id": "stovner" + }, + { + "name": "Bydel Søndre Nordstrand", + "id": "sondre-nordstrand" + }, + { + "name": "Bydel Ullern", + "id": "ullern" + }, + { + "name": "Bydel Vestre Aker", + "id": "vestre-aker" + }, + { + "name": "Bydel Østensjø", + "id": "ostensjo" + } + + ] + }, { + "name": "Rogaland", + "id": "fylke-11", + "kommuner": [ + { + "name": "Eigersund", + "id": "kommune-1101" + }, + { + "name": "Stavanger", + "id": "kommune-1103" + }, + { + "name": "Haugesund", + "id": "kommune-1106" + }, + { + "name": "Sandnes", + "id": "kommune-1108" + }, + { + "name": "Sokndal", + "id": "kommune-1111" + }, + { + "name": "Lund", + "id": "kommune-1112" + }, + { + "name": "Bjerkreim", + "id": "kommune-1114" + }, + { + "name": "Hå", + "id": "kommune-1119" + }, + { + "name": "Klepp", + "id": "kommune-1120" + }, + { + "name": "Time", + "id": "kommune-1121" + }, + { + "name": "Gjesdal", + "id": "kommune-1122" + }, + { + "name": "Sola", + "id": "kommune-1124" + }, + { + "name": "Randaberg", + "id": "kommune-1127" + }, + { + "name": "Strand", + "id": "kommune-1130" + }, + { + "name": "Hjelmeland", + "id": "kommune-1133" + }, + { + "name": "Suldal", + "id": "kommune-1134" + }, + { + "name": "Sauda", + "id": "kommune-1135" + }, + { + "name": "Kvitsøy", + "id": "kommune-1144" + }, + { + "name": "Bokn", + "id": "kommune-1145" + }, + { + "name": "Tysvær", + "id": "kommune-1146" + }, + { + "name": "Karmøy", + "id": "kommune-1149" + }, + { + "name": "Utsira", + "id": "kommune-1151" + }, + { + "name": "Vindafjord", + "id": "kommune-1160" + } + ] + }, + { + "name": "Møre og Romsdal", + "id": "fylke-15", + "kommuner": [ + { + "name": "Kristiansund", + "id": "kommune-1505" + }, + { + "name": "Molde", + "id": "kommune-1506" + }, + { + "name": "Ålesund", + "id": "kommune-1508" + }, + { + "name": "Vanylven", + "id": "kommune-1511" + }, + { + "name": "Sande", + "id": "kommune-1514" + }, + { + "name": "Herøy", + "id": "kommune-1515" + }, + { + "name": "Ulstein", + "id": "kommune-1516" + }, + { + "name": "Hareid", + "id": "kommune-1517" + }, + { + "name": "Ørsta", + "id": "kommune-1520" + }, + { + "name": "Stranda", + "id": "kommune-1525" + }, + { + "name": "Sykkylven", + "id": "kommune-1528" + }, + { + "name": "Sula", + "id": "kommune-1531" + }, + { + "name": "Giske", + "id": "kommune-1532" + }, + { + "name": "Vestnes", + "id": "kommune-1535" + }, + { + "name": "Rauma", + "id": "kommune-1539" + }, + { + "name": "Aukra", + "id": "kommune-1547" + }, + { + "name": "Averøy", + "id": "kommune-1554" + }, + { + "name": "Gjemnes", + "id": "kommune-1557" + }, + { + "name": "Tingvoll", + "id": "kommune-1560" + }, + { + "name": "Sunndal", + "id": "kommune-1563" + }, + { + "name": "Surnadal", + "id": "kommune-1566" + }, + { + "name": "Smøla", + "id": "kommune-1573" + }, + { + "name": "Aure", + "id": "kommune-1576" + }, + { + "name": "Volda", + "id": "kommune-1577" + }, + { + "name": "Fjord", + "id": "kommune-1578" + }, + { + "name": "Hustadvika", + "id": "kommune-1579" + }, + { + "name": "Haram", + "id": "kommune-1580" + } + ] + }, + { + "name": "Nordland", + "id": "fylke-18", + "kommuner": [ + { + "name": "Andøy", + "id": "kommune-1871" + }, + { + "name": "Sømna", + "id": "kommune-1812" + }, + { + "name": "Brønnøy", + "id": "kommune-1813" + }, + { + "name": "Vega", + "id": "kommune-1815" + }, + { + "name": "Vevelstad", + "id": "kommune-1816" + }, + { + "name": "Hábmer", + "id": "kommune-1875" + }, + { + "name": "Steigen", + "id": "kommune-1848" + }, + { + "name": "Lødingen", + "id": "kommune-1851" + }, + { + "name": "Evenes", + "id": "kommune-1853" + }, + { + "name": "Bodø", + "id": "kommune-1804" + }, + { + "name": "Narvik", + "id": "kommune-1806" + }, + { + "name": "Bindal", + "id": "kommune-1811" + }, + { + "name": "Herøy", + "id": "kommune-1818" + }, + { + "name": "Alstahaug", + "id": "kommune-1820" + }, + { + "name": "Leirfjord", + "id": "kommune-1822" + }, + { + "name": "Vefsn", + "id": "kommune-1824" + }, + { + "name": "Grane", + "id": "kommune-1825" + }, + { + "name": "Aarborte", + "id": "kommune-1826" + }, + { + "name": "Dønna", + "id": "kommune-1827" + }, + { + "name": "Nesna", + "id": "kommune-1828" + }, + { + "name": "Hemnes", + "id": "kommune-1832" + }, + { + "name": "Rana", + "id": "kommune-1833" + }, + { + "name": "Lurøy", + "id": "kommune-1834" + }, + { + "name": "Træna", + "id": "kommune-1835" + }, + { + "name": "Rødøy", + "id": "kommune-1836" + }, + { + "name": "Meløy", + "id": "kommune-1837" + }, + { + "name": "Gildeskål", + "id": "kommune-1838" + }, + { + "name": "Beiarn", + "id": "kommune-1839" + }, + { + "name": "Saltdal", + "id": "kommune-1840" + }, + { + "name": "Fauske", + "id": "kommune-1841" + }, + { + "name": "Sørfold", + "id": "kommune-1845" + }, + { + "name": "Røst", + "id": "kommune-1856" + }, + { + "name": "Værøy", + "id": "kommune-1857" + }, + { + "name": "Vestvågøy", + "id": "kommune-1860" + }, + { + "name": "Flakstad", + "id": "kommune-1859" + }, + { + "name": "Vågan", + "id": "kommune-1865" + }, + { + "name": "Hadsel", + "id": "kommune-1866" + }, + { + "name": "Bø", + "id": "kommune-1867" + }, + { + "name": "Øksnes", + "id": "kommune-1868" + }, + { + "name": "Sortland", + "id": "kommune-1870" + }, + { + "name": "Moskenes", + "id": "kommune-1874" + } + ] + }, + { + "name": "Østfold", + "id": "fylke-31", + "kommuner": [ + { + "name": "Skiptvet", + "id": "kommune-3116" + }, + { + "name": "Indre Østfold", + "id": "kommune-3118" + }, + { + "name": "Halden", + "id": "kommune-3101" + }, + { + "name": "Moss", + "id": "kommune-3103" + }, + { + "name": "Sarpsborg", + "id": "kommune-3105" + }, + { + "name": "Fredrikstad", + "id": "kommune-3107" + }, + { + "name": "Hvaler", + "id": "kommune-3110" + }, + { + "name": "Råde", + "id": "kommune-3112" + }, + { + "name": "Våler", + "id": "kommune-3114" + }, + { + "name": "Rakkestad", + "id": "kommune-3120" + }, + { + "name": "Marker", + "id": "kommune-3122" + }, + { + "name": "Aremark", + "id": "kommune-3124" + } + ] + }, + { + "name": "Akershus", + "id": "fylke-32", + "kommuner": [ + { + "name": "Bærum", + "id": "kommune-3201" + }, + { + "name": "Asker", + "id": "kommune-3203" + }, + { + "name": "Lillestrøm", + "id": "kommune-3205" + }, + { + "name": "Nordre Follo", + "id": "kommune-3207" + }, + { + "name": "Ullensaker", + "id": "kommune-3209" + }, + { + "name": "Nesodden", + "id": "kommune-3212" + }, + { + "name": "Frogn", + "id": "kommune-3214" + }, + { + "name": "Vestby", + "id": "kommune-3216" + }, + { + "name": "Ås", + "id": "kommune-3218" + }, + { + "name": "Enebakk", + "id": "kommune-3220" + }, + { + "name": "Lørenskog", + "id": "kommune-3222" + }, + { + "name": "Rælingen", + "id": "kommune-3224" + }, + { + "name": "Aurskog-Høland", + "id": "kommune-3226" + }, + { + "name": "Nes", + "id": "kommune-3228" + }, + { + "name": "Gjerdrum", + "id": "kommune-3230" + }, + { + "name": "Nittedal", + "id": "kommune-3232" + }, + { + "name": "Lunner", + "id": "kommune-3234" + }, + { + "name": "Jevnaker", + "id": "kommune-3236" + }, + { + "name": "Nannestad", + "id": "kommune-3238" + }, + { + "name": "Eidsvoll", + "id": "kommune-3240" + }, + { + "name": "Hurdal", + "id": "kommune-3242" + } + ] + }, + { + "name": "Buskerud", + "id": "fylke-33", + "kommuner": [ + { + "name": "Drammen", + "id": "kommune-3301" + }, + { + "name": "Kongsberg", + "id": "kommune-3303" + }, + { + "name": "Ringerike", + "id": "kommune-3305" + }, + { + "name": "Hole", + "id": "kommune-3310" + }, + { + "name": "Lier", + "id": "kommune-3312" + }, + { + "name": "Øvre Eiker", + "id": "kommune-3314" + }, + { + "name": "Modum", + "id": "kommune-3316" + }, + { + "name": "Krødsherad", + "id": "kommune-3318" + }, + { + "name": "Flå", + "id": "kommune-3320" + }, + { + "name": "Nesbyen", + "id": "kommune-3322" + }, + { + "name": "Gol", + "id": "kommune-3324" + }, + { + "name": "Hemsedal", + "id": "kommune-3326" + }, + { + "name": "Ål", + "id": "kommune-3328" + }, + { + "name": "Hol", + "id": "kommune-3330" + }, + { + "name": "Sigdal", + "id": "kommune-3332" + }, + { + "name": "Flesberg", + "id": "kommune-3334" + }, + { + "name": "Rollag", + "id": "kommune-3336" + }, + { + "name": "Nore og Uvdal", + "id": "kommune-3338" + } + ] + }, + { + "name": "Innlandet", + "id": "fylke-34", + "kommuner": [ + { + "name": "Dovre", + "id": "kommune-3431" + }, + { + "name": "Kongsvinger", + "id": "kommune-3401" + }, + { + "name": "Hamar", + "id": "kommune-3403" + }, + { + "name": "Lillehammer", + "id": "kommune-3405" + }, + { + "name": "Gjøvik", + "id": "kommune-3407" + }, + { + "name": "Ringsaker", + "id": "kommune-3411" + }, + { + "name": "Løten", + "id": "kommune-3412" + }, + { + "name": "Stange", + "id": "kommune-3413" + }, + { + "name": "Nord-Odal", + "id": "kommune-3414" + }, + { + "name": "Sør-Odal", + "id": "kommune-3415" + }, + { + "name": "Eidskog", + "id": "kommune-3416" + }, + { + "name": "Grue", + "id": "kommune-3417" + }, + { + "name": "Åsnes", + "id": "kommune-3418" + }, + { + "name": "Våler", + "id": "kommune-3419" + }, + { + "name": "Elverum", + "id": "kommune-3420" + }, + { + "name": "Trysil", + "id": "kommune-3421" + }, + { + "name": "Åmot", + "id": "kommune-3422" + }, + { + "name": "Stor-Elvdal", + "id": "kommune-3423" + }, + { + "name": "Rendalen", + "id": "kommune-3424" + }, + { + "name": "Engerdal", + "id": "kommune-3425" + }, + { + "name": "Tolga", + "id": "kommune-3426" + }, + { + "name": "Tynset", + "id": "kommune-3427" + }, + { + "name": "Alvdal", + "id": "kommune-3428" + }, + { + "name": "Folldal", + "id": "kommune-3429" + }, + { + "name": "Os", + "id": "kommune-3430" + }, + { + "name": "Lesja", + "id": "kommune-3432" + }, + { + "name": "Skjåk", + "id": "kommune-3433" + }, + { + "name": "Lom", + "id": "kommune-3434" + }, + { + "name": "Vågå", + "id": "kommune-3435" + }, + { + "name": "Nord-Fron", + "id": "kommune-3436" + }, + { + "name": "Sel", + "id": "kommune-3437" + }, + { + "name": "Sør-Fron", + "id": "kommune-3438" + }, + { + "name": "Ringebu", + "id": "kommune-3439" + }, + { + "name": "Øyer", + "id": "kommune-3440" + }, + { + "name": "Gausdal", + "id": "kommune-3441" + }, + { + "name": "Østre Toten", + "id": "kommune-3442" + }, + { + "name": "Vestre Toten", + "id": "kommune-3443" + }, + { + "name": "Gran", + "id": "kommune-3446" + }, + { + "name": "Søndre Land", + "id": "kommune-3447" + }, + { + "name": "Nordre Land", + "id": "kommune-3448" + }, + { + "name": "Sør-Aurdal", + "id": "kommune-3449" + }, + { + "name": "Etnedal", + "id": "kommune-3450" + }, + { + "name": "Nord-Aurdal", + "id": "kommune-3451" + }, + { + "name": "Vestre Slidre", + "id": "kommune-3452" + }, + { + "name": "Øystre Slidre", + "id": "kommune-3453" + }, + { + "name": "Vang", + "id": "kommune-3454" + } + ] + }, + { + "name": "Vestfold", + "id": "fylke-39", + "kommuner": [ + { + "name": "Horten", + "id": "kommune-3901" + }, + { + "name": "Holmestrand", + "id": "kommune-3903" + }, + { + "name": "Tønsberg", + "id": "kommune-3905" + }, + { + "name": "Sandefjord", + "id": "kommune-3907" + }, + { + "name": "Larvik", + "id": "kommune-3909" + }, + { + "name": "Færder", + "id": "kommune-3911" + } + ] + }, + { + "name": "Telemark", + "id": "fylke-40", + "kommuner": [ + { + "name": "Hjartdal", + "id": "kommune-4024" + }, + { + "name": "Tinn", + "id": "kommune-4026" + }, + { + "name": "Kviteseid", + "id": "kommune-4028" + }, + { + "name": "Nissedal", + "id": "kommune-4030" + }, + { + "name": "Fyresdal", + "id": "kommune-4032" + }, + { + "name": "Porsgrunn", + "id": "kommune-4001" + }, + { + "name": "Skien", + "id": "kommune-4003" + }, + { + "name": "Notodden", + "id": "kommune-4005" + }, + { + "name": "Siljan", + "id": "kommune-4010" + }, + { + "name": "Bamble", + "id": "kommune-4012" + }, + { + "name": "Kragerø", + "id": "kommune-4014" + }, + { + "name": "Drangedal", + "id": "kommune-4016" + }, + { + "name": "Nome", + "id": "kommune-4018" + }, + { + "name": "Midt-Telemark", + "id": "kommune-4020" + }, + { + "name": "Seljord", + "id": "kommune-4022" + }, + { + "name": "Tokke", + "id": "kommune-4034" + }, + { + "name": "Vinje", + "id": "kommune-4036" + } + ] + }, + { + "name": "Agder", + "id": "fylke-42", + "kommuner": [ + { + "name": "Risør", + "id": "kommune-4201" + }, + { + "name": "Grimstad", + "id": "kommune-4202" + }, + { + "name": "Arendal", + "id": "kommune-4203" + }, + { + "name": "Kristiansand", + "id": "kommune-4204" + }, + { + "name": "Lindesnes", + "id": "kommune-4205" + }, + { + "name": "Farsund", + "id": "kommune-4206" + }, + { + "name": "Flekkefjord", + "id": "kommune-4207" + }, + { + "name": "Gjerstad", + "id": "kommune-4211" + }, + { + "name": "Vegårshei", + "id": "kommune-4212" + }, + { + "name": "Tvedestrand", + "id": "kommune-4213" + }, + { + "name": "Froland", + "id": "kommune-4214" + }, + { + "name": "Lillesand", + "id": "kommune-4215" + }, + { + "name": "Birkenes", + "id": "kommune-4216" + }, + { + "name": "Åmli", + "id": "kommune-4217" + }, + { + "name": "Iveland", + "id": "kommune-4218" + }, + { + "name": "Evje og Hornnes", + "id": "kommune-4219" + }, + { + "name": "Bygland", + "id": "kommune-4220" + }, + { + "name": "Valle", + "id": "kommune-4221" + }, + { + "name": "Bykle", + "id": "kommune-4222" + }, + { + "name": "Vennesla", + "id": "kommune-4223" + }, + { + "name": "Åseral", + "id": "kommune-4224" + }, + { + "name": "Lyngdal", + "id": "kommune-4225" + }, + { + "name": "Hægebostad", + "id": "kommune-4226" + }, + { + "name": "Kvinesdal", + "id": "kommune-4227" + }, + { + "name": "Sirdal", + "id": "kommune-4228" + } + ] + }, + { + "name": "Vestland", + "id": "fylke-46", + "kommuner": [ + { + "name": "Bergen", + "id": "kommune-4601" + }, + { + "name": "Kinn", + "id": "kommune-4602" + }, + { + "name": "Etne", + "id": "kommune-4611" + }, + { + "name": "Bømlo", + "id": "kommune-4613" + }, + { + "name": "Tysnes", + "id": "kommune-4616" + }, + { + "name": "Kvinnherad", + "id": "kommune-4617" + }, + { + "name": "Ullensvang", + "id": "kommune-4618" + }, + { + "name": "Eidfjord", + "id": "kommune-4619" + }, + { + "name": "Ulvik", + "id": "kommune-4620" + }, + { + "name": "Voss", + "id": "kommune-4621" + }, + { + "name": "Sveio", + "id": "kommune-4612" + }, + { + "name": "Kvam", + "id": "kommune-4622" + }, + { + "name": "Samnanger", + "id": "kommune-4623" + }, + { + "name": "Stord", + "id": "kommune-4614" + }, + { + "name": "Bjørnafjorden", + "id": "kommune-4624" + }, + { + "name": "Fedje", + "id": "kommune-4633" + }, + { + "name": "Fitjar", + "id": "kommune-4615" + }, + { + "name": "Austevoll", + "id": "kommune-4625" + }, + { + "name": "Øygarden", + "id": "kommune-4626" + }, + { + "name": "Askøy", + "id": "kommune-4627" + }, + { + "name": "Vaksdal", + "id": "kommune-4628" + }, + { + "name": "Modalen", + "id": "kommune-4629" + }, + { + "name": "Osterøy", + "id": "kommune-4630" + }, + { + "name": "Alver", + "id": "kommune-4631" + }, + { + "name": "Austrheim", + "id": "kommune-4632" + }, + { + "name": "Masfjorden", + "id": "kommune-4634" + }, + { + "name": "Gulen", + "id": "kommune-4635" + }, + { + "name": "Solund", + "id": "kommune-4636" + }, + { + "name": "Hyllestad", + "id": "kommune-4637" + }, + { + "name": "Høyanger", + "id": "kommune-4638" + }, + { + "name": "Vik", + "id": "kommune-4639" + }, + { + "name": "Sogndal", + "id": "kommune-4640" + }, + { + "name": "Aurland", + "id": "kommune-4641" + }, + { + "name": "Lærdal", + "id": "kommune-4642" + }, + { + "name": "Årdal", + "id": "kommune-4643" + }, + { + "name": "Luster", + "id": "kommune-4644" + }, + { + "name": "Askvoll", + "id": "kommune-4645" + }, + { + "name": "Fjaler", + "id": "kommune-4646" + }, + { + "name": "Sunnfjord", + "id": "kommune-4647" + }, + { + "name": "Stad", + "id": "kommune-4649" + }, + { + "name": "Gloppen", + "id": "kommune-4650" + }, + { + "name": "Stryn", + "id": "kommune-4651" + }, + { + "name": "Bremanger", + "id": "kommune-4648" + } + ] + }, + { + "name": "Trøndelag", + "id": "fylke-50", + "kommuner": [ + { + "name": "Trondheim", + "id": "kommune-5001" + }, + { + "name": "Steinkjer", + "id": "kommune-5006" + }, + { + "name": "Namsos", + "id": "kommune-5007" + }, + { + "name": "Frøya", + "id": "kommune-5014" + }, + { + "name": "Osen", + "id": "kommune-5020" + }, + { + "name": "Oppdal", + "id": "kommune-5021" + }, + { + "name": "Rennebu", + "id": "kommune-5022" + }, + { + "name": "Rosse", + "id": "kommune-5025" + }, + { + "name": "Holtålen", + "id": "kommune-5026" + }, + { + "name": "Midtre Gauldal", + "id": "kommune-5027" + }, + { + "name": "Melhus", + "id": "kommune-5028" + }, + { + "name": "Skaun", + "id": "kommune-5029" + }, + { + "name": "Malvik", + "id": "kommune-5031" + }, + { + "name": "Selbu", + "id": "kommune-5032" + }, + { + "name": "Tydal", + "id": "kommune-5033" + }, + { + "name": "Meråker", + "id": "kommune-5034" + }, + { + "name": "Stjørdal", + "id": "kommune-5035" + }, + { + "name": "Frosta", + "id": "kommune-5036" + }, + { + "name": "Levanger", + "id": "kommune-5037" + }, + { + "name": "Verdal", + "id": "kommune-5038" + }, + { + "name": "Snåase", + "id": "kommune-5041" + }, + { + "name": "Lierne", + "id": "kommune-5042" + }, + { + "name": "Høylandet", + "id": "kommune-5046" + }, + { + "name": "Overhalla", + "id": "kommune-5047" + }, + { + "name": "Flatanger", + "id": "kommune-5049" + }, + { + "name": "Heim", + "id": "kommune-5055" + }, + { + "name": "Raarvihke", + "id": "kommune-5043" + }, + { + "name": "Namsskogan", + "id": "kommune-5044" + }, + { + "name": "Grong", + "id": "kommune-5045" + }, + { + "name": "Leka", + "id": "kommune-5052" + }, + { + "name": "Inderøy", + "id": "kommune-5053" + }, + { + "name": "Indre Fosen", + "id": "kommune-5054" + }, + { + "name": "Hitra", + "id": "kommune-5056" + }, + { + "name": "Ørland", + "id": "kommune-5057" + }, + { + "name": "Åfjord", + "id": "kommune-5058" + }, + { + "name": "Orkland", + "id": "kommune-5059" + }, + { + "name": "Nærøysund", + "id": "kommune-5060" + }, + { + "name": "Rindal", + "id": "kommune-5061" + } + ] + }, + { + "name": "Troms", + "id": "fylke-55", + "kommuner": [ + { + "name": "Loabák", + "id": "kommune-5518" + }, + { + "name": "Bardu", + "id": "kommune-5520" + }, + { + "name": "Salangen", + "id": "kommune-5522" + }, + { + "name": "Målselv", + "id": "kommune-5524" + }, + { + "name": "Sørreisa", + "id": "kommune-5526" + }, + { + "name": "Tromsø", + "id": "kommune-5501" + }, + { + "name": "Harstad", + "id": "kommune-5503" + }, + { + "name": "Kvæfjord", + "id": "kommune-5510" + }, + { + "name": "Dielddanuorri", + "id": "kommune-5512" + }, + { + "name": "Ibestad", + "id": "kommune-5514" + }, + { + "name": "Gratangen", + "id": "kommune-5516" + }, + { + "name": "Dyrøy", + "id": "kommune-5528" + }, + { + "name": "Senja", + "id": "kommune-5530" + }, + { + "name": "Balsfjord", + "id": "kommune-5532" + }, + { + "name": "Karlsøy", + "id": "kommune-5534" + }, + { + "name": "Lyngen", + "id": "kommune-5536" + }, + { + "name": "Storfjord", + "id": "kommune-5538" + }, + { + "name": "Gáivuotna", + "id": "kommune-5540" + }, + { + "name": "Skjervøy", + "id": "kommune-5542" + }, + { + "name": "Nordreisa", + "id": "kommune-5544" + }, + { + "name": "Kvænangen", + "id": "kommune-5546" + } + ] + }, + { + "name": "Finnmark", + "id": "fylke-56", + "kommuner": [ + { + "name": "Måsøy", + "id": "kommune-5618" + }, + { + "name": "Nordkapp", + "id": "kommune-5620" + }, + { + "name": "Alta", + "id": "kommune-5601" + }, + { + "name": "Hammerfest", + "id": "kommune-5603" + }, + { + "name": "Sør-Varanger", + "id": "kommune-5605" + }, + { + "name": "Vadsø", + "id": "kommune-5607" + }, + { + "name": "Kárášjohka", + "id": "kommune-5610" + }, + { + "name": "Guovdageaidnu", + "id": "kommune-5612" + }, + { + "name": "Loppa", + "id": "kommune-5614" + }, + { + "name": "Hasvik", + "id": "kommune-5616" + }, + { + "name": "Porsanger", + "id": "kommune-5622" + }, + { + "name": "Lebesby", + "id": "kommune-5624" + }, + { + "name": "Gamvik", + "id": "kommune-5626" + }, + { + "name": "Deatnu", + "id": "kommune-5628" + }, + { + "name": "Berlevåg", + "id": "kommune-5630" + }, + { + "name": "Båtsfjord", + "id": "kommune-5632" + }, + { + "name": "Vardø", + "id": "kommune-5634" + }, + { + "name": "Unjárga", + "id": "kommune-5636" + } + ] + } +] diff --git a/app/views/spaces/index/_search_form.html.erb b/app/views/spaces/index/_search_form.html.erb index 24e96995..110abb68 100644 --- a/app/views/spaces/index/_search_form.html.erb +++ b/app/views/spaces/index/_search_form.html.erb @@ -3,7 +3,7 @@ } do |f| %>
- <%= f.label t("space_filter.title"), class: "text-xl font-bold" %> + <%= f.label t("space_filter.title"), for: :title, class: "text-xl font-bold" %>
<%= f.text_field :title, data: { mapbox_target: "title" }, @@ -12,13 +12,14 @@
- <%= f.label t("space_filter.location"), class: "text-xl font-bold" %> + <%= f.label t("space_filter.choose_municipality"), for: "locationInput", class: "text-xl font-bold" %>
+

<%= t("space_filter.or_choose_by_map") %>

diff --git a/config/locales/nb.yml b/config/locales/nb.yml index 526fbcbf..bb798c72 100644 --- a/config/locales/nb.yml +++ b/config/locales/nb.yml @@ -84,6 +84,9 @@ nb: other: "%{count} lokaler passer:" edit_filter: 'Rediger filtre' location: 'Område' + municipality: 'Kommune' + choose_municipality: 'Velg kommune' + or_choose_by_map: 'Eller flytt på kart' title: 'Navn på lokalet' more_filters: 'Flere filtre' map_reload: 'Søk i området' From 9ed967c26b311fe6e05a6701034efdb34e73b06f Mon Sep 17 00:00:00 2001 From: Daniel Jackson Date: Thu, 29 Feb 2024 10:11:31 +0100 Subject: [PATCH 03/11] Remove bydeler for now, as there is no easy way to implement them atm --- .../search_and_filter/fylker_and_kommuner.js | 61 +------------------ 1 file changed, 3 insertions(+), 58 deletions(-) diff --git a/app/javascript/controllers/search_and_filter/fylker_and_kommuner.js b/app/javascript/controllers/search_and_filter/fylker_and_kommuner.js index e06a050d..62db3ccf 100644 --- a/app/javascript/controllers/search_and_filter/fylker_and_kommuner.js +++ b/app/javascript/controllers/search_and_filter/fylker_and_kommuner.js @@ -1,66 +1,11 @@ export default [ { - "name": "Hele Norge", - "id": "hele-landet" + "name": "Norge", + "id": "" }, { "name": "Oslo", - "id": "fylke-03", - "kommuner": [ - { - "name": "Bydel Alna", - "id": "alna" - },{ - "name": "Bydel Bjerke", - "id": "bjerke" - },{ - "name": "Bydel Frogner", - "id": "frogner" - },{ - "name": "Bydel Gamle Oslo", - "id": "gamle-oslo" - },{ - "name": "Bydel Grorud", - "id": "grorud" - },{ - "name": "Bydel Grünerløkka", - "id": "grunderlokka" - },{ - "name": "Bydel Nordre Aker", - "id": "norde-aker" - },{ - "name": "Bydel Nordstrand", - "id": "nordstrand" - },{ - "name": "Bydel Sagene", - "id": "sagene" - }, - { - "name": "Bydel St. Hanshaugen", - "id": "sthanshaugen" - }, - { - "name": "Bydel Stovner", - "id": "stovner" - }, - { - "name": "Bydel Søndre Nordstrand", - "id": "sondre-nordstrand" - }, - { - "name": "Bydel Ullern", - "id": "ullern" - }, - { - "name": "Bydel Vestre Aker", - "id": "vestre-aker" - }, - { - "name": "Bydel Østensjø", - "id": "ostensjo" - } - - ] + "id": "fylke-03" }, { "name": "Rogaland", "id": "fylke-11", From 6ecfae0f2ac341349a66588424fd84e831e38144 Mon Sep 17 00:00:00 2001 From: Daniel Jackson Date: Thu, 29 Feb 2024 10:12:06 +0100 Subject: [PATCH 04/11] Get locations from geonorge based on kommuner and fylker, not by seraching by name --- .../controllers/locationsearch_controller.js | 28 +++--- .../controllers/mapbox_controller.js | 97 +++++++++++-------- config/locales/nb.yml | 4 +- 3 files changed, 73 insertions(+), 56 deletions(-) diff --git a/app/javascript/controllers/locationsearch_controller.js b/app/javascript/controllers/locationsearch_controller.js index c13b4c96..6125a966 100644 --- a/app/javascript/controllers/locationsearch_controller.js +++ b/app/javascript/controllers/locationsearch_controller.js @@ -10,12 +10,11 @@ export default class extends Controller { connect() { this.tomselect = new TomSelect('#locationInput', { maxOptions: null, - valueField: 'kommune', + valueField: 'value', labelField: 'kommune', searchField: ['kommune', 'fylke'], optgroupField: 'fylke', sortField: ['fylke', 'kommune'], - allowEmptyOption: true, persist: false, // but do not store user inputted options as dropdown items plugins: ['clear_button'], // For multiple select optgroups: this.optGroups, @@ -29,19 +28,26 @@ export default class extends Controller { } get options() { - console.log(fylkerAndKommuner) return fylkerAndKommuner.map(fylke => { - if (!fylke.kommuner) return { - kommune: fylke.name, + const options = [] + + options.push({ + kommune: `Hele ${fylke.name}`, + value: fylke.id, fylke: fylke.name + }) + + if (fylke.kommuner) { + fylke.kommuner.forEach(kommune => { + options.push({ + kommune: kommune.name, + value: kommune.id, + fylke: fylke.name + }) + }) } - return fylke.kommuner.map(kommune => { - return { - kommune: kommune.name, - fylke: fylke.name - } - }) + return options }).flat() } } diff --git a/app/javascript/controllers/mapbox_controller.js b/app/javascript/controllers/mapbox_controller.js index dd98dae6..fe4cc4a4 100644 --- a/app/javascript/controllers/mapbox_controller.js +++ b/app/javascript/controllers/mapbox_controller.js @@ -264,14 +264,14 @@ export default class extends Controller { }); this.locationTarget.onchange = (event) => { - this.submitSearch(event) + this.getSearchCoordinatesFromGeoNorge(event) }; this.formTarget.onsubmit = (event) => { // To stop the form from submitting, as that currently does nothing but refresh the page. event.preventDefault() this.clearDebounce("titleSearch") - this.submitSearch(event) + this.getSearchCoordinatesFromGeoNorge(event) this.hideSearchBox() }; } @@ -311,15 +311,62 @@ export default class extends Controller { } } - async submitSearch(event) { - const url =`https://ws.geonorge.no/stedsnavn/v1/sted?sok=${event.target.value}&fuzzy=true` + async getSearchCoordinatesFromGeoNorge(event) { + + const url = this.getUrlForGeoNorgeLocation(event); + + if (!url) { + return this.moveMapToFitNorway(); + } + const result = await (await fetch(url)).json(); + this.moveMapToGeoNorgeLocation(result); + } + + getUrlForGeoNorgeLocation(event) { + const fylkesnummer = event.target.value.split('fylke-')[1]; + const kommunenummer = event.target.value.split('kommune-')[1]; + + if (kommunenummer) { + return `https://ws.geonorge.no/kommuneinfo/v1/kommuner/${kommunenummer}`; + } + + if (fylkesnummer) { + return `https://ws.geonorge.no/kommuneinfo/v1/fylker/${fylkesnummer}`; + } + + // Else return the whole country + return null + } - if(!result || !result.navn || result.navn.length === 0) - return; - const place = result.navn[0] - this.moveMapToGeoNorgeLocation(place) + moveMapToGeoNorgeLocation(result) { + + const avgrensningsboks = result?.avgrensningsboks?.coordinates?.[0] + if (!avgrensningsboks) { + return this.moveMapToFitNorway(); + } + + return this.moveMapToFitBounds(this.geoNorgeAvgrensingsBoksToBoundingBox(avgrensningsboks)); + } + + geoNorgeAvgrensingsBoksToBoundingBox = (avgrensningsboks) => { + const [lng1, lat1] = avgrensningsboks[0]; + const [lng2, lat2] = avgrensningsboks[2]; + return [lng1, lat1, lng2, lat2]; + } + + moveMapToFitNorway() { + return this.moveMapToFitBounds([4.032154,57.628953,31.497974,71.269784]) + } + moveMapToFitBounds(bounds) { + console.log(bounds) + this.map.fitBounds(bounds, { + padding: 10, + animate: false + }, { + wasZoom: true, + }); } removeMarker(key) { @@ -374,40 +421,6 @@ export default class extends Controller { }); } - moveMapToGeoNorgeLocation(place) { - const location = place.geojson.geometry; - - if (location.type === "Point") { - this.moveMapTo(location.coordinates) - } - - if (location.type === "MultiPoint") { - // MultiPoints can have arbitrary amounts of points, and so needs to be fit - // as described here: https://docs.mapbox.com/mapbox-gl-js/example/zoomto-linestring/ - const bounds = new mapboxgl.LngLatBounds( - location.coordinates[0], - location.coordinates[0] - ); - for (const coord of location.coordinates) { - bounds.extend(coord); - } - return this.map.fitBounds(bounds, { - padding: 100, - animate: false, - maxZoom: 13 - }, { - wasZoom: true, - }); - } - - // Unsure about location type given, use the center given by representasjonspunkt instead - const point = [ - place.representasjonspunkt.øst, - place.representasjonspunkt.nord - ]; - return this.moveMapTo(point); - } - moveMapTo(point) { this.map.jumpTo({ center: point, zoom: 12 }, { wasZoom: true }); } diff --git a/config/locales/nb.yml b/config/locales/nb.yml index bb798c72..54ec3169 100644 --- a/config/locales/nb.yml +++ b/config/locales/nb.yml @@ -84,9 +84,7 @@ nb: other: "%{count} lokaler passer:" edit_filter: 'Rediger filtre' location: 'Område' - municipality: 'Kommune' - choose_municipality: 'Velg kommune' - or_choose_by_map: 'Eller flytt på kart' + choose_location: 'Søk her, eller flytt på kart' title: 'Navn på lokalet' more_filters: 'Flere filtre' map_reload: 'Søk i området' From 5b9ad113c3431b1f6006b1cba155255d4cecf4e3 Mon Sep 17 00:00:00 2001 From: Daniel Jackson Date: Thu, 29 Feb 2024 10:12:36 +0100 Subject: [PATCH 05/11] Style the little pill for reloading the map, so it's clearer that it exists --- app/views/spaces/index/_map.html.erb | 13 ++++++++++++- tailwind.config.js | 11 +++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app/views/spaces/index/_map.html.erb b/app/views/spaces/index/_map.html.erb index a581527f..7e6a512e 100644 --- a/app/views/spaces/index/_map.html.erb +++ b/app/views/spaces/index/_map.html.erb @@ -5,7 +5,18 @@
- <%= f.label t("space_filter.choose_municipality"), for: "locationInput", class: "text-xl font-bold" %> + <%= f.label t("space_filter.location"), for: "locationInput", class: "text-xl font-bold" %>
-

<%= t("space_filter.or_choose_by_map") %>

From 5df7969165d76cac9eb635635ac6ba46e4854d75 Mon Sep 17 00:00:00 2001 From: Daniel Jackson Date: Thu, 29 Feb 2024 10:33:23 +0100 Subject: [PATCH 07/11] Fix brittle test. Sometimes there are two fieldsets, sometimes not - depends on the data. --- spec/support/user_creates_facility_reviews_helpers.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/support/user_creates_facility_reviews_helpers.rb b/spec/support/user_creates_facility_reviews_helpers.rb index 0c440cff..8fa90ae9 100644 --- a/spec/support/user_creates_facility_reviews_helpers.rb +++ b/spec/support/user_creates_facility_reviews_helpers.rb @@ -99,7 +99,7 @@ def save_form_modal end def enter_data_about_facility(facility:, description_to_add: "") - fieldset = find("fieldset", text: facility.title) + fieldset = first("fieldset", text: facility.title) within(fieldset) do click_on("Rediger") choose(I18n.t("reviews.form.facility_experience_particular_tense.was_allowed"), allow_label_click: true) From 8e775d0ad76eedda0d8e5b5804e0fb34a0f2cce7 Mon Sep 17 00:00:00 2001 From: Daniel Jackson Date: Thu, 29 Feb 2024 10:53:08 +0100 Subject: [PATCH 08/11] Fix bug where stars given was not output correctly --- app/views/reviews/_review_content.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/reviews/_review_content.html.erb b/app/views/reviews/_review_content.html.erb index 49a039d9..2fc44249 100644 --- a/app/views/reviews/_review_content.html.erb +++ b/app/views/reviews/_review_content.html.erb @@ -4,7 +4,7 @@ <%= inline_svg_tag 'star', class: 'text-pink-600' if review.star_rating %> <%= review.star_rating %> - <%= t("reviews.stars_given", count: review.star_rating) %> + <%= t("reviews.stars_given", count: review.star_rating.to_i) %> <% unless review.star_rating %> - <% if review.type_of_contact %> From 399286a35f09208a1830fa3bbda1260547ed32d0 Mon Sep 17 00:00:00 2001 From: Daniel Jackson Date: Thu, 29 Feb 2024 10:57:30 +0100 Subject: [PATCH 09/11] Fix bug where sr-only caused reviews to overflow. They need to be inside a relative container, for some reason. See https://github.com/tailwindlabs/tailwindcss/discussions/12429#discussioncomment-7587178 --- app/views/reviews/_review_content.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/reviews/_review_content.html.erb b/app/views/reviews/_review_content.html.erb index 2fc44249..92c1d198 100644 --- a/app/views/reviews/_review_content.html.erb +++ b/app/views/reviews/_review_content.html.erb @@ -1,7 +1,7 @@ <% show_full_review = false unless defined?(:show_full_review) %>

- + <%= inline_svg_tag 'star', class: 'text-pink-600' if review.star_rating %> <%= review.star_rating %> <%= t("reviews.stars_given", count: review.star_rating.to_i) %> From f8b4a84d64bc07b2c2e3cb194f0b843652f1bcd8 Mon Sep 17 00:00:00 2001 From: Daniel Jackson Date: Thu, 29 Feb 2024 11:03:59 +0100 Subject: [PATCH 10/11] Fix brittle test. have_text does not wait for JS to run. Have_content does. It mostly worked with have_text, but not always --- spec/features/user_manages_space_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/features/user_manages_space_spec.rb b/spec/features/user_manages_space_spec.rb index bdf7af72..959557e3 100644 --- a/spec/features/user_manages_space_spec.rb +++ b/spec/features/user_manages_space_spec.rb @@ -27,14 +27,14 @@ fill_in "space_post_number", with: space.post_number find("body").click # Blur from field - expect(page).to have_text(I18n.t("space_create.any_of_these.one")) + expect(page).to have_content(I18n.t("space_create.any_of_these.one")) click_on I18n.t("space_create.none_are_duplicates.one") tom_select("select#space_space_type_ids", option_id: space_type.id) tom_select("select#space_space_group_title", option_id: space_group.title) click_on I18n.t("helpers.submit.create", model: Space.model_name.human) - expect(page).to have_text(space.title) + expect(page).to have_content(space.title) end end From 68a218a249ac00b83566ab4cc61f450a09d36bea Mon Sep 17 00:00:00 2001 From: Daniel Jackson Date: Thu, 29 Feb 2024 11:42:47 +0100 Subject: [PATCH 11/11] Fix bug where the duplicate checker at space#new at times would not work after adding title to duplicate check --- .../duplicate_checker_controller.js | 54 +++++++++++++------ spec/features/user_manages_space_spec.rb | 4 +- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/app/javascript/controllers/duplicate_checker_controller.js b/app/javascript/controllers/duplicate_checker_controller.js index 5d54d670..c762474f 100644 --- a/app/javascript/controllers/duplicate_checker_controller.js +++ b/app/javascript/controllers/duplicate_checker_controller.js @@ -25,39 +25,51 @@ export default class extends Controller { connect() { const form = this.element this.hideRestOfForm() + form.addEventListener('change', () => { this.checkDuplicates() }); const titleField = form.querySelector("#space_title") titleField.addEventListener('input', (e) => { - if (e.target.value.length < 5) { - return - } - this.checkDuplicates() }) } async checkDuplicates() { - const data = new FormData(this.element); - const title = data.get("space[title]"); - const address = data.get("space[address]"); - const post_number = data.get("space[post_number]"); + const { + title, + address, + post_number + } = this.extractDataFromForm() - const enoughDataToFindByTitle = title.length > 5 - const enoughDataToFindByAddress = (!post_number || (!address && !post_number)) + if (this.checkIfDataIsStale(title, address, post_number)) { + return // No need to check anything if we have the same data + } - if (!enoughDataToFindByTitle || !enoughDataToFindByAddress) return + const enoughDataToFindByTitle = title.length > 5 + const enoughDataToFindByAddress = (!!address && !!post_number) - if (this.checkIfDataIsStale(title, address, post_number)) { - return + if (!enoughDataToFindByTitle && !enoughDataToFindByAddress) { + return this.renderNoDuplicates() } + return await this.renderFetchedDuplicates(title, address, post_number) + } + + extractDataFromForm() { + const data = new FormData(this.element); + const title = data.get("space[title]"); + const address = data.get("space[address]"); + const post_number = data.get("space[post_number]"); + return { title, address, post_number } + } + + async renderFetchedDuplicates(title, address, post_number) { let url = "/check_duplicates?" - url += `title=${title}&` - url += `address=${address}&` - url += `post_number=${post_number}&` + url += `title=${title}&` + url += `address=${address}&` + url += `post_number=${post_number}&` const result = await (await fetch(url)).json() if (result && result.html) { @@ -68,8 +80,16 @@ export default class extends Controller { return this.showRestOfForm() } + renderNoDuplicates() { + return this.duplicatesRenderHereTarget.innerHTML = "" + } + + checkIfDataIsStale(title, address, post_number) { - if (title !== this.state.title || address !== this.state.address || post_number !== this.state.post_number) { + if ( + title !== this.state.title || + address !== this.state.address || + post_number !== this.state.post_number) { this.state.title = title this.state.address = address this.state.post_number = post_number diff --git a/spec/features/user_manages_space_spec.rb b/spec/features/user_manages_space_spec.rb index 959557e3..f0626b15 100644 --- a/spec/features/user_manages_space_spec.rb +++ b/spec/features/user_manages_space_spec.rb @@ -27,14 +27,14 @@ fill_in "space_post_number", with: space.post_number find("body").click # Blur from field - expect(page).to have_content(I18n.t("space_create.any_of_these.one")) + expect(page).to have_content(I18n.t("space_create.any_of_these.one"), wait: 10) click_on I18n.t("space_create.none_are_duplicates.one") tom_select("select#space_space_type_ids", option_id: space_type.id) tom_select("select#space_space_group_title", option_id: space_group.title) click_on I18n.t("helpers.submit.create", model: Space.model_name.human) - expect(page).to have_content(space.title) + expect(page).to have_text(space.title) end end