view client/src/fairway/store.js @ 1013:15c3cc6f29a4

refac: fairwayprofile store cleanup
author Thomas Junk <thomas.junk@intevation.de>
date Tue, 23 Oct 2018 12:08:49 +0200
parents b9aaa093a9fb
children d2f30a784fb3
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: {
    availableSurveys: null,
    totalLength: 0,
    minAlt: 0,
    maxAlt: 0,
    currentProfile: [],
    waterLevels: [{ year: "2016", level: DEMOLEVEL, color: "#005DFF" }],
    selectedWaterLevel: DEMOLEVEL,
    fairwayCoordinates: [],
    startPoint: null,
    endPoint: null
  },
  getters: {
    length: state => {
      return state.totalLength;
    }
  },
  mutations: {
    setAvailableSurveys: (state, surveys) => {
      state.availableSurveys = surveys;
    },
    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;