view client/src/store/bottlenecks.js @ 1296:99c039e86624

replaced manual store cleanup on logout with central store reset The store files now provide a init method with which the store can be resetted on logout. This is reasonable because missing store cleanups on logout caused bugs. Furthermore the localStorage.clear() was replaced specific removal of values since currently there are also saved cross profiles which should be persistent. The initialization of the cut/line/polygon tool was moved from their respective buttons to the map store right after saving the ol map object in the store. Before that change the map object was not present sometimes when trying to initiate those tools.
author Markus Kottlaender <markus@intevation.de>
date Thu, 22 Nov 2018 17:00:26 +0100
parents bc55ffaeb639
children ca33ad696594
line wrap: on
line source

/*
 * This is Free Software under GNU Affero General Public License v >= 3.0
 * without warranty, see README.md and license for details.
 *
 * SPDX-License-Identifier: AGPL-3.0-or-later
 * License-Filename: LICENSES/AGPL-3.0.txt
 *
 * Copyright (C) 2018 by via donau
 *   – Österreichische Wasserstraßen-Gesellschaft mbH
 * Software engineering by Intevation GmbH
 *
 * Author(s):
 * Markus Kottländer <markuks.kottlaender@intevation.de>
 */
import { HTTP } from "../lib/http";
import { WFS } from "ol/format.js";
import { displayError } from "../lib/errors.js";

// initial state
const init = () => {
  return {
    bottlenecks: [],
    selectedBottleneck: null,
    surveys: [],
    selectedSurvey: null
  };
};

export default {
  init,
  namespaced: true,
  state: init(),
  mutations: {
    setBottlenecks: (state, bottlenecks) => {
      state.bottlenecks = bottlenecks;
    },
    setSelectedBottleneck: (state, name) => {
      state.selectedBottleneck = name;
    },
    setSurveys(state, surveys) {
      state.surveys = surveys;
    },
    setSelectedSurvey(state, survey) {
      state.selectedSurvey = survey;
    }
  },
  actions: {
    setSelectedBottleneck({ state, commit, dispatch }, name) {
      if (name !== state.selectedBottleneck) {
        commit("setSelectedSurvey", null);
        commit("fairwayprofile/clearCurrentProfile", null, { root: true });
      }
      commit("setSelectedBottleneck", name);
      dispatch("querySurveys", name);
    },
    loadBottlenecks({ commit }) {
      var bottleneckFeatureCollectionRequest = new WFS().writeGetFeature({
        srsName: "EPSG:4326",
        featureNS: "gemma",
        featurePrefix: "gemma",
        featureTypes: ["bottleneck_overview"],
        outputFormat: "application/json"
      });

      HTTP.post(
        "/internal/wfs",
        new XMLSerializer().serializeToString(
          bottleneckFeatureCollectionRequest
        ),
        {
          headers: {
            "X-Gemma-Auth": localStorage.getItem("token"),
            "Content-type": "text/xml; charset=UTF-8"
          }
        }
      ).then(response => {
        commit("setBottlenecks", response.data.features);
      });
    },
    querySurveys({ commit }, name) {
      if (name) {
        HTTP.get("/surveys/" + name, {
          headers: {
            "X-Gemma-Auth": localStorage.getItem("token"),
            "Content-type": "text/xml; charset=UTF-8"
          }
        })
          .then(response => {
            commit("setSurveys", response.data.surveys);
          })
          .catch(error => {
            commit("setSurveys", []);
            const { status, data } = error.response;
            displayError({
              title: "Backend Error",
              message: `${status}: ${data.message || data}`
            });
          });
      } else {
        commit("setSurveys", []);
      }
    }
  }
};