diff --git a/app/controllers/champs/carte_controller.rb b/app/controllers/champs/carte_controller.rb index 9746896a5..3609272fd 100644 --- a/app/controllers/champs/carte_controller.rb +++ b/app/controllers/champs/carte_controller.rb @@ -1,6 +1,9 @@ class Champs::CarteController < ApplicationController before_action :authenticate_logged_user! + EMPTY_GEO_JSON = '[]' + ERROR_GEO_JSON = '' + def show @selector = ".carte-#{params[:position]}" @@ -26,13 +29,17 @@ class Champs::CarteController < ApplicationController end geo_areas = [] - geo_json = geo_json.blank? ? [] : JSON.parse(geo_json) - if geo_json.empty? + if geo_json == EMPTY_GEO_JSON + @champ.value = nil + @champ.geo_areas = [] + elsif geo_json == ERROR_GEO_JSON @error = true @champ.value = nil @champ.geo_areas = [] - elsif geo_json.present? + else + geo_json = JSON.parse(geo_json) + if @champ.cadastres? cadastres = ModuleApiCartoService.generate_cadastre(geo_json) geo_areas += cadastres.map do |cadastre| diff --git a/app/javascript/shared/carte.js b/app/javascript/shared/carte.js index 1c7d7655e..a9360e829 100644 --- a/app/javascript/shared/carte.js +++ b/app/javascript/shared/carte.js @@ -107,15 +107,18 @@ export function getCurrentMap(input) { } } +const EMPTY_GEO_JSON = '[]'; +const ERROR_GEO_JSON = ''; + export function addFreeDrawEvents(map, selector) { const input = findInput(selector); map.freeDraw.on('markers', ({ latLngs }) => { if (latLngs.length === 0) { - input.value = ''; + input.value = EMPTY_GEO_JSON; } else if (polygonArea(latLngs) < 300000) { input.value = JSON.stringify(latLngs); } else { - input.value = ''; + input.value = ERROR_GEO_JSON; } fire(input, 'change'); diff --git a/spec/controllers/champs/carte_controller_spec.rb b/spec/controllers/champs/carte_controller_spec.rb index 9bd6c7627..6fbbd88e6 100644 --- a/spec/controllers/champs/carte_controller_spec.rb +++ b/spec/controllers/champs/carte_controller_spec.rb @@ -8,7 +8,7 @@ describe Champs::CarteController, type: :controller do { dossier: { champs_attributes: { - '1' => { value: selection.to_json } + '1' => { value: value } } }, position: '1', @@ -35,13 +35,18 @@ describe Champs::CarteController, type: :controller do end context 'when coordinates are empty' do - let(:selection) { [] } + let(:value) { '[]' } - it { expect(response.body).to include("DS.drawMapData(\".carte-1\", {\"position\":{\"lon\":\"2.428462\",\"lat\":\"46.538192\",\"zoom\":\"13\"},\"selection\":[],\"quartiersPrioritaires\":[],\"cadastres\":[],\"parcellesAgricoles\":[]});") } + it { + expect(assigns(:error)).to eq(nil) + expect(champ.reload.value).to eq(nil) + expect(champ.reload.geo_areas).to eq([]) + expect(response.body).to include("DS.drawMapData(\".carte-1\", {\"position\":{\"lon\":\"2.428462\",\"lat\":\"46.538192\",\"zoom\":\"13\"},\"selection\":[],\"quartiersPrioritaires\":[],\"cadastres\":[],\"parcellesAgricoles\":[]});") + } end context 'when coordinates are informed' do - let(:selection) { [[{ "lat": 48.87442541960633, "lng": 2.3859214782714844 }, { "lat": 48.87273183590832, "lng": 2.3850631713867183 }, { "lat": 48.87081237174292, "lng": 2.3809432983398438 }, { "lat": 48.8712640169951, "lng": 2.377510070800781 }, { "lat": 48.87510283703279, "lng": 2.3778533935546875 }, { "lat": 48.87544154230615, "lng": 2.382831573486328 }, { "lat": 48.87442541960633, "lng": 2.3859214782714844 }]] } + let(:value) { [[{ "lat": 48.87442541960633, "lng": 2.3859214782714844 }, { "lat": 48.87273183590832, "lng": 2.3850631713867183 }, { "lat": 48.87081237174292, "lng": 2.3809432983398438 }, { "lat": 48.8712640169951, "lng": 2.377510070800781 }, { "lat": 48.87510283703279, "lng": 2.3778533935546875 }, { "lat": 48.87544154230615, "lng": 2.382831573486328 }, { "lat": 48.87442541960633, "lng": 2.3859214782714844 }]].to_json } it { expect(response.body).not_to be_nil } it { expect(response.body).to include('MultiPolygon') } @@ -50,9 +55,10 @@ describe Champs::CarteController, type: :controller do context 'when error' do let(:geojson) { [[{ "lat": 48.87442541960633, "lng": 2.3859214782714844 }, { "lat": 48.87273183590832, "lng": 2.3850631713867183 }, { "lat": 48.87081237174292, "lng": 2.3809432983398438 }, { "lat": 48.8712640169951, "lng": 2.377510070800781 }, { "lat": 48.87510283703279, "lng": 2.3778533935546875 }, { "lat": 48.87544154230615, "lng": 2.382831573486328 }, { "lat": 48.87442541960633, "lng": 2.3859214782714844 }]] } - let(:selection) { '' } + let(:value) { '' } it { + expect(assigns(:error)).to eq(true) expect(champ.reload.value).to eq(nil) expect(champ.reload.geo_areas).to eq([]) }