comparison client/src/store/fairwayavailability.js @ 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 70605404f37d
children ccb5455f0713
comparison
equal deleted inserted replaced
3375:e1a75cd15450 3376:70b7078b4516
11 * Author(s): 11 * Author(s):
12 * Thomas Junk <thomas.junk@intevation.de> 12 * Thomas Junk <thomas.junk@intevation.de>
13 */ 13 */
14 14
15 import { HTTP } from "@/lib/http"; 15 import { HTTP } from "@/lib/http";
16 import { format, subYears } from "date-fns"; 16 import {
17 format,
18 subYears,
19 startOfYear,
20 endOfYear,
21 startOfQuarter,
22 endOfQuarter
23 } from "date-fns";
17 24
18 const LNWLDIAGRAMMOCKDATA = { 25 const LNWLDIAGRAMMOCKDATA = {
19 lnwl: [ 26 lnwl: [
20 { level: "LDC", value: 162, percent: 40 }, 27 { level: "LDC", value: 162, percent: 40 },
21 { level: "HDC", value: 564, percent: 60 } 28 { level: "HDC", value: 564, percent: 60 }
29 36
30 const FREQUENCIES = { 37 const FREQUENCIES = {
31 MONTHLY: "monthly", 38 MONTHLY: "monthly",
32 QUARTERLY: "quarterly", 39 QUARTERLY: "quarterly",
33 YEARLY: "yearly" 40 YEARLY: "yearly"
41 };
42
43 const getIntervallBorders = (start, end, frequency) => {
44 switch (frequency) {
45 case FREQUENCIES.MONTHLY:
46 return [start, end];
47 case FREQUENCIES.YEARLY:
48 return [
49 format(startOfYear(start), "YYYY-MM-DD"),
50 format(endOfYear(end), "YYYY-MM-DD")
51 ];
52 case FREQUENCIES.QUARTERLY:
53 return [
54 format(startOfQuarter(start), "YYYY-MM-DD"),
55 format(endOfQuarter(end), "YYYY-MM-DD")
56 ];
57 default:
58 throw new Error("Boom!");
59 }
34 }; 60 };
35 61
36 const init = () => { 62 const init = () => {
37 return { 63 return {
38 type: "bottlenecks", 64 type: "bottlenecks",
123 } 149 }
124 }, 150 },
125 actions: { 151 actions: {
126 loadAvailableFairwayDepth: ({ commit }, options) => { 152 loadAvailableFairwayDepth: ({ commit }, options) => {
127 return new Promise((resolve, reject) => { 153 return new Promise((resolve, reject) => {
128 const { feature, from, to, frequency, LOS } = options; 154 const { feature, frequency, LOS } = options;
155 let { from, to } = options;
129 let name = 156 let name =
130 feature.constructor.name === "Feature" 157 feature.constructor.name === "Feature"
131 ? feature.get("objnam") 158 ? feature.get("objnam")
132 : feature.properties.name; 159 : feature.properties.name;
160 [from, to] = getIntervallBorders(from, to, frequency);
133 const start = encodeURIComponent("00:00:00+00:00"); 161 const start = encodeURIComponent("00:00:00+00:00");
134 const end = encodeURIComponent("23:59:59+00:00"); 162 const end = encodeURIComponent("23:59:59+00:00");
135 const URL = `/data/bottleneck/fairway-depth/${encodeURIComponent( 163 const URL = `/data/bottleneck/fairway-depth/${encodeURIComponent(
136 name 164 name
137 )}?from=${from}T${start}&to=${to}T${end}&mode=${frequency}&los=${LOS}`; 165 )}?from=${from}T${start}&to=${to}T${end}&mode=${frequency}&los=${LOS}`;
172 }); 200 });
173 }); 201 });
174 }, 202 },
175 loadAvailableFairwayDepthLNWL: (context, options) => { 203 loadAvailableFairwayDepthLNWL: (context, options) => {
176 return new Promise((resolve, reject) => { 204 return new Promise((resolve, reject) => {
177 const { feature, from, to, frequency, LOS } = options; 205 const { feature, frequency, LOS } = options;
206 let { from, to } = options;
178 let name = 207 let name =
179 feature.constructor.name === "Feature" 208 feature.constructor.name === "Feature"
180 ? feature.get("objnam") 209 ? feature.get("objnam")
181 : feature.properties.name; 210 : feature.properties.name;
211 [from, to] = getIntervallBorders(from, to, frequency);
182 const start = encodeURIComponent("00:00:00+00:00"); 212 const start = encodeURIComponent("00:00:00+00:00");
183 const end = encodeURIComponent("23:59:59+00:00"); 213 const end = encodeURIComponent("23:59:59+00:00");
184 const URL = `/data/bottleneck/availability/${encodeURIComponent( 214 const URL = `/data/bottleneck/availability/${encodeURIComponent(
185 name 215 name
186 )}?from=${from}T${start}&to=${to}T${end}&mode=${frequency}&los=${LOS}`; 216 )}?from=${from}T${start}&to=${to}T${end}&mode=${frequency}&los=${LOS}`;