Including build.sh to run gulp-webpack
[aai/sparky-fe.git] / src / generic-components / dateRange / DateRangeActions.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 i18n from 'utils/i18n/i18n';
24 import moment from 'moment';
25
26 import {
27   dateRangeActionTypes,
28   DATE_PICKER_PLACEHOLDER,
29   START_DATE,
30   END_DATE,
31   IS_AFTER_END_DATE,
32   IS_BEFORE_START_DATE
33 } from 'generic-components/dateRange/DateRangeConstants';
34
35 function buildInvalidDateRangeAction(startDate, endDate, errorMessage) {
36   return {
37     type: dateRangeActionTypes.INVALID_DATE_RANGE,
38     data: {
39       dateRange: {
40         startDate: startDate,
41         endDate: endDate
42       },
43       errorMsg: errorMessage
44     }
45   };
46 }
47
48 function buildDateRangeAction(startDate, endDate) {
49   return {
50     type: dateRangeActionTypes.DATE_RANGE_CHANGE,
51     data: {
52       dateRange: {
53         startDate: startDate,
54         endDate: endDate
55       }
56     }
57   };
58 }
59
60 export default {
61   onStartDateChange(startDate, endDate) {
62     if (endDate && endDate.isBefore(startDate)) {
63       var errorMessage = i18n(START_DATE) +
64         ': ' +
65         moment(new Date(startDate)).format(DATE_PICKER_PLACEHOLDER) +
66         ' ' +
67         i18n(IS_AFTER_END_DATE);
68       return buildInvalidDateRangeAction(startDate, endDate, errorMessage);
69     } else {
70       return buildDateRangeAction(startDate, endDate);
71     }
72   },
73   onEndDateChange(startDate, endDate) {
74     if (startDate && startDate.isAfter(endDate)) {
75       var errorMessage = i18n(END_DATE) +
76         ': ' +
77         moment(new Date(endDate)).format(DATE_PICKER_PLACEHOLDER) +
78         ' ' +
79         i18n(IS_BEFORE_START_DATE);
80       return buildInvalidDateRangeAction(startDate, endDate, errorMessage);
81     } else {
82       return buildDateRangeAction(startDate, endDate);
83     }
84   }
85 };