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