view client/src/fairway/store.js @ 779:8ba1be486833

client: improve search * Add display of searchResults as list-group.
author Bernhard Reiter <bernhard@intevation.de>
date Wed, 26 Sep 2018 13:46:18 +0200
parents dedf252b3e01
children c98f88ac08a4
line wrap: on
line source

import { HTTP } from "../application/lib/http";
import { prepareProfile } from "../application/lib/geo";

const FairwayProfile = {
  namespaced: true,
  state: {
    totalLength: 0,
    minAlt: 0,
    maxAlt: 0,
    currentProfile: [],
    startPoint: null,
    endPoint: null
  },
  getters: {
    currentProfile: state => {
      return state.currentProfile;
    },
    startPoint: state => {
      return state.startPoint;
    },
    endPoint: state => {
      return state.endPoint;
    },
    length: state => {
      return state.totalLength;
    },
    minAlt: state => {
      return state.minAlt;
    },
    maxAlt: state => {
      return state.maxAlt;
    }
  },
  mutations: {
    profileLoaded: (state, response) => {
      const { data } = response;
      const coordinates = data.geometry.coordinates;
      if (!coordinates) return;
      const startPoint = state.startPoint;
      const endPoint = state.endPoint;
      const geoJSON = data;
      const result = prepareProfile({ geoJSON, startPoint, endPoint });
      state.currentProfile = result.points;
      state.minAlt = result.minAlt;
      state.maxAlt = result.maxAlt;
      state.totalLength = result.totalLength;
    },
    setStartPoint: (state, start) => {
      state.startPoint = start;
    },
    setEndPoint: (state, end) => {
      state.endPoint = end;
    }
  },

  actions: {
    loadProfile({ commit }, geoJSON) {
      return new Promise((resolve, reject) => {
        HTTP.post("/cross", geoJSON, {
          headers: { "X-Gemma-Auth": localStorage.getItem("token") }
        })
          .then(response => {
            commit("profileLoaded", response);
            resolve(response);
          })
          .catch(error => {
            reject(error);
          });
      });
    }
  }
};

export default FairwayProfile;