From 1bb70effa5122984aa22ad386a21b4ba8e847e17 Mon Sep 17 00:00:00 2001
From: Jean-Baptiste Pasquier <contact@jbpasquier.eu>
Date: Wed, 14 Apr 2021 00:17:03 +0200
Subject: [PATCH] minor: community addresses implementation with Nominatim

---
 src/locales/en.json                           |  3 +-
 src/locales/es.json                           |  3 +-
 src/locales/fr.json                           |  3 +-
 src/scripts/hubl-geocoord.js                  | 33 +++++++++++++++++++
 .../communities/page-community-edit.pug       | 11 ++++---
 src/views/partials/widgets.pug                |  1 +
 .../hubl-communities-edit-add-button.pug      | 11 +++++++
 7 files changed, 58 insertions(+), 7 deletions(-)
 create mode 100644 src/scripts/hubl-geocoord.js
 create mode 100644 src/views/partials/widgets/hubl-communities-edit-add-button.pug

diff --git a/src/locales/en.json b/src/locales/en.json
index f5a5e7cd..955cc4fc 100644
--- a/src/locales/en.json
+++ b/src/locales/en.json
@@ -183,7 +183,8 @@
       "noPermission": "You don't have access to this page",
       "tableHeader1": "Address line 1",
       "tableHeader2": "Address line 2",
-      "buttonDelete": "Delete"
+      "buttonDelete": "Delete",
+      "buttonAdd": "Add"
     }
   },
   "project": {
diff --git a/src/locales/es.json b/src/locales/es.json
index c61ea85a..9ad1628b 100644
--- a/src/locales/es.json
+++ b/src/locales/es.json
@@ -183,7 +183,8 @@
       "noPermission": "No tiene permiso",
       "tableHeader1": "Dirección línea 1",
       "tableHeader2": "Dirección línea 2",
-      "buttonDelete": "Eliminar"
+      "buttonDelete": "Eliminar",
+      "buttonAdd": "Añadir"
     }
   },
   "project": {
diff --git a/src/locales/fr.json b/src/locales/fr.json
index 433cbf35..3cb4ba47 100644
--- a/src/locales/fr.json
+++ b/src/locales/fr.json
@@ -181,7 +181,8 @@
       "noPermission": "Vous n'avez pas la permission",
       "tableHeader1": "Adresse ligne 1",
       "tableHeader2": "Adresse ligne 2",
-      "buttonDelete": "Effacer"
+      "buttonDelete": "Effacer",
+      "buttonAdd": "Ajouter"
     }
   },
   "project": {
diff --git a/src/scripts/hubl-geocoord.js b/src/scripts/hubl-geocoord.js
new file mode 100644
index 00000000..1f4d3de2
--- /dev/null
+++ b/src/scripts/hubl-geocoord.js
@@ -0,0 +1,33 @@
+/*
+  Geocoord helper using Nominatim
+  Usage:
+  const madrid = await hubl.geocoord('Madrid');
+  madrid == ["-3.7035825", "40.4167047"]
+*/
+window.hubl.geocoord = async (address = false) => {
+  if (address) {
+    const nominatim = await fetch('https://nominatim.openstreetmap.org/?format=geocodejson&limit=1&q=' + encodeURI(address));
+    const response = await nominatim.json();
+    if (response.features[0]) {
+      const coords = response.features[0].geometry.coordinates;
+      if (coords[0] && coords[1]) {
+        return [String(coords[0]), String(coords[1])];
+      }
+    } else {
+      console.error("Address not found");
+    }
+  } else {
+    console.error("Missing address");
+  }
+  return ["-47.15", "-123.716667"];
+}
+
+window.hubl.geocalc = (element) => {
+  const editionForm = element.parentElement.parentElement.parentElement.parentElement;
+  window.hubl.geocoord(editionForm.querySelector('input[name="address_line1"]').value + " " + editionForm.querySelector('input[name="address_line2"]').value).then(coords => {
+    editionForm.querySelector('input[name="lat"]').value = coords[0];
+    editionForm.querySelector('input[name="lng"]').value = coords[1];
+    editionForm.querySelector('input[type="submit"]').click();
+  });
+  return false;
+}
\ No newline at end of file
diff --git a/src/views/partials/communities/page-community-edit.pug b/src/views/partials/communities/page-community-edit.pug
index 3ac610b4..d89dc66c 100644
--- a/src/views/partials/communities/page-community-edit.pug
+++ b/src/views/partials/communities/page-community-edit.pug
@@ -133,7 +133,7 @@ div.bg-color-white
           solid-form.form.table-body.edit-address(
             bind-resources
             nested-field="addresses"
-            fields="segment1(address_line1), segment2(address_line2), segment3(deleteButton)"
+            fields="segment1(address_line1), segment2(address_line2), lat, lng, segment3(addButton)"
 
             placeholder-address_line1=""
             placeholder-address_line2=""
@@ -145,12 +145,15 @@ div.bg-color-white
             class-address_line1="segment full text-small"
             class-address_line2="segment full text-small"
 
+            class-lat="hidden"
+            class-lng="hidden"
+
             widget-community="solid-form-hidden"
             widget-address_line1="solid-form-placeholder-text"
             widget-address_line2="solid-form-placeholder-text"
-
-            action-deleteButton=""
-            widget-deleteButton=`hubl-communities-edit-delete-button`
+            widget-lat="solid-form-hidden"
+            widget-lng="solid-form-hidden"
+            widget-addButton=`hubl-communities-edit-add-button`
 
             data-trans="placeholder-address_line1=communities.edit.labelAddressLine1;placeholder-address_line2=communities.edit.labelAddressLine2"     
           )
diff --git a/src/views/partials/widgets.pug b/src/views/partials/widgets.pug
index 7a479284..ee38e845 100644
--- a/src/views/partials/widgets.pug
+++ b/src/views/partials/widgets.pug
@@ -22,6 +22,7 @@ include widgets/hubl-circle-team-contact.pug
 include widgets/hubl-circle-user-admin.pug
 include widgets/hubl-communities-counter-alternate.pug
 include widgets/hubl-communities-edit-button.pug
+include widgets/hubl-communities-edit-add-button.pug
 include widgets/hubl-communities-edit-email.pug
 include widgets/hubl-communities-edit-website.pug
 include widgets/hubl-communities-logo.pug
diff --git a/src/views/partials/widgets/hubl-communities-edit-add-button.pug b/src/views/partials/widgets/hubl-communities-edit-add-button.pug
new file mode 100644
index 00000000..85b78589
--- /dev/null
+++ b/src/views/partials/widgets/hubl-communities-edit-add-button.pug
@@ -0,0 +1,11 @@
+if componentSet.has('communities') && getRoute('communities')
+  solid-widget(name='hubl-communities-edit-add-button')
+    template
+      div(
+        class="segment text-xsmall children-link-button children-link-text-bold children-link-text-uppercase children-link-color-secondary bordered"
+      )
+        button.form-sub(
+          data-trans="communities.edit.buttonAdd"
+          type="button"
+          onclick="window.hubl.geocalc(this);"
+        )
\ No newline at end of file
-- 
GitLab