From f563e210f37adc22d0487659806ddef1a172b8ea Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Fri, 9 Jun 2023 16:29:42 +0200 Subject: [PATCH] refactor(carto): geo area as component --- .../dossiers/geo_area_component.html.haml} | 2 +- app/components/dossiers/geo_area_component.rb | 7 +++++++ .../dossiers/geo_areas_component.html.haml} | 4 ++-- app/components/dossiers/geo_areas_component.rb | 7 +++++++ .../carte_component/carte_component.html.haml | 2 +- app/views/champs/carte/index.turbo_stream.haml | 3 ++- app/views/shared/champs/carte/_show.html.haml | 2 +- 7 files changed, 21 insertions(+), 6 deletions(-) rename app/{views/shared/champs/carte/_geo_area.html.haml => components/dossiers/geo_area_component.html.haml} (91%) create mode 100644 app/components/dossiers/geo_area_component.rb rename app/{views/shared/champs/carte/_geo_areas.html.haml => components/dossiers/geo_areas_component.html.haml} (68%) create mode 100644 app/components/dossiers/geo_areas_component.rb diff --git a/app/views/shared/champs/carte/_geo_area.html.haml b/app/components/dossiers/geo_area_component.html.haml similarity index 91% rename from app/views/shared/champs/carte/_geo_area.html.haml rename to app/components/dossiers/geo_area_component.html.haml index bfab2f8ae..99e6f8aef 100644 --- a/app/views/shared/champs/carte/_geo_area.html.haml +++ b/app/components/dossiers/geo_area_component.html.haml @@ -2,7 +2,7 @@ - if editing = link_to '#', data: { action: 'geo-area#onClick' } do = geo_area.label - = text_field_tag :description, geo_area.description, data: { action: 'focus->geo-area#onFocus input->geo-area#onInput', geo_area_target: 'description' }, placeholder: 'Description', class: 'no-margin' + = text_field_tag :description, geo_area.description, data: { action: 'focus->geo-area#onFocus input->geo-area#onInput', geo_area_target: 'description' }, placeholder: 'Description', class: 'no-margin', id: "geo_area_#{geo_area.id}_description" - else = link_to '#', data: { action: 'geo-area#onClick' } do = geo_area.label diff --git a/app/components/dossiers/geo_area_component.rb b/app/components/dossiers/geo_area_component.rb new file mode 100644 index 000000000..6e004d635 --- /dev/null +++ b/app/components/dossiers/geo_area_component.rb @@ -0,0 +1,7 @@ +class Dossiers::GeoAreaComponent < ApplicationComponent + attr_reader :geo_area, :editing + + def initialize(geo_area:, editing:) + @geo_area, @editing = geo_area, editing + end +end diff --git a/app/views/shared/champs/carte/_geo_areas.html.haml b/app/components/dossiers/geo_areas_component.html.haml similarity index 68% rename from app/views/shared/champs/carte/_geo_areas.html.haml rename to app/components/dossiers/geo_areas_component.html.haml index d9de25f33..52f8168f4 100644 --- a/app/views/shared/champs/carte/_geo_areas.html.haml +++ b/app/components/dossiers/geo_areas_component.html.haml @@ -3,7 +3,7 @@ .areas %ul - champ.selections_utilisateur.slice(0, 20).each do |geo_area| - = render partial: 'shared/champs/carte/geo_area', locals: { geo_area: geo_area, editing: editing } + = render Dossiers::GeoAreaComponent.new(geo_area:, editing:) - if champ.selections_utilisateur.size > 20 %li %span @@ -14,4 +14,4 @@ .areas %ul - champ.cadastres.each do |geo_area| - = render partial: 'shared/champs/carte/geo_area', locals: { geo_area: geo_area, editing: editing } + = render Dossiers::GeoAreaComponent.new(geo_area:, editing:) diff --git a/app/components/dossiers/geo_areas_component.rb b/app/components/dossiers/geo_areas_component.rb new file mode 100644 index 000000000..51e0b93ab --- /dev/null +++ b/app/components/dossiers/geo_areas_component.rb @@ -0,0 +1,7 @@ +class Dossiers::GeoAreasComponent < ApplicationComponent + attr_reader :champ, :editing + + def initialize(champ:, editing:) + @champ, @editing = champ, editing + end +end diff --git a/app/components/editable_champ/carte_component/carte_component.html.haml b/app/components/editable_champ/carte_component/carte_component.html.haml index ce5df6c7e..f217fc9e9 100644 --- a/app/components/editable_champ/carte_component/carte_component.html.haml +++ b/app/components/editable_champ/carte_component/carte_component.html.haml @@ -8,4 +8,4 @@ autocompleteScreenReaderInstructions: t("combo_search_component.screen_reader_instructions")) .geo-areas{ id: dom_id(@champ, :geo_areas) } - = render partial: 'shared/champs/carte/geo_areas', locals: { champ: @champ, editing: true } + = render Dossiers::GeoAreasComponent.new(champ: @champ, editing: true) diff --git a/app/views/champs/carte/index.turbo_stream.haml b/app/views/champs/carte/index.turbo_stream.haml index ebbdd946f..55694596a 100644 --- a/app/views/champs/carte/index.turbo_stream.haml +++ b/app/views/champs/carte/index.turbo_stream.haml @@ -1,4 +1,5 @@ -= turbo_stream.update dom_id(@champ, :geo_areas), partial: 'shared/champs/carte/geo_areas', locals: { champ: @champ, editing: true } += turbo_stream.update dom_id(@champ, :geo_areas) do + = render Dossiers::GeoAreasComponent.new(champ: @champ, editing: true) - if @focus = turbo_stream.dispatch 'map:feature:focus', bbox: @champ.bounding_box diff --git a/app/views/shared/champs/carte/_show.html.haml b/app/views/shared/champs/carte/_show.html.haml index a4fbda3eb..4b957d132 100644 --- a/app/views/shared/champs/carte/_show.html.haml +++ b/app/views/shared/champs/carte/_show.html.haml @@ -1,4 +1,4 @@ - if champ.geometry? = react_component("MapReader", { featureCollection: champ.to_feature_collection, options: champ.render_options } ) .geo-areas - = render partial: 'shared/champs/carte/geo_areas', locals: { champ: champ, editing: false } + = render Dossiers::GeoAreasComponent.new(champ:, editing: false)