view client/src/store/diagram.js @ 3205:bf571429515f

client: fairway availability: moved component's type property to store to manipulate it from outside
author Markus Kottlaender <markus@intevation.de>
date Wed, 08 May 2019 17:15:08 +0200
parents cb67ee72485b
children
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):
 * Thomas Junk <thomas.junk@intevation.de>
 */

import { HTTP } from "@/lib/http";

const init = () => {
  return {
    type: "bottlenecks",
    selectedFairwayAvailabilityFeature: null,
    from: null,
    to: null,
    frequency: null,
    fwData: null,
    legend: null,
    LOS: null
  };
};

const diagram = {
  init,
  namespaced: true,
  state: init(),
  mutations: {
    type: (state, type) => {
      state.type = type;
    },
    setLOS: (state, LOS) => {
      state.LOS = LOS;
    },
    setFrequency: (state, frequency) => {
      state.frequency = frequency;
    },
    setFrom: (state, from) => {
      state.from = from;
    },
    setTo: (state, to) => {
      state.to = to;
    },
    setSelectedFairwayAvailability: (state, feature) => {
      state.selectedFairwayAvailabilityFeature = feature;
    },
    setFwData: (state, fwData) => {
      state.fwData = fwData;
    },
    setLegend: (state, header) => {
      const headerEntries = header.split(",");
      headerEntries.shift();
      state.legend = headerEntries.map(x => {
        return x.split("#")[1].trim();
      });
    }
  },
  actions: {
    loadAvailableFairwayDepth: ({ commit }, options) => {
      return new Promise((resolve, reject) => {
        const { feature, from, to, frequency, LOS } = options;
        const start = encodeURIComponent("00:00:00+00:00");
        const end = encodeURIComponent("23:59:59+00:00");
        const URL = `/data/bottleneck/fairway-depth/${encodeURIComponent(
          feature.properties.name
        )}?from=${from}T${start}&to=${to}T${end}&mode=${frequency}&los=${LOS}`;
        HTTP.get(URL, {
          headers: { "X-Gemma-Auth": localStorage.getItem("token") }
        })
          .then(response => {
            const { data } = response;
            const csv = data.split("\n");
            commit("setLegend", csv.shift());
            let transformed = csv.map(e => {
              const result = e.split(",");
              const label = result.shift();
              const ldc = result.shift();
              const highestLevel = result.pop();
              return {
                label: label,
                ldc: ldc,
                highestLevel: highestLevel,
                lowerLevels: result
              };
            });
            commit("setFwData", transformed);
            resolve(response);
          })
          .catch(error => {
            reject(error);
          });
      });
    }
  }
};

export { diagram };