playbook_ui 12.39.0 → 13.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4474115903d0053dd049ba1fce7b6d33f1be8490a518e49a9422acf83427a6b0
4
- data.tar.gz: 3b6259cc14efe5dbcc98c8cd23ca8d8befa087e6c1751c8062f4aa97b946b1aa
3
+ metadata.gz: 68b8019423c98f0b17cb991be96ff5eaf31f5ec127a81e1079f1d30d89bf2f8a
4
+ data.tar.gz: 266d1ba7ec9735ab40b632b590b448742429e780ef5635f92bd9e74b0e0fc6df
5
5
  SHA512:
6
- metadata.gz: be41e2bcd1e5eee5501a8142c22a6326f980baa0e9e6f939ee67330f56d478ccf92b390e4a59261a37daf69898611f6e30905e2a733efcc17115f2b03a2b22c1
7
- data.tar.gz: 4630db5119496f55cf137a925dd54a989947fd9ae914d5574c4014082f9ff2f99bb2141e7ac73564ec5c5487cde472c796f09ffd61e1d6ddf7511ed6ed64e1d2
6
+ metadata.gz: b2a9a36c332c8f6cf8ced66861d01e6bccaf17d6ace709c3c1e3a180f89a145d8d4d98beecf8ba52cbdde6d41129238df73b0c364e9997ae4f225587a6d85c11
7
+ data.tar.gz: 4f42ad5ca624705aa6139ebf6b698ede8350fe94dd30fafbce662a433c6a898fd16ee47d22710ca648999c4bbd4ef9457458acc44c9c35f0d22823e84276d2bd
@@ -1,16 +1,26 @@
1
1
  /* eslint-disable no-console */
2
2
  import React from 'react'
3
- import moment from 'moment'
4
3
  import { fireEvent, render, screen, waitFor, within } from '../utilities/test-utils'
5
4
 
6
5
  import DatePicker from './_date_picker'
6
+ import DateTime from "../pb_kit/dateTime.ts"
7
7
  import { getTimezoneText } from './plugins/timeSelect'
8
8
 
9
-
10
-
11
9
  jest.setSystemTime(new Date('01/01/2020'));
12
10
  const DEFAULT_DATE = new Date()
13
11
 
