view client/src/store/gauges.js @ 2692:966c94e699fa

client: waterlevel diagram: simulate loading time and removed old demo code
author Markus Kottlaender <markus@intevation.de>
date Fri, 15 Mar 2019 17:02:36 +0100
parents b6865a08c905
children e622689d73bd
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 <markus@intevation.de>
 */
import { HTTP } from "@/lib/http";
import { WFS } from "ol/format.js";

const init = () => {
  return {
    gauges: [],
    selectedGaugeISRS: null,
    waterlevels: [],
    loading: false
  };
};

export default {
  init,
  namespaced: true,
  state: init(),
  getters: {
    selectedGauge: state => {
      return state.gauges.find(
        g => g.properties.isrs_code === state.selectedGaugeISRS
      );
    }
  },
  mutations: {
    gauges: (state, gauges) => {
      state.gauges = gauges;
    },
    selectedGaugeISRS: (state, isrs) => {
      state.selectedGaugeISRS = isrs;
    },
    waterlevels: (state, data) => {
      state.waterlevels = data;
    },
    loading: (state, loading) => {
      state.loading = loading;
    }
  },
  actions: {
    selectedGaugeISRS: ({ commit }, isrs) => {
      commit("selectedGaugeISRS", isrs);
      commit("application/showGauges", true, { root: true });
      commit("application/showSplitscreen", false, { root: true });
    },
    loadGauges: ({ commit }) => {
      return new Promise((resolve, reject) => {
        var gaugesFeatureCollectionRequest = new WFS().writeGetFeature({
          srsName: "EPSG:4326",
          featureNS: "gemma",
          featurePrefix: "gemma",
          featureTypes: ["gauges_geoserver"],
          outputFormat: "application/json"
        });
        HTTP.post(
          "/internal/wfs",
          new XMLSerializer().serializeToString(gaugesFeatureCollectionRequest),
          {
            headers: {
              "X-Gemma-Auth": localStorage.getItem("token"),
              "Content-type": "text/xml; charset=UTF-8"
            }
          }
        )
          .then(response => {
            commit("gauges", response.data.features);
            resolve(response);
          })
          .catch(error => {
            reject(error);
          });
      });
    },
    loadWaterlevels({ getters, commit }, timePeriod) {
      let waterlevels = require("@/components/gauge/waterlevels-demo.json");

      return new Promise(resolve => {
        if (getters.selectedGauge && timePeriod) {
          let data = waterlevels
            .filter(wl => wl.gauge === getters.selectedGauge.id.split(".")[1])
            .map(wl => {
              wl.date = new Date(wl.date);
              return wl;
            });

          setTimeout(() => {
            commit("waterlevels", data);
            resolve(data);
          }, 2000);
        } else {
          commit("waterlevels", []);
        }
      });
    }
  }
};