diff --git a/app/controllers/champs/carte_controller.rb b/app/controllers/champs/carte_controller.rb index 64f694c0c..4dc16440b 100644 --- a/app/controllers/champs/carte_controller.rb +++ b/app/controllers/champs/carte_controller.rb @@ -26,11 +26,11 @@ class Champs::CarteController < ApplicationController end geo_areas = [] - geo_json = JSON.parse(geo_json) + geo_json = geo_json.blank? ? [] : JSON.parse(geo_json) if geo_json.first == ["error", "TooManyPolygons"] @error = true - else + elsif geo_json.present? 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 5528cc1be..f56c859dc 100644 --- a/app/javascript/shared/carte.js +++ b/app/javascript/shared/carte.js @@ -97,7 +97,9 @@ export function getCurrentMap(input) { export function addFreeDrawEvents(map, selector) { const input = findInput(selector); map.freeDraw.on('markers', ({ latLngs }) => { - if (polygonArea(latLngs) < 300000) { + if (latLngs.length === 0) { + input.value = ''; + } else if (polygonArea(latLngs) < 300000) { input.value = JSON.stringify(latLngs); } else { input.value = '{ "error": "TooManyPolygons" }'; diff --git a/app/models/champ.rb b/app/models/champ.rb index 99bd1e2c0..94123a53e 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -17,7 +17,16 @@ class Champ < ApplicationRecord end def mandatory_and_blank? - mandatory? && value.blank? + if mandatory? + case type_de_champ.type_champ + when TypeDeChamp.type_champs.fetch(:carte) + value.blank? || value == '[]' + else + value.blank? + end + else + false + end end def search_terms diff --git a/spec/models/champ_shared_example.rb b/spec/models/champ_shared_example.rb index 91eeb2438..31ae546fa 100644 --- a/spec/models/champ_shared_example.rb +++ b/spec/models/champ_shared_example.rb @@ -9,6 +9,12 @@ shared_examples 'champ_spec' do it { expect(champ.mandatory_and_blank?).to be(true) } end + context 'when carte mandatory and blank' do + let(:type_de_champ) { build(:type_de_champ_carte, mandatory: mandatory) } + let(:value) { '[]' } + it { expect(champ.mandatory_and_blank?).to be(true) } + end + context 'when not blank' do let(:value) { 'yop' } it { expect(champ.mandatory_and_blank?).to be(false) }