From 2c3ef9553b0fe8e0fb58a8425f6606a0865ac754 Mon Sep 17 00:00:00 2001
From: Friedjoff Trautwein <friedjoff.trautwein@geops.de>
Date: Fri, 6 Mar 2020 15:30:13 +0100
Subject: [PATCH] feat: run locate function on location changes

---
 src/store/actions.js              |  5 +++++
 src/store/enhancers/projection.js | 10 +++++++++-
 src/store/reducers.js             | 12 ++++++++----
 3 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/src/store/actions.js b/src/store/actions.js
index 7ca7c847..dcca2203 100644
--- a/src/store/actions.js
+++ b/src/store/actions.js
@@ -1,4 +1,5 @@
 export const SET_FORM_LOCATION = 'SET_FORM_LOCATION';
+export const SET_LOCATE_RESULT = 'SET_LOCATE_RESULT';
 export const SET_LOCATION = 'SET_LOCATION';
 export const SET_MAP_LAYER = 'SET_MAP_LAYER';
 export const SET_MAP_LOCATION = 'SET_MAP_LOCATION';
@@ -12,6 +13,10 @@ export function setFormLocation(formLocation) {
   return { type: SET_FORM_LOCATION, formLocation };
 }
 
+export function setLocateResult(locateResult) {
+  return { type: SET_LOCATE_RESULT, locateResult };
+}
+
 export function setLocation(location) {
   return { type: SET_LOCATION, location };
 }
diff --git a/src/store/enhancers/projection.js b/src/store/enhancers/projection.js
index 1e9232c0..0f7d1322 100644
--- a/src/store/enhancers/projection.js
+++ b/src/store/enhancers/projection.js
@@ -1,5 +1,5 @@
 /* eslint-disable no-console */
-import { project } from '@geops/tree-lib';
+import { locate, project } from '@geops/tree-lib';
 import { applyMiddleware } from 'redux';
 
 import {
@@ -7,6 +7,7 @@ import {
   SET_MAP_LOCATION,
   SET_PROJECTION_MODE,
   setLocation,
+  setLocateResult,
   setProjectionResult,
   setTargetAltitudinalZone,
 } from '../actions';
@@ -35,6 +36,13 @@ const projection = store => next => action => {
         ? mapLocation.targetAltitudinalZoneExtreme
         : formLocation.targetAltitudinalZone;
     store.dispatch(setTargetAltitudinalZone(targetAltitudinalZone));
+    try {
+      const locateResult = locate(location);
+      console.log(locateResult, location);
+      store.dispatch(setLocateResult(locateResult));
+    } catch (error) {
+      console.log('Locate error: ', error);
+    }
     try {
       const projectionResult = project(location, targetAltitudinalZone);
       store.dispatch(setProjectionResult(projectionResult));
diff --git a/src/store/reducers.js b/src/store/reducers.js
index af729a7d..c1fc8ea2 100644
--- a/src/store/reducers.js
+++ b/src/store/reducers.js
@@ -1,6 +1,7 @@
 import {
-  SET_LOCATION,
   SET_FORM_LOCATION,
+  SET_LOCATE_RESULT,
+  SET_LOCATION,
   SET_MAP_LAYER,
   SET_MAP_LOCATION,
   SET_MAP_VIEW,
@@ -50,12 +51,15 @@ const getFormLocation = (state, action) => {
 
 function tree(state = initialState, action) {
   switch (action.type) {
-    case SET_LOCATION: {
-      return { ...state, location: action.location };
-    }
     case SET_FORM_LOCATION: {
       return { ...state, formLocation: getFormLocation(state, action) };
     }
+    case SET_LOCATE_RESULT: {
+      return { ...state, locateResult: action.locateResult };
+    }
+    case SET_LOCATION: {
+      return { ...state, location: action.location };
+    }
     case SET_MAP_LAYER:
       return { ...state, mapLayer: action.mapLayer };
     case SET_MAP_LOCATION: {