playbook_ui 12.39.0.pre.alpha.salesbookmismatchingdate1114 → 12.39.0.pre.alpha.salesbookmismatchingdate1117

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: 53e1efed14125432de973864540db434bd15d7ca728af5bfd7facb459c42f3a5
4
- data.tar.gz: 8ea21f9bdf376162e82ab98a9d14b3f186e8438d81a979faa663ffad1f3b57f7
3
+ metadata.gz: 6650ac3fb53a1c1a6d57ea4d4c08d61741e60c7aedd7149994b5f6a239a669e5
4
+ data.tar.gz: 0b0a15f6fe4e1d400bc78b5b74638c3ac6be8c9ef0804773edde1a3711fef286
5
5
  SHA512:
6
- metadata.gz: 5b78591f4053df9f189134735a3689c5e010f823220d0217c65f222240a1aed961f8cdb79df3f91e771f7336717031dfd2594064388e3f9478549d0281f78724
7
- data.tar.gz: 93d06173adcc6db7672b2580de009a31d530c0135802524b0d5514d09939737230c38dd845f47c862a9be503eb6c42916372498afc5481d54f7c2afe495f6644
6
+ metadata.gz: 5eb5a4c90549d5f7f4dafb4455ff212573749b54c9734b09daa79a6a2d924587b820a8883fc6e340e31571515b935476e872344207b6e88b7be4f98ac1f25815
7
+ data.tar.gz: 8986120162f91c579c0574dd70ef13fb0bc190b687548edd456b33e2d9ff4447c9b122bf431719376390a2dc49e21d6c8714c4b53da362204b908b01ccd2b3d0
@@ -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;
@@ -71,13 +71,13 @@ export const toMonthNum = (newDate: Date | string): number => {
71
71
  }
72
72
 
73
73
  export const toYear = (newDate: Date | string, timeZone?: string): number => {
74
- if (timeZone) {
75
- const date = new Date(formatDate(newDate).toLocaleString(undefined, { timeZone }));
76
- return date.getFullYear()
77
- } else {
78
- const date = formatDate(newDate)
79
- return date.getFullYear()
80
- }
74
+ if (timeZone) {
75
+ const date = new Date(formatDate(newDate).toLocaleString(undefined, { timeZone }));
76
+ return date.getFullYear()
77
+ } else {
78
+ const date = formatDate(newDate)
79
+ return date.getFullYear()
80
+ }
81
81
  }
82
82
 
