@capillarytech/cap-ui-utils 1.3.3 → 1.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export { default as Auth } from './auth';
2
2
  export { default as GA } from './GA';
3
3
  export { default as multipleOrgSwitch } from './utils/multipleOrgSwitch';
4
- export { default as utilsSessionStorageApi } from './utils/utilsSessionStorageApi';
4
+ export { default as utilsSessionStorageApi } from './utils/utilsSessionStorageApi';
5
+ export { default as dateHelper } from './utils/date/dateHelper';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capillarytech/cap-ui-utils",
3
- "version": "1.3.3",
3
+ "version": "1.3.5",
4
4
  "description": "Utility functions shared accross all the modules",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -10,6 +10,8 @@
10
10
  "dependencies": {
11
11
  "react": "^16.13.0",
12
12
  "react-ga": "^2.7.0",
13
- "tti-polyfill": "^0.2.2"
13
+ "tti-polyfill": "^0.2.2",
14
+ "moment-timezone": "^0.5.25",
15
+ "react-intl": "2.7.2"
14
16
  }
15
17
  }
@@ -0,0 +1,5 @@
1
+ export const DATE_FORMAT = "DD/MM/YYYY";
2
+ export const DATE_DISPLAY_FORMAT = "DD MMM YYYY";
3
+ export const TIME_DISPLAY_FORMAT = "hh:mm a";
4
+
5
+ export const STANDARD_DATE_FORMAT = 'YYYY-MM-DD';
@@ -0,0 +1,157 @@
1
+ import moment from "moment-timezone";
2
+ import range from "lodash/range";
3
+ import messages from "./messages";
4
+ import * as constants from "./constants";
5
+
6
+ const {
7
+ DATE_DISPLAY_FORMAT,
8
+ DATE_FORMAT,
9
+ TIME_DISPLAY_FORMAT,
10
+ STANDARD_DATE_FORMAT,
11
+ } = constants;
12
+
13
+ let _intl = {
14
+ formatMessage: () => {},
15
+ formatNumer: () => {},
16
+ };
17
+
18
+ export const initializeIntl = (intl) => {
19
+ _intl = intl;
20
+ };
21
+
22
+ export const isPreviousDate = (current) =>
23
+ current < moment().subtract(1, "days").endOf("day");
24
+
25
+ export const getPreviousHours = (date) => {
26
+ const momentDate = date ? moment(date) : moment();
27
+ return range(momentDate.hour());
28
+ };
29
+
30
+ export const getPreviousMinutes = (date) => {
31
+ const momentDate = date ? moment(date) : moment();
32
+ return range(momentDate.minute());
33
+ };
34
+
35
+ export const disableDateRange = (startDate, endDate) => (current) =>
36
+ moment(endDate).endOf("day") < current ||
37
+ current < moment(startDate).startOf("day");
38
+
39
+ export const getRelativeDay = (date) => {
40
+ const selectedFormat = "Do MMM";
41
+ const todayDate = moment().format(selectedFormat);
42
+ const yesterdayDate = moment().subtract(1, "days").format(selectedFormat);
43
+ const tomorrowDate = moment().add(1, "days").format(selectedFormat);
44
+
45
+ return moment(date).calendar(null, {
46
+ sameDay: `[${_intl.formatMessage(messages.today)}, ${todayDate}]`,
47
+ nextDay: `[${_intl.formatMessage(messages.tomorrow)}, ${tomorrowDate}]`,
48
+ lastDay: `[${_intl.formatMessage(messages.yesterday)}, ${yesterdayDate}]`,
49
+ sameElse: DATE_DISPLAY_FORMAT,
50
+ nextWeek: DATE_DISPLAY_FORMAT,
51
+ lastWeek: DATE_DISPLAY_FORMAT,
52
+ });
53
+ };
54
+ export const getEpochTimeStamp = ({
55
+ timezone,
56
+ date,
57
+ withMilliSeconds = true,
58
+ }) => {
59
+ let method = "valueOf"; // Unix time in milliseconds: http://momentjs.com/docs/#/displaying/unix-timestamp-milliseconds/
60
+ if (!withMilliSeconds) {
61
+ method = "unix"; // unix time in seconds: http://momentjs.com/docs/#/displaying/unix-timestamp/
62
+ }
63
+ if (timezone) {
64
+ return moment(date).tz(timezone)[method](); //eslint-disable-line prettier/prettier
65
+ } else {
66
+ return moment(date)[method]();
67
+ }
68
+ };
69
+
70
+ /**
71
+ * Gives ISO day of the week with 1 being Monday and 7 being Sunday in numbers for org settings
72
+ * Org Monday = 0, Tuesday = 1, Wed = 2, Thur =3, Fri = 4, Sat = 5, Sun = 6
73
+ * @param weekDay
74
+ * @returns {number}
75
+ */
76
+ export function getISOWeekDayNumberForOrgSetting(weekDay) {
77
+ if (isNaN(Number(weekDay)) || Number(weekDay) > 6 || Number(weekDay) < 0) {
78
+ return 1;
79
+ }
80
+ if (Number(weekDay) === 6) return 0; //to handle Sunday of this week and not next sunday
81
+ return Number(weekDay) + 1;
82
+ }
83
+
84
+ /**
85
+ * base date range for last period
86
+ * @param lastSyncDate
87
+ * @param baseFilter
88
+ * @param periodValue
89
+ * @param weekStart
90
+ * @returns {{start, end}}
91
+ */
92
+ export function getDateRangeForPeriod(
93
+ lastSyncDate,
94
+ baseFilter,
95
+ periodValue,
96
+ weekStart = 0
97
+ ) {
98
+ const dateRange = {};
99
+ const now = moment(lastSyncDate);
100
+ if (baseFilter === "week") {
101
+ const weekStartDay = getISOWeekDayNumberForOrgSetting(weekStart);
102
+ const startOfWeek = now.clone().isoWeekday(weekStartDay);
103
+ if (startOfWeek.isAfter(now)) {
104
+ startOfWeek.subtract(1, "week");
105
+ }
106
+ dateRange.start = startOfWeek.clone().subtract(periodValue, baseFilter);
107
+ dateRange.end = startOfWeek.clone();
108
+ } else if (baseFilter === "month") {
109
+ dateRange.start = now.clone().subtract(periodValue, baseFilter);
110
+ dateRange.end = now.clone();
111
+ } else {
112
+ dateRange.start = moment()
113
+ .subtract(periodValue, baseFilter)
114
+ .startOf(baseFilter);
115
+ dateRange.end = now.clone();
116
+ }
117
+ return getFormattedDateRange(dateRange);
118
+ }
119
+
120
+ /**
121
+ * Return formatted values for start and end of date range
122
+ * @param dateRange
123
+ * @param dateFormat
124
+ * @returns {{start, end}}
125
+ */
126
+ export function getFormattedDateRange(
127
+ dateRange,
128
+ dateFormat = STANDARD_DATE_FORMAT
129
+ ) {
130
+ return {
131
+ start: dateRange.start.clone().format(dateFormat),
132
+ end: dateRange.end.clone().format(dateFormat),
133
+ };
134
+ }
135
+ export const getDayDifference = (currentDate, compareDate) => {
136
+ // startDate, endDate can't be epoch time in seconds, as the parsing is different for that
137
+ const formattedCurrentDate = moment(currentDate).format(DATE_FORMAT);
138
+ const formattedCompareDate = moment(compareDate).format(DATE_FORMAT);
139
+ return moment(formattedCompareDate, DATE_FORMAT).diff(
140
+ moment(formattedCurrentDate, DATE_FORMAT),
141
+ "day"
142
+ );
143
+ };
144
+
145
+ export const msToDateFormat = (date) =>
146
+ moment(date).format(DATE_DISPLAY_FORMAT);
147
+
148
+ export const msToTimeFormat = (date) =>
149
+ moment(date).format(TIME_DISPLAY_FORMAT);
150
+
151
+ /* unix() function gives number of seconds since epoch time,
152
+ but for the api need to app number of days since epoch time.
153
+ so deviding number of seconds since epoch with number of seconds a day to get number of days since epoch
154
+ */
155
+
156
+ export const getEpochDays = (date) =>
157
+ date && Math.floor(moment(date.clone()).unix() / 86400); // 86400 === number of seconds a day
@@ -0,0 +1,24 @@
1
+ /*
2
+ * dateHelper Messages
3
+ *
4
+ * This contains all the text for the dateHelper.
5
+ */
6
+
7
+ import { defineMessages } from "react-intl";
8
+
9
+ export const scope = "uiUtils.utils.date.dateHelper";
10
+
11
+ export default defineMessages({
12
+ today: {
13
+ id: `${scope}.today`,
14
+ defaultMessage: "Today",
15
+ },
16
+ tomorrow: {
17
+ id: `${scope}.tomorrow`,
18
+ defaultMessage: "Tomorrow",
19
+ },
20
+ yesterday: {
21
+ id: `${scope}.yesterday`,
22
+ defaultMessage: "Yesterday",
23
+ },
24
+ });
@@ -11,7 +11,7 @@ const logNewTab = () => {
11
11
  saveItem(`tab_${hash}`, true);
12
12
  };
13
13
 
14
- const removeTabsFromStorage = (tabs) => {
14
+ const removeTabsFromStorage = (tabs = {}) => {
15
15
  for (const key in tabs) {
16
16
  if (tabs.hasOwnProperty(key)) {
17
17
  clearItem(key);
@@ -32,7 +32,7 @@ const filterObjectByKeyName = (substring, object) => {
32
32
  return filteredObject;
33
33
  };
34
34
 
35
- const utilsGetOrgNameFromId = (orgId, proxyOrgList, defaultOrgID, defaultOrgName) => {
35
+ const utilsGetOrgNameFromId = (orgId, proxyOrgList = [], defaultOrgID, defaultOrgName) => {
36
36
  const orgObj = proxyOrgList.find(proxyOrg => proxyOrg['orgID'] == orgId);
37
37
  const orgName = (orgObj && orgObj.orgName) || orgId;
38
38
  if (orgName === defaultOrgID){