12
+ const formatDate = (date) => {
13
+ const month = (date.getMonth() + 1).toString().padStart(2, "0")
14
+ const day = (date.getDate()).toString().padStart(2, "0")
15
+ const year = date.getFullYear()
16
+
17
+ return `${month}/${day}/${year}`
18
+ }
19
+
20
+ Date.prototype.formatDate = function () {
21
+ return formatDate(this)
22
+ }
23
+
14
24
  describe('DatePicker Kit', () => {
15
25
  beforeEach(() => {
16
26
  jest.spyOn(console, 'error').mockImplementation(() => { });
@@ -158,6 +168,7 @@ describe('DatePicker Kit', () => {
158
168
  expect(input).toHaveValue('01/01/2020 at 12:00 PM')
159
169
  })
160
170
  })
171
+
161
172
  test('shows DatePicker QuickPick dropdown and adds correct date to input', async () => {
162
173
  const testId = 'datepicker-quick-pick'
163
174
  render(
@@ -197,10 +208,10 @@ describe('DatePicker Kit', () => {
197
208
  )
198
209
 
199
210
  await waitFor(() => {
200
- expect(input).toHaveValue(moment().startOf('year').format('MM/DD/YYYY') + " to " + moment().endOf('year').format('MM/DD/YYYY'))
211
+ expect(input).toHaveValue(DateTime.getYearStartDate(new Date()).formatDate() + " to " + DateTime.getYearEndDate(new Date()).formatDate())
201
212
  })
202
-
203
213
  })
214
+
204
215
  test('shows DatePicker QuickPick ranges ending today', async () => {
205
216
  const testId = 'datepicker-quick-pick-ends-today'
206
217
  render(
@@ -225,7 +236,7 @@ describe('DatePicker Kit', () => {
225
236
  cancelable: true,
226
237
  }),
227
238
  )
228
-
239
+
229
240
  const thisYear = within(kit).getByText('This year')
230
241
 
231
242
  fireEvent(
@@ -237,8 +248,7 @@ describe('DatePicker Kit', () => {
237
248
  )
238
249
 
239
250
  await waitFor(() => {
240
- expect(input).toHaveValue(moment().startOf('year').format('MM/DD/YYYY') + " to " + moment().format('MM/DD/YYYY'))
251
+ expect(input).toHaveValue(DateTime.getYearStartDate(new Date()).formatDate() + " to " + new Date().formatDate())
241
252
  })
242
-
243
253
  })
244
254
  })
@@ -1,4 +1,4 @@
1
- import moment from 'moment'
1
+ import DateTime from '../../pb_kit/dateTime';
2
2
 
3
3
  type FpTypes = {
4
4
  setDate: (arg0: any, arg1: boolean) => void,
@@ -23,37 +23,44 @@ let activeLabel = ""
23
23
 
24
24
  const quickPickPlugin = (thisRangesEndToday: boolean) => {
25
25
  return function (fp: FpTypes & any): any {
26
- const thisWeekEndDate = thisRangesEndToday ? new Date() : moment().endOf('isoWeek').toDate()
27
- const thisMonthEndDate = thisRangesEndToday ? new Date() : moment().endOf('month').toDate()
28
- const thisQuarterEndDate = thisRangesEndToday ? new Date() : moment().endOf('quarter').toDate()
29
- const thisYearEndDate = thisRangesEndToday ? new Date() : moment().endOf('year').toDate()
26
+ const today = new Date()
27
+ const yesterday = DateTime.getYesterdayDate(new Date())
28
+
29
+ const thisWeekStartDate = DateTime.getFirstDayOfWeek(new Date())
30
+ const thisWeekEndDate = thisRangesEndToday ? new Date() : DateTime.getLastDayOfWeek(new Date())
31
+ const lastWeekStartDate = DateTime.getPreviousWeekStartDate(new Date())
32
+ const lastWeekEndDate = DateTime.getPreviousWeekEndDate(new Date())
33
+
34
+ const thisMonthStartDate = DateTime.getMonthStartDate(new Date())
35
+ const thisMonthEndDate = thisRangesEndToday ? new Date() : DateTime.getMonthEndDate(new Date())
36
+ const lastMonthStartDate = DateTime.getPreviousMonthStartDate(new Date())
37
+ const lastMonthEndDate = DateTime.getPreviousMonthEndDate(new Date())
38
+
39
+ const thisQuarterStartDate = DateTime.getQuarterStartDate(new Date())
40
+ const thisQuarterEndDate = thisRangesEndToday ? new Date() : DateTime.getQuarterEndDate(new Date())
41
+ const lastQuarterStartDate = DateTime.getPreviousQuarterStartDate(new Date())
42
+ const lastQuarterEndDate = DateTime.getPreviousQuarterEndDate(new Date())
43
+
44
+ const thisYearStartDate = DateTime.getYearStartDate(new Date())
45
+ const thisYearEndDate = thisRangesEndToday ? new Date() : DateTime.getYearEndDate(new Date())
46
+ const lastYearStartDate = DateTime.getPreviousYearStartDate(new Date())
47
+ const lastYearEndDate = DateTime.getPreviousYearEndDate(new Date())
30
48
 
31
49
  // variable that holds the ranges available
32
50
  const ranges = {
33
- 'Today': [new Date(), new Date()],
34
- 'Yesterday': [moment().subtract(1, 'days').toDate(), moment().subtract(1, 'days').toDate()],
35
- 'This week': [moment().startOf('isoWeek').toDate(), thisWeekEndDate],
36
- 'This month': [moment().startOf('month').toDate(), thisMonthEndDate],
37
- 'This quarter': [moment().startOf('quarter').toDate(), thisQuarterEndDate],
38
- 'This year': [moment().startOf('year').toDate(), thisYearEndDate],
39
- 'Last week': [
40
- moment().subtract(1, 'week').startOf('isoWeek').toDate(),
41
- moment().subtract(1, 'week').endOf('isoWeek').toDate()
42
- ],
43
- 'Last month': [
44
- moment().subtract(1, 'month').startOf('month').toDate(),
45
- moment().subtract(1, 'month').endOf('month').toDate()
46
- ],
47
- 'Last quarter': [
48
- moment().subtract(1, 'quarter').startOf('quarter').toDate(),
49
- moment().subtract(1, 'quarter').endOf('quarter').toDate()
50
- ],
51
- 'Last year': [
52
- moment().subtract(1, 'year').startOf('year').toDate(),
53
- moment().subtract(1, 'year').endOf('year').toDate()
54
- ]
51
+ 'Today': [today, today],
52
+ 'Yesterday': [yesterday, yesterday],
53
+ 'This week': [thisWeekStartDate, thisWeekEndDate],
54
+ 'This month': [thisMonthStartDate, thisMonthEndDate],
55
+ 'This quarter': [thisQuarterStartDate, thisQuarterEndDate],
56
+ 'This year': [thisYearStartDate, thisYearEndDate],
57
+ 'Last week': [lastWeekStartDate, lastWeekEndDate],
58
+ 'Last month': [lastMonthStartDate, lastMonthEndDate],
59
+ 'Last quarter': [lastQuarterStartDate, lastQuarterEndDate],
60
+ 'Last year': [lastYearStartDate, lastYearEndDate]
55
61
  }
56
- //creating the ul element for the nav dropdown and giving it classnames
62
+
63
+ // creating the ul element for the nav dropdown and giving it classnames
57
64
  const rangesNav = document.createElement('ul');
58
65
 
59
66
  // creating the pluginData object that will hold the properties of this plugin
@@ -64,11 +71,11 @@ const quickPickPlugin = (thisRangesEndToday: boolean) => {
64
71
  };
65
72
 
66
73
  /**
67
- * @param {string} label
68
- * @returns HTML Element
69
- */
74
+ * @param {string} label
75
+ * @returns HTML Element
76
+ */
70
77
 
71
- //function for creating the range buttons in the nav
78
+ // function for creating the range buttons in the nav
72
79
  const addRangeButton = (label: string) => {
73
80
 
74
81
  // creating new elements to mimick selectable card component
@@ -88,7 +95,7 @@ const quickPickPlugin = (thisRangesEndToday: boolean) => {
88
95
  // append the li item to the ul rangeNav prop
89
96
  pluginData.rangesNav.appendChild(item);
90
97
 
91
- // return the ranges buton prop
98
+ // return the ranges button prop
92
99
  return pluginData.rangesButtons[label];
93
100
  };
94
101
 
@@ -98,7 +105,7 @@ const quickPickPlugin = (thisRangesEndToday: boolean) => {
98
105
  if (current) {
99
106
  current.classList.remove('active');
100
107
  }
101
-
108
+
102
109
  if (selectedDates.length > 0 && activeLabel) {
103
110
  pluginData.rangesButtons[activeLabel].classList.add('active');
104
111
  }
@@ -109,16 +116,15 @@ const quickPickPlugin = (thisRangesEndToday: boolean) => {
109
116
  selectedDates[1].toDateString() === pluginData.ranges[activeLabel][1].toDateString()
110
117
  }
111
118
 
112
-
113
119
  return {
114
- // onReady is a hook from flatpickr that runs when calender is in a ready state
120
+ // onReady is a hook from flatpickr that runs when calendar is in a ready state
115
121
  onReady(selectedDates: Array<Date>) {
116
122
  // loop through the ranges and create an anchor tag for each range and add an event listener to set the date when user clicks on a date range
117
123
  for (const [label, range] of Object.entries(pluginData.ranges)) {
118
124
  addRangeButton(label).addEventListener('click', function () {
119
125
 
120
- const start = moment(range[0]).toDate();
121
- const end = moment(range[1]).toDate();
126
+ const start = new Date(range[0]);
127
+ const end = new Date(range[1]);
122
128
 
123
129
  if (!start) {
124
130
  fp.clear();
@@ -170,4 +176,4 @@ const quickPickPlugin = (thisRangesEndToday: boolean) => {
170
176
  };
171
177
  }
172
178
 
173
- export default quickPickPlugin;
179
+ export default quickPickPlugin;
@@ -116,7 +116,6 @@ export const toIso = (newDate: Date | string): string => {
116
116
  }
117
117
 
118
118
  export const fromNow = (newDate: Date | string): string => {
119
-
120
119
  const startDate = formatDate(newDate).getTime()
121
120
  const endDate = new Date().getTime()
122
121
  const elapsedTime = endDate - startDate
@@ -154,6 +153,193 @@ export const toCustomFormat = (newDate: Date | string, format = 'month_day'): st
154
153
  }
155
154
  }
156
155
 
156
+ // For quickPick.tsx
157
+ // Yesterday
158
+ export const getYesterdayDate = (newDate: Date | string): Date => {
159
+ const today = formatDate(newDate)
160
+ const yesterday = new Date()
161
+ yesterday.setDate(today.getDate() - 1)
162
+
163
+ return yesterday
164
+ }
165
+
166
+ // Weeks
167
+ export const getFirstDayOfWeek = (newDate: Date | string): Date => {
168
+ const today = formatDate(newDate)
169
+ const dayOfWeek = today.getDay()
170
+ // Replicate Moment.js: Start of week (Monday) has a time of 00:00:00
171
+ const firstDayOfWeek = new Date(today.setHours(0, 0, 0))
172
+ const isSunday = dayOfWeek === 0
173
+
174
+ const daysToSubtract = isSunday ? 6 : (dayOfWeek - 1)
175
+ firstDayOfWeek.setDate(today.getDate() - daysToSubtract)
176
+
177
+ return firstDayOfWeek
178
+ }
179
+
180
+ export const getLastDayOfWeek = (newDate: Date | string): Date => {
181
+ const today = formatDate(newDate)
182
+ const dayOfWeek = today.getDay()
183
+ // Replicate Moment.js: End of week (Sunday) has a time of 23:59:59
184
+ const lastDayOfWeek = new Date(today.setHours(23, 59, 59, 0))
185
+ const isSunday = dayOfWeek === 0
186
+
187
+ const daysToAdd = isSunday ? 0 : (7 - dayOfWeek)
188
+ lastDayOfWeek.setDate(today.getDate() + daysToAdd)
189
+
190
+ return lastDayOfWeek
191
+ }
192
+
193
+ export const getPreviousWeekStartDate = (newDate: Date | string): Date => {
194
+ const firstDayOfWeek = getFirstDayOfWeek(newDate)
195
+ const firstDayOfPreviousWeek = new Date(
196
+ firstDayOfWeek.getFullYear(),
197
+ firstDayOfWeek.getMonth(),
198
+ firstDayOfWeek.getDate() - 7
199
+ )
200
+
201
+ return firstDayOfPreviousWeek
202
+ }
203
+
204
+ export const getPreviousWeekEndDate = (newDate: Date | string): Date => {
205
+ const lastDayOfWeek = getLastDayOfWeek(newDate)
206
+ const lastDayOfPreviousWeek = new Date(
207
+ lastDayOfWeek.getFullYear(),
208
+ lastDayOfWeek.getMonth(),
209
+ lastDayOfWeek.getDate() - 7,
210
+ lastDayOfWeek.getHours(),
211
+ lastDayOfWeek.getMinutes(),
212
+ lastDayOfWeek.getSeconds()
213
+ )
214
+
215
+ return lastDayOfPreviousWeek
216
+ }
217
+
218
+ // Months
219
+ export const getMonthStartDate = (newDate: Date | string): Date => {
220
+ const date = formatDate(newDate)
221
+ const firstDayOfMonth = new Date(date.getFullYear(), date.getMonth(), 1)
222
+
223
+ return firstDayOfMonth
224
+ }
225
+
226
+ export const getMonthEndDate = (newDate: Date | string): Date => {
227
+ const date = formatDate(newDate)
228
+ // Replicate Moment.js: End of month has a time of 23:59:59
229
+ const lastDayOfMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0, 23, 59, 59)
230
+
231
+ return lastDayOfMonth
232
+ }
233
+
234
+ export const getPreviousMonthStartDate = (newDate: Date | string): Date => {
235
+ const firstDayOfMonth = getMonthStartDate(newDate)
236
+ const firstDayOfPreviousMonth = new Date(
237
+ firstDayOfMonth.getFullYear(),
238
+ firstDayOfMonth.getMonth() - 1,
239
+ firstDayOfMonth.getDate()
240
+ )
241
+
242
+ return firstDayOfPreviousMonth
243
+ }
244
+
245
+ export const getPreviousMonthEndDate = (newDate: Date | string): Date => {
246
+ const lastDayOfMonth = getMonthEndDate(newDate)
247
+ const lastDayOfPreviousMonth = new Date(
248
+ lastDayOfMonth.getFullYear(),
249
+ lastDayOfMonth.getMonth() - 1,
250
+ lastDayOfMonth.getDate(),
251
+ lastDayOfMonth.getHours(),
252
+ lastDayOfMonth.getMinutes(),
253
+ lastDayOfMonth.getSeconds()
254
+ )
255
+
256
+ return lastDayOfPreviousMonth
257
+ }
258
+
259
+ // Quarters
260
+ export const getQuarterStartDate = (newDate: Date | string): Date => {
261
+ const date = formatDate(newDate)
262
+ const quarter = Math.floor(date.getMonth() / 3)
263
+ const startOfQuarter = new Date(date.getFullYear(), quarter * 3, 1)
264
+
265
+ return startOfQuarter
266
+ }
267
+
268
+ export const getQuarterEndDate = (newDate: Date | string): Date => {
269
+ const date = formatDate(newDate)
270
+ const quarter = Math.floor(date.getMonth() / 3)
271
+ const startOfNextQuarter = new Date(date.getFullYear(), (quarter + 1) * 3, 1)
272
+ // Replicate Moment.js: End of quarter has a time of 23:59:59
273
+ const endOfQuarter = new Date(startOfNextQuarter.getTime() - 1)
274
+
275
+ return endOfQuarter
276
+ }
277
+
278
+ export const getPreviousQuarterStartDate = (newDate: Date | string): Date => {
279
+ const startOfQuarter = getQuarterStartDate(newDate)
280
+ const firstDayOfPreviousQuarter = new Date(
281
+ startOfQuarter.getFullYear(),
282
+ startOfQuarter.getMonth() - 3,
283
+ startOfQuarter.getDate()
284
+ )
285
+
286
+ return firstDayOfPreviousQuarter
287
+ }
288
+
289
+ export const getPreviousQuarterEndDate = (newDate: Date | string): Date => {
290
+ const endOfQuarter = getQuarterEndDate(newDate)
291
+ const lastDayOfPreviousQuarter = new Date(
292
+ endOfQuarter.getFullYear(),
293
+ endOfQuarter.getMonth() - 3,
294
+ endOfQuarter.getDate(),
295
+ endOfQuarter.getHours(),
296
+ endOfQuarter.getMinutes(),
297
+ endOfQuarter.getSeconds()
298
+ )
299
+
300
+ return lastDayOfPreviousQuarter
301
+ }
302
+
303
+ // Years
304
+ export const getYearStartDate = (newDate: Date | string): Date => {
305
+ const date = formatDate(newDate)
306
+ const startOfYear = new Date(date.getFullYear(), 0, 1)
307
+
308
+ return startOfYear
309
+ }
310
+
311
+ export const getYearEndDate = (newDate: Date | string): Date => {
312
+ const date = formatDate(newDate)
313
+ const endOfYear = new Date(date.getFullYear(), 11, 31, 23, 59, 59)
314
+
315
+ return endOfYear
316
+ }
317
+
318
+ export const getPreviousYearStartDate = (newDate: Date | string): Date => {
319
+ const startOfYear = getYearStartDate(newDate)
320
+ const firstDayOfPreviousYear = new Date(
321
+ startOfYear.getFullYear() - 1,
322
+ startOfYear.getMonth(),
323
+ startOfYear.getDate()
324
+ )
325
+
326
+ return firstDayOfPreviousYear
327
+ }
328
+
329
+ export const getPreviousYearEndDate = (newDate: Date | string): Date => {
330
+ const endOfYear = getYearEndDate(newDate)
331
+ const lastDayOfPreviousYear = new Date(
332
+ endOfYear.getFullYear() - 1,
333
+ endOfYear.getMonth(),
334
+ endOfYear.getDate(),
335
+ endOfYear.getHours(),
336
+ endOfYear.getMinutes(),
337
+ endOfYear.getSeconds()
338
+ )
339
+
340
+ return lastDayOfPreviousYear
341
+ }
342
+
157
343
  export default {
158
344
  toMinute,
159
345
  toHour,
@@ -170,4 +356,21 @@ export default {
170
356
  toIso,
171
357
  fromNow,
172
358
  toCustomFormat,
359
+ getYesterdayDate,
360
+ getFirstDayOfWeek,
361
+ getLastDayOfWeek,
362
+ getPreviousWeekStartDate,
363
+ getPreviousWeekEndDate,
364
+ getMonthStartDate,
365
+ getMonthEndDate,
366
+ getPreviousMonthStartDate,
367
+ getPreviousMonthEndDate,
368
+ getQuarterStartDate,
369
+ getQuarterEndDate,
370
+ getPreviousQuarterStartDate,
371
+ getPreviousQuarterEndDate,
372
+ getYearStartDate,
373
+ getYearEndDate,
374
+ getPreviousYearStartDate,
375
+ getPreviousYearEndDate
173
376
  }