-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathActivityGrid.utils.tsx
More file actions
254 lines (228 loc) · 7.36 KB
/
Copy pathActivityGrid.utils.tsx
File metadata and controls
254 lines (228 loc) · 7.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import { t } from 'i18next';
import {
isWithinInterval,
getDay,
getDate,
getYear,
getMonth,
getHours,
getMinutes,
getSeconds,
format,
isWeekend,
isAfter,
setDate,
setMonth,
setYear,
} from 'date-fns';
import { Event } from 'modules/Dashboard/state';
import { Svg } from 'shared/components/Svg';
import { MenuItem, MenuItemType } from 'shared/components';
import {
checkIfCanAccessData,
checkIfCanEdit,
checkIfCanManageParticipants,
checkIfFullAccess,
getIsWebSupported,
} from 'shared/utils';
import { EditablePerformanceTasks } from 'modules/Builder/features/Activities/Activities.const';
import { ActivityActions, ActivityActionProps } from './ActivityGrid.types';
/**
* this function checks if the current date is within the interval passed as arguments
* @param start Date
* @param end Date
* @returns boolean
*/
function datetimeIsWithinInterval(start: Date, end: Date) {
const today = new Date();
if (isAfter(start, end)) {
console.error('The start date is after the end date, be sure to check the dates');
// using the isWithinInterval function with the start and end dates swapped
// in case the start date is after the end date
return isWithinInterval(today, { start: end, end: start });
}
return isWithinInterval(today, { start, end });
}
/**
* this function returns the values of the date represented as an object
* @param date Date
* @returns object
*/
function getDateValues(date: Date) {
if (!date || !(date instanceof Date)) {
console.error('The date is not valid, be sure to pass a valid date');
return {
day: 0,
month: 0,
year: 0,
hour: 0,
minute: 0,
second: 0,
dayInWeek: 0,
date: new Date(),
};
}
return {
day: getDate(date),
month: getMonth(date) + 1,
year: getYear(date),
hour: getHours(date),
minute: getMinutes(date),
second: getSeconds(date),
dayInWeek: getDay(date),
date,
};
}
const PERIODICITY_VALUES = {
ONCE: 'ONCE',
ALWAYS: 'ALWAYS',
DAILY: 'DAILY',
WEEKLY: 'WEEKLY',
WEEKDAYS: 'WEEKDAYS',
MONTHLY: 'MONTHLY',
};
/**
* this function formats the date as YYYY-MM-DD
* @param date Date
* @returns string
*/
function formatDateAsYYYYMMDD(date: Date) {
if (!date || !(date instanceof Date)) {
console.error('The date is not valid, be sure to pass a valid date');
}
return format(date, 'yyyy-MM-dd');
}
/**
* this function validates if the date is in range based on the PeriodicityTypes object
* the options of validation are:
* - if the periodicity is always, which means that the activity is always available
* - if the periodicity is once, which means that the activity is available only once
* - if the periodicity is daily, which means that the activity is available every day
* - if the periodicity is weekly, which means that the activity is available every week at the same week day
* - if the periodicity is weekdays, which means that the activity is available every weekday and not in weekends
* - if the periodicity is monthly, which means that the activity is available every month at the same day
* - if the periodicity is not set
* @param scheduleEvent PeriodicityType
* @returns boolean
*/
function validateIfDateIsInRange(scheduleEvent: Event) {
if (!scheduleEvent) {
console.error('The periodicity is not set, be sure to pass a valid periodicity');
return false;
}
if (
scheduleEvent.accessBeforeSchedule ||
scheduleEvent.periodicity.type === PERIODICITY_VALUES.ALWAYS
) {
return true;
}
const currentDate = new Date();
const currentTimeValues = getDateValues(currentDate);
const startDate = new Date(
`${
scheduleEvent.periodicity.startDate
? scheduleEvent.periodicity.startDate
: formatDateAsYYYYMMDD(currentDate)
}T${scheduleEvent.startTime}`,
);
const endDate = new Date(
`${
scheduleEvent?.periodicity.endDate
? scheduleEvent.periodicity.endDate
: formatDateAsYYYYMMDD(currentDate)
}T${scheduleEvent.endTime}`,
);
const startTimeValues = getDateValues(startDate);
if (
scheduleEvent.periodicity.type === PERIODICITY_VALUES.ONCE ||
scheduleEvent.periodicity.type === PERIODICITY_VALUES.DAILY
) {
const todayEndDateWithEndTime = setYear(
setMonth(setDate(endDate, currentTimeValues.day), currentTimeValues.month - 1),
currentTimeValues.year,
);
return datetimeIsWithinInterval(startDate, todayEndDateWithEndTime);
} else if (scheduleEvent.periodicity.type === PERIODICITY_VALUES.WEEKLY) {
if (currentTimeValues.dayInWeek !== startTimeValues.dayInWeek) {
return false;
}
return datetimeIsWithinInterval(startDate, endDate);
} else if (scheduleEvent.periodicity.type === PERIODICITY_VALUES.WEEKDAYS) {
if (isWeekend(currentDate)) {
return false;
}
const todayEndDateWithEndTime = setYear(
setMonth(setDate(endDate, currentTimeValues.day), currentTimeValues.month - 1),
currentTimeValues.year,
);
return datetimeIsWithinInterval(startDate, todayEndDateWithEndTime);
} else if (
scheduleEvent.periodicity.type === PERIODICITY_VALUES.MONTHLY &&
currentTimeValues.day === startTimeValues.day
) {
return datetimeIsWithinInterval(startDate, endDate);
}
return false;
}
export const getActivityActions = ({
actions: { editActivity, exportData, assignActivity, takeNow },
appletId,
dataTestId,
roles,
featureFlags,
hasParticipants,
activity,
}: ActivityActions): MenuItem<ActivityActionProps>[] => {
const canEdit =
(checkIfCanEdit(roles) && !activity?.isPerformanceTask) ||
EditablePerformanceTasks.includes(activity?.performanceTaskType ?? '');
const canAccessData = checkIfCanAccessData(roles);
const canDoTakeNow =
featureFlags.enableMultiInformantTakeNow && hasParticipants && checkIfFullAccess(roles);
const canAssignActivity =
checkIfCanManageParticipants(roles) && featureFlags.enableActivityAssign;
const showDivider = (canEdit || canAccessData) && (canDoTakeNow || canAssignActivity);
const { id: activityId } = activity;
const isWebUnsupported = !getIsWebSupported(activity.items);
if (!activityId || !activity?.event) return [];
const isInRange = validateIfDateIsInRange(activity?.event);
return [
{
icon: <Svg id="edit" />,
action: editActivity,
title: t('editActivity'),
context: { appletId, activityId },
isDisplayed: canEdit,
'data-testid': `${dataTestId}-activity-edit`,
},
{
icon: <Svg id="export" />,
action: exportData,
title: t('exportData'),
context: { appletId, activityId },
isDisplayed: canAccessData,
'data-testid': `${dataTestId}-activity-export`,
},
{ type: MenuItemType.Divider, isDisplayed: showDivider },
{
icon: <Svg id="add" />,
action: assignActivity,
title: t('assignActivity'),
context: { appletId, activityId },
isDisplayed: canAssignActivity,
'data-testid': `${dataTestId}-activity-assign`,
},
{
icon: <Svg id="play-outline" />,
action: takeNow,
title: t('takeNow.menuItem'),
context: { appletId, activityId },
isDisplayed: canDoTakeNow,
disabled: isWebUnsupported || !isInRange,
tooltip:
(!isInRange && t('activityIsUnavailableAtThisTime')) ||
(isWebUnsupported && t('activityIsMobileOnly')),
'data-testid': `${dataTestId}-activity-take-now`,
},
];
};