changeset 3376:70b7078b4516

available_fairway_depth: determine intervall borders for dates
author Thomas Junk <thomas.junk@intevation.de>
date Wed, 22 May 2019 15:03:30 +0200
parents e1a75cd15450
children ccb5455f0713
files client/src/store/fairwayavailability.js
diffstat 1 files changed, 33 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/client/src/store/fairwayavailability.js	Wed May 22 14:00:28 2019 +0200
+++ b/client/src/store/fairwayavailability.js	Wed May 22 15:03:30 2019 +0200
@@ -13,7 +13,14 @@
  */
 
 import { HTTP } from "@/lib/http";
-import { format, subYears } from "date-fns";
+import {
+  format,
+  subYears,
+  startOfYear,
+  endOfYear,
+  startOfQuarter,
+  endOfQuarter
+} from "date-fns";
 
 const LNWLDIAGRAMMOCKDATA = {
   lnwl: [
@@ -33,6 +40,25 @@
   YEARLY: "yearly"
 };
 
+const getIntervallBorders = (start, end, frequency) => {
+  switch (frequency) {
+    case FREQUENCIES.MONTHLY:
+      return [start, end];
+    case FREQUENCIES.YEARLY:
+      return [
+        format(startOfYear(start), "YYYY-MM-DD"),
+        format(endOfYear(end), "YYYY-MM-DD")
+      ];
+    case FREQUENCIES.QUARTERLY:
+      return [
+        format(startOfQuarter(start), "YYYY-MM-DD"),
+        format(endOfQuarter(end), "YYYY-MM-DD")
+      ];
+    default:
+      throw new Error("Boom!");
+  }
+};
+
 const init = () => {
   return {
     type: "bottlenecks",
@@ -125,11 +151,13 @@
   actions: {
     loadAvailableFairwayDepth: ({ commit }, options) => {
       return new Promise((resolve, reject) => {
-        const { feature, from, to, frequency, LOS } = options;
+        const { feature, frequency, LOS } = options;
+        let { from, to } = options;
         let name =
           feature.constructor.name === "Feature"
             ? feature.get("objnam")
             : feature.properties.name;
+        [from, to] = getIntervallBorders(from, to, frequency);
         const start = encodeURIComponent("00:00:00+00:00");
         const end = encodeURIComponent("23:59:59+00:00");
         const URL = `/data/bottleneck/fairway-depth/${encodeURIComponent(
@@ -174,11 +202,13 @@
     },
     loadAvailableFairwayDepthLNWL: (context, options) => {
       return new Promise((resolve, reject) => {
-        const { feature, from, to, frequency, LOS } = options;
+        const { feature, frequency, LOS } = options;
+        let { from, to } = options;
         let name =
           feature.constructor.name === "Feature"
             ? feature.get("objnam")
             : feature.properties.name;
+        [from, to] = getIntervallBorders(from, to, frequency);
         const start = encodeURIComponent("00:00:00+00:00");
         const end = encodeURIComponent("23:59:59+00:00");
         const URL = `/data/bottleneck/availability/${encodeURIComponent(