view client/src/fairway/store.js @ 849:d63e60b868bf

WIP Fairwayprofile
author Thomas Junk <thomas.junk@intevation.de>
date Fri, 28 Sep 2018 12:35:24 +0200
parents 07be3e5f99a9
children b9aaa093a9fb
line wrap: on
line source

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

const DEMOLEVEL = 149.345;

const FairwayProfile = {
  namespaced: true,
  state: {
    totalLength: 0,
    minAlt: 0,
    maxAlt: 0,
    currentProfile: [],
    waterLevels: [
      { year: "2016", level: DEMOLEVEL, color: "#005DFF" },
      { year: "2017", level: 147.2, color: "#639CFF" }
    ],
    selectedWaterLevel: DEMOLEVEL,
    fairwayCoordinates: [],
    startPoint: null,
    endPoint: null
  },
  getters: {
    selectedWaterLevel: state => {
      return state.selectedWaterLevel;
    },
    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: {
    setSelectedWaterLevel: (state, level) => {
      state.selectedWaterLevel = level;
    },
    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;