view client/src/fairway/store.js @ 802:327aa4a18a1c

Fairway profile WIP
author Thomas Junk <thomas.junk@intevation.de>
date Thu, 27 Sep 2018 13:36:43 +0200
parents 1bee00039973
children 07be3e5f99a9
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: [],
    waterLevels: [
      { year: "2016", level: 0, color: "#005DFF" },
      { year: "2017", level: -0.5, color: "#639CFF" }
    ],
    fairwayCoordinates: [],
    startPoint: null,
    endPoint: null
  },
  getters: {
    fairwayCoordinates: state => {
      return state.fairwayCoordinates;
    },
    waterLevels: state => {
      return state.waterLevels;
    },
    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;
    },
    totalLength: state => {
      return state.totalLength;
    }
  },
  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.lengthPolyLine;
    },
    setStartPoint: (state, start) => {
      state.startPoint = start;
    },
    setEndPoint: (state, end) => {
      state.endPoint = end;
    },
    setFairwayCoordinates: (state, coordinates) => {
      state.fairwayCoordinates = coordinates;
    }
  },

  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;