5d786f7196d0d77b876ce90b76f18a4583a3884f
[aai/sparky-fe.git] / src / generic-components / dateRangeSelector / DateRangeSelectorActions.js
1 /*
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25
26 import {
27   dateRangeSelectorActionTypes,
28   ERROR_UNKNOWN_PERIOD,
29   TODAY,
30   YESTERDAY,
31   LAST_WEEK,
32   LAST_MONTH,
33   CUSTOM
34 } from 'generic-components/dateRangeSelector/DateRangeSelectorConstants.js';
35
36 function buildPeriodChangeAction(startMoment, endMoment, period) {
37   return {
38     type: dateRangeSelectorActionTypes.EVENT_PERIOD_CHANGE,
39     data: {
40       dateRange: {
41         startDate: startMoment,
42         endDate: endMoment
43       },
44       period: period
45     }
46   };
47 }
48
49 function buildUnknownPeriodAction(startMoment, endMoment, period, errorMsg) {
50   return {
51     type: dateRangeSelectorActionTypes.EVENT_PERIOD_ERROR,
52     data: {
53       dateRange: {
54         startDate: startMoment,
55         endDate: endMoment
56       },
57       period: period,
58       errorMsg: errorMsg
59     }
60   };
61 }
62
63 export default {
64   onPeriodChange(startMoment, endMoment, period) {
65     var moment = require('moment');
66     let startPeriod = moment(new Date());
67     let endPeriod = moment(new Date());
68     let unknownPeriod = false;
69
70     switch (period) {
71       case TODAY:
72         // already have today's date set
73         break;
74       case YESTERDAY:
75         startPeriod = moment(startPeriod).subtract(1, 'days');
76         break;
77       case LAST_WEEK:
78         startPeriod = moment(startPeriod).subtract(7, 'days');
79         break;
80       case LAST_MONTH:
81         startPeriod = moment(startPeriod).subtract(1, 'months');
82         break;
83       case CUSTOM:
84         startPeriod = startMoment;
85         endPeriod = endMoment;
86         break;
87       default:
88         unknownPeriod = true;
89         break;
90     }
91
92     if (unknownPeriod) {
93       let errorMsg = ERROR_UNKNOWN_PERIOD + ': ' + period;
94       return buildUnknownPeriodAction(startMoment, endMoment, period, errorMsg);
95     } else {
96       /*
97        Temp fix until we support time ...
98        - set start date time to 00:00:00
99        - set end date time to 23:59:59
100        this is to ensure we cover an entire day
101        (ex: start day May 26, end day May 26 ... expect to cover
102           0:00:00 to 23:59:59 for that day)
103        */
104       startPeriod.toDate();
105       startPeriod.hour(0);
106       startPeriod.minute(0);
107       startPeriod.second(0);
108
109       endPeriod.toDate();
110       endPeriod.hour(23);
111       endPeriod.minute(59);
112       endPeriod.second(59);
113
114       return buildPeriodChangeAction(startPeriod, endPeriod, period);
115     }
116
117   }
118 };