Skip to content

Commit

Permalink
Add answer option modications / Fix json validation for location form…
Browse files Browse the repository at this point in the history
… in case of empty geojson
  • Loading branch information
JoonasAapro committed Mar 21, 2024
1 parent 2775a37 commit a96bc95
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 12 deletions.
6 changes: 3 additions & 3 deletions app/cells/decidim/locations/map_cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ def markers_center

def markers_data_for_map
format_map_locations(model).map do |data|
if data.instance_of?(Decidim::Forms::LocationOption)
if data.instance_of?(Decidim::Forms::AnswerOption)
{
location: data.title,
geojson: JSON.parse(data.body)
location: data.body,
geojson: JSON.parse(data.geojson)
}
else
body = data[2]
Expand Down
13 changes: 13 additions & 0 deletions app/forms/decidim/locations/location_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ def to_param
private

def json_validation
if geojson.blank?
geojson = {
type: "Feature",
geometry: {
type: shape,
coordinates:
[latitude, longitude]
}
}

return
end

# check if GeoJSON is valid
begin
geo_factory = RGeo::Geographic.spherical_factory
Expand Down
4 changes: 3 additions & 1 deletion app/packs/src/decidim/locations/map/controller/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ export default class ModelLocMapController extends MapController {
// Position the center of the map
const lat = mapEl.dataset.lat;
const lng = mapEl.dataset.lng;
const selectLocation = mapEl.dataset.selectLocation;

let defaultLat = 0;
let defaultLng = 0;
let zoom = 0;

if (lat !== defaultLat.toFixed(1) || lng !== defaultLng.toFixed(1)) {
if (selectLocation === "false" && (lat !== defaultLat.toFixed(1) || lng !== defaultLng.toFixed(1))) {
console.log("aASD")
defaultLat = lat;
defaultLng = lng;
zoom = 14;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const addInputGroup = function (shapeFieldContainer, addressData, wrapperEl) {
const coordinates = JSON.stringify(addressData.coordinates);
const shapeField = shapeFieldContainer.querySelector(`[data-shape-id="${shapeId}"]`);

if (parentNodeId === "location-option-selector") {
if (parentNodeId === "answer-option-map-selector") {
const currentGeo = document.querySelector("button.location-selector").parentNode.querySelector("label > textarea");

currentGeo.value = JSON.stringify(buildGeoJson(coordinates, objectShape, address));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const coordinatesToObject = function (coordinates, shape) {
}

const buildGeoJson = function (coordinates, shape, address) {

console.log(address)
if (address) {
return {
"type": "Feature",
Expand Down
7 changes: 4 additions & 3 deletions app/packs/src/decidim/map/controller/markers.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default class MapMarkersController extends MapController {
const bounds = new L.LatLngBounds(
markersData.map(
(markerData) => {
if (markerData.geojson.type) {
if (markerData.location) {
return markerData.geojson.geometry.coordinates
}

Expand All @@ -70,7 +70,7 @@ export default class MapMarkersController extends MapController {
markersData.forEach((markerData) => {
let shape = {}

if (markerData.geojson.type) {
if (markerData.location) {
const coordinates = markerData.geojson.geometry.coordinates;
const location = markerData.location;
const objectShape = markerData.geojson.geometry.type;
Expand All @@ -95,7 +95,7 @@ export default class MapMarkersController extends MapController {
shape: objectShape})
}

shape.bindTooltip(location, {permanent: true, interactive: true});
shape.bindTooltip(location.en, {permanent: true, interactive: true});

this.markerClusters.addLayer(shape);
} else {
Expand Down Expand Up @@ -141,6 +141,7 @@ export default class MapMarkersController extends MapController {
// mobile). Make sure there is at least the same amount of width and
// height available on both sides + the padding (i.e. 4x padding in
// total).

if (this.selectLocation()) {
this.map.fitBounds(bounds);
} else {
Expand Down
5 changes: 2 additions & 3 deletions spec/forms/decidim/locations/location_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,11 @@
address: "",
latitude: 50.149792,
longitude: 24.887430,
shape: "Point",
geojson: ""
shape: "Point"
}
end

it { is_expected.not_to be_valid }
it { is_expected.to be_valid }
end

context "when geojson is not correct" do
Expand Down

0 comments on commit a96bc95

Please sign in to comment.