83
83
  export const toTime = (newDate: Date | string, timeZone?: string): string => {
@@ -121,11 +121,12 @@ export const toIso = (newDate: Date | string): string => {
121
121
  }
122
122
 
123
123
  export const fromNow = (newDate: Date | string): string => {
124
-
125
124
  const startDate = formatDate(newDate).getTime()
126
125
  const endDate = new Date().getTime()
127
126
  const elapsedTime = endDate - startDate
128
- let elapsedTimeString = `${Math.round(elapsedTime / (365.25 * 24 * 60 * 60 * 1000))} years ago.`; // 730+ days
127
+ let elapsedTimeString = `${Math.round(elapsedTime / (365.25 * 24 * 60 * 60 * 1000))} years ago`; // 730+ days
128
+
129
+ const MILLISECONDS_IN_A_MONTH = 30.44 * 24 * 60 * 60 * 1000
129
130
 
130
131
  const elapsedTimeData = [
131
132
  { min: 0, max: 44999, value: "a few seconds ago" }, // 0-44 seconds
@@ -136,7 +137,7 @@ export const fromNow = (newDate: Date | string): string => {
136
137
  { min: 75700000, max: 172899999, value: "a day ago" }, // 22-48 hours
137
138
  { min: 172900000, max: 2169999999, value: `${Math.round(elapsedTime / 86400000)} days ago`}, // 2-25 days
138
139
  { min: 2170000000, max: 5184999999, value: "a month ago"}, // 26-60 days
139
- { min: 5185000000, max: 27561699999, value: `${Math.round(elapsedTime / 30.44 * 24 * 60 * 60 * 1000)} months ago`}, // 60-319 days
140
+ { min: 5185000000, max: 27561699999, value: `${Math.round(elapsedTime / MILLISECONDS_IN_A_MONTH)} months ago`}, // 60-319 days
140
141
  { min: 27561700000, max: 63072999999, value: "a year ago"}, // 320-730 days
141
142
  ];
142
143
 
@@ -159,6 +160,193 @@ export const toCustomFormat = (newDate: Date | string, format = 'month_day'): st
159
160
  }
160
161
  }
161
162
 
163
+ // For quickPick.tsx
164
+ // Yesterday
165
+ export const getYesterdayDate = (newDate: Date | string): Date => {
166
+ const today = formatDate(newDate)
167
+ const yesterday = new Date()
168
+ yesterday.setDate(today.getDate() - 1)
169
+
170
+ return yesterday
171
+ }
172
+
173
+ // Weeks
174
+ export const getFirstDayOfWeek = (newDate: Date | string): Date => {
175
+ const today = formatDate(newDate)
176
+ const dayOfWeek = today.getDay()
177
+ // Replicate Moment.js: Start of week (Monday) has a time of 00:00:00
178
+ const firstDayOfWeek = new Date(today.setHours(0, 0, 0))
179
+ const isSunday = dayOfWeek === 0
180
+
181
+ const daysToSubtract = isSunday ? 6 : (dayOfWeek - 1)
182
+ firstDayOfWeek.setDate(today.getDate() - daysToSubtract)
183
+
184
+ return firstDayOfWeek
185
+ }
186
+
187
+ export const getLastDayOfWeek = (newDate: Date | string): Date => {
188
+ const today = formatDate(newDate)
189
+ const dayOfWeek = today.getDay()
190
+ // Replicate Moment.js: End of week (Sunday) has a time of 23:59:59
191
+ const lastDayOfWeek = new Date(today.setHours(23, 59, 59, 0))
192
+ const isSunday = dayOfWeek === 0
193
+
194
+ const daysToAdd = isSunday ? 0 : (7 - dayOfWeek)
195
+ lastDayOfWeek.setDate(today.getDate() + daysToAdd)
196
+
197
+ return lastDayOfWeek
198
+ }
199
+
200
+ export const getPreviousWeekStartDate = (newDate: Date | string): Date => {
201
+ const firstDayOfWeek = getFirstDayOfWeek(newDate)
202
+ const firstDayOfPreviousWeek = new Date(
203
+ firstDayOfWeek.getFullYear(),
204
+ firstDayOfWeek.getMonth(),
205
+ firstDayOfWeek.getDate() - 7
206
+ )
207
+
208
+ return firstDayOfPreviousWeek
209
+ }
210
+
211
+ export const getPreviousWeekEndDate = (newDate: Date | string): Date => {
212
+ const lastDayOfWeek = getLastDayOfWeek(newDate)
213
+ const lastDayOfPreviousWeek = new Date(
214
+ lastDayOfWeek.getFullYear(),
215
+ lastDayOfWeek.getMonth(),
216
+ lastDayOfWeek.getDate() - 7,
217
+ lastDayOfWeek.getHours(),
218
+ lastDayOfWeek.getMinutes(),
219
+ lastDayOfWeek.getSeconds()
220
+ )
221
+
222
+ return lastDayOfPreviousWeek
223
+ }
224
+
225
+ // Months
226
+ export const getMonthStartDate = (newDate: Date | string): Date => {
227
+ const date = formatDate(newDate)
228
+ const firstDayOfMonth = new Date(date.getFullYear(), date.getMonth(), 1)
229
+
230
+ return firstDayOfMonth
231
+ }
232
+
233
+ export const getMonthEndDate = (newDate: Date | string): Date => {
234
+ const date = formatDate(newDate)
235
+ // Replicate Moment.js: End of month has a time of 23:59:59
236
+ const lastDayOfMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0, 23, 59, 59)
237
+
238
+ return lastDayOfMonth
239
+ }
240
+
241
+ export const getPreviousMonthStartDate = (newDate: Date | string): Date => {
242
+ const firstDayOfMonth = getMonthStartDate(newDate)
243
+ const firstDayOfPreviousMonth = new Date(
244
+ firstDayOfMonth.getFullYear(),
245
+ firstDayOfMonth.getMonth() - 1,
246
+ firstDayOfMonth.getDate()
247
+ )
248
+
249
+ return firstDayOfPreviousMonth
250
+ }
251
+
252
+ export const getPreviousMonthEndDate = (newDate: Date | string): Date => {
253
+ const lastDayOfMonth = getMonthEndDate(newDate)
254
+ const lastDayOfPreviousMonth = new Date(
255
+ lastDayOfMonth.getFullYear(),
256
+ lastDayOfMonth.getMonth() - 1,
257
+ lastDayOfMonth.getDate(),
258
+ lastDayOfMonth.getHours(),
259
+ lastDayOfMonth.getMinutes(),
260
+ lastDayOfMonth.getSeconds()
261
+ )
262
+
263
+ return lastDayOfPreviousMonth
264
+ }
265
+
266
+ // Quarters
267
+ export const getQuarterStartDate = (newDate: Date | string): Date => {
268
+ const date = formatDate(newDate)
269
+ const quarter = Math.floor(date.getMonth() / 3)
270
+ const startOfQuarter = new Date(date.getFullYear(), quarter * 3, 1)
271
+
272
+ return startOfQuarter
273
+ }
274
+
275
+ export const getQuarterEndDate = (newDate: Date | string): Date => {
276
+ const date = formatDate(newDate)
277
+ const quarter = Math.floor(date.getMonth() / 3)
278
+ const startOfNextQuarter = new Date(date.getFullYear(), (quarter + 1) * 3, 1)
279
+ // Replicate Moment.js: End of quarter has a time of 23:59:59
280
+ const endOfQuarter = new Date(startOfNextQuarter.getTime() - 1)
281
+
282
+ return endOfQuarter
283
+ }
284
+
285
+ export const getPreviousQuarterStartDate = (newDate: Date | string): Date => {
286
+ const startOfQuarter = getQuarterStartDate(newDate)
287
+ const firstDayOfPreviousQuarter = new Date(
288
+ startOfQuarter.getFullYear(),
289
+ startOfQuarter.getMonth() - 3,
290
+ startOfQuarter.getDate()
291
+ )
292
+
293
+ return firstDayOfPreviousQuarter
294
+ }
295
+
296
+ export const getPreviousQuarterEndDate = (newDate: Date | string): Date => {
297
+ const endOfQuarter = getQuarterEndDate(newDate)
298
+ const lastDayOfPreviousQuarter = new Date(
299
+ endOfQuarter.getFullYear(),
300
+ endOfQuarter.getMonth() - 3,
301
+ endOfQuarter.getDate(),
302
+ endOfQuarter.getHours(),
303
+ endOfQuarter.getMinutes(),
304
+ endOfQuarter.getSeconds()
305
+ )
306
+
307
+ return lastDayOfPreviousQuarter
308
+ }
309
+
310
+ // Years
311
+ export const getYearStartDate = (newDate: Date | string): Date => {
312
+ const date = formatDate(newDate)
313
+ const startOfYear = new Date(date.getFullYear(), 0, 1)
314
+
315
+ return startOfYear
316
+ }
317
+
318
+ export const getYearEndDate = (newDate: Date | string): Date => {
319
+ const date = formatDate(newDate)
320
+ const endOfYear = new Date(date.getFullYear(), 11, 31, 23, 59, 59)
321
+
322
+ return endOfYear
323
+ }
324
+
325
+ export const getPreviousYearStartDate = (newDate: Date | string): Date => {
326
+ const startOfYear = getYearStartDate(newDate)
327
+ const firstDayOfPreviousYear = new Date(
328
+ startOfYear.getFullYear() - 1,
329
+ startOfYear.getMonth(),
330
+ startOfYear.getDate()
331
+ )
332
+
333
+ return firstDayOfPreviousYear
334
+ }
335
+
336
+ export const getPreviousYearEndDate = (newDate: Date | string): Date => {
337
+ const endOfYear = getYearEndDate(newDate)
338
+ const lastDayOfPreviousYear = new Date(
339
+ endOfYear.getFullYear() - 1,
340
+ endOfYear.getMonth(),
341
+ endOfYear.getDate(),
342
+ endOfYear.getHours(),
343
+ endOfYear.getMinutes(),
344
+ endOfYear.getSeconds()
345
+ )
346
+
347
+ return lastDayOfPreviousYear
348
+ }
349
+
162
350
  export default {
163
351
  toMinute,
164
352
  toHour,
@@ -175,4 +363,21 @@ export default {
175
363
  toIso,
176
364
  fromNow,
177
365
  toCustomFormat,
366
+ getYesterdayDate,
367
+ getFirstDayOfWeek,
368
+ getLastDayOfWeek,
369
+ getPreviousWeekStartDate,
370
+ getPreviousWeekEndDate,
371
+ getMonthStartDate,
372
+ getMonthEndDate,
373
+ getPreviousMonthStartDate,
374
+ getPreviousMonthEndDate,
375
+ getQuarterStartDate,
376
+ getQuarterEndDate,
377
+ getPreviousQuarterStartDate,
378
+ getPreviousQuarterEndDate,
379
+ getYearStartDate,
380
+ getYearEndDate,
381
+ getPreviousYearStartDate,
382
+ getPreviousYearEndDate
178
383
  }