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