Skip to content

Commit

Permalink
[PLAY-932] Remove Moment Packages and Update Date Picker's Quick Pick…
Browse files Browse the repository at this point in the history
… Variants (#2707)

**What does this PR do?**
- [Runway](https://nitro.powerhrg.com/runway/backlog_items/PLAY-932)
- Remove `moment` packages and usage within `quickPick.tsx` and
`date_picker.test.js`
- `ESLint`: Remove "moment" as restricted global variable
- Create implementations of the date range functionality that `moment`
used to provide:
  -  ex. Yesterday, This Week, This Quarter, Last Week, ...and so on

**How to test?** Steps to confirm the desired behavior:
1. Go to the docs page
2. Go to the Date Picker kit and test both Quick Pick variants

#### Checklist:
- [X] **LABELS** Add a label: `enhancement`, `bug`, `improvement`, `new
kit`, `deprecated`, or `breaking`. See [Changelog &
Labels](https://github.com/powerhome/playbook/wiki/Changelog-&-Labels)
for details.
- [X] **DEPLOY** I have added the `milano` label to show I'm ready for a
review.
- [X] **TESTS** I have added test coverage to my code.

---------

Co-authored-by: Nida Ghuman <[email protected]>
  • Loading branch information
gavhuang and nidaqg authored Aug 22, 2023
1 parent d9ca78a commit 350784e
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 71 deletions.
2 changes: 1 addition & 1 deletion playbook/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"rules": {
"jsx-control-statements/jsx-jcs-no-undef": 1,
"no-console": 1,
"no-restricted-globals": [1, { "name": "moment" }],
"no-restricted-globals": 1,
"no-use-before-define": [1, { "functions": true, "classes": true }],
"react/forbid-prop-types": 2,
"react/jsx-boolean-value": 2,
Expand Down
26 changes: 18 additions & 8 deletions playbook/app/pb_kits/playbook/pb_date_picker/date_picker.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
/* eslint-disable no-console */
import React from 'react'
import moment from 'moment'
import { fireEvent, render, screen, waitFor, within } from '../utilities/test-utils'

import DatePicker from './_date_picker'
import DateTime from "../pb_kit/dateTime.ts"
import { getTimezoneText } from './plugins/timeSelect'



jest.setSystemTime(new Date('01/01/2020'));
const DEFAULT_DATE = new Date()

const formatDate = (date) => {
const month = (date.getMonth() + 1).toString().padStart(2, "0")
const day = (date.getDate()).toString().padStart(2, "0")
const year = date.getFullYear()

return `${month}/${day}/${year}`
}

Date.prototype.formatDate = function () {
return formatDate(this)
}

describe('DatePicker Kit', () => {
beforeEach(() => {
jest.spyOn(console, 'error').mockImplementation(() => { });
Expand Down Expand Up @@ -158,6 +168,7 @@ describe('DatePicker Kit', () => {
expect(input).toHaveValue('01/01/2020 at 12:00 PM')
})
})

test('shows DatePicker QuickPick dropdown and adds correct date to input', async () => {
const testId = 'datepicker-quick-pick'
render(
Expand Down Expand Up @@ -197,10 +208,10 @@ describe('DatePicker Kit', () => {
)

await waitFor(() => {
expect(input).toHaveValue(moment().startOf('year').format('MM/DD/YYYY') + " to " + moment().endOf('year').format('MM/DD/YYYY'))
expect(input).toHaveValue(DateTime.getYearStartDate(new Date()).formatDate() + " to " + DateTime.getYearEndDate(new Date()).formatDate())
})

})

test('shows DatePicker QuickPick ranges ending today', async () => {
const testId = 'datepicker-quick-pick-ends-today'
render(
Expand All @@ -225,7 +236,7 @@ describe('DatePicker Kit', () => {
cancelable: true,
}),
)

const thisYear = within(kit).getByText('This year')

fireEvent(
Expand All @@ -237,8 +248,7 @@ describe('DatePicker Kit', () => {
)

await waitFor(() => {
expect(input).toHaveValue(moment().startOf('year').format('MM/DD/YYYY') + " to " + moment().format('MM/DD/YYYY'))
expect(input).toHaveValue(DateTime.getYearStartDate(new Date()).formatDate() + " to " + new Date().formatDate())
})

})
})
84 changes: 45 additions & 39 deletions playbook/app/pb_kits/playbook/pb_date_picker/plugins/quickPick.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import moment from 'moment'
import DateTime from '../../pb_kit/dateTime';

type FpTypes = {
setDate: (arg0: any, arg1: boolean) => void,
Expand All @@ -23,37 +23,44 @@ let activeLabel = ""

const quickPickPlugin = (thisRangesEndToday: boolean) => {
return function (fp: FpTypes & any): any {
const thisWeekEndDate = thisRangesEndToday ? new Date() : moment().endOf('isoWeek').toDate()
const thisMonthEndDate = thisRangesEndToday ? new Date() : moment().endOf('month').toDate()
const thisQuarterEndDate = thisRangesEndToday ? new Date() : moment().endOf('quarter').toDate()
const thisYearEndDate = thisRangesEndToday ? new Date() : moment().endOf('year').toDate()
const today = new Date()
const yesterday = DateTime.getYesterdayDate(new Date())

const thisWeekStartDate = DateTime.getFirstDayOfWeek(new Date())
const thisWeekEndDate = thisRangesEndToday ? new Date() : DateTime.getLastDayOfWeek(new Date())
const lastWeekStartDate = DateTime.getPreviousWeekStartDate(new Date())
const lastWeekEndDate = DateTime.getPreviousWeekEndDate(new Date())

const thisMonthStartDate = DateTime.getMonthStartDate(new Date())
const thisMonthEndDate = thisRangesEndToday ? new Date() : DateTime.getMonthEndDate(new Date())
const lastMonthStartDate = DateTime.getPreviousMonthStartDate(new Date())
const lastMonthEndDate = DateTime.getPreviousMonthEndDate(new Date())

const thisQuarterStartDate = DateTime.getQuarterStartDate(new Date())
const thisQuarterEndDate = thisRangesEndToday ? new Date() : DateTime.getQuarterEndDate(new Date())
const lastQuarterStartDate = DateTime.getPreviousQuarterStartDate(new Date())
const lastQuarterEndDate = DateTime.getPreviousQuarterEndDate(new Date())

const thisYearStartDate = DateTime.getYearStartDate(new Date())
const thisYearEndDate = thisRangesEndToday ? new Date() : DateTime.getYearEndDate(new Date())
const lastYearStartDate = DateTime.getPreviousYearStartDate(new Date())
const lastYearEndDate = DateTime.getPreviousYearEndDate(new Date())

// variable that holds the ranges available
const ranges = {
'Today': [new Date(), new Date()],
'Yesterday': [moment().subtract(1, 'days').toDate(), moment().subtract(1, 'days').toDate()],
'This week': [moment().startOf('isoWeek').toDate(), thisWeekEndDate],
'This month': [moment().startOf('month').toDate(), thisMonthEndDate],
'This quarter': [moment().startOf('quarter').toDate(), thisQuarterEndDate],
'This year': [moment().startOf('year').toDate(), thisYearEndDate],
'Last week': [
moment().subtract(1, 'week').startOf('isoWeek').toDate(),
moment().subtract(1, 'week').endOf('isoWeek').toDate()
],
'Last month': [
moment().subtract(1, 'month').startOf('month').toDate(),
moment().subtract(1, 'month').endOf('month').toDate()
],
'Last quarter': [
moment().subtract(1, 'quarter').startOf('quarter').toDate(),
moment().subtract(1, 'quarter').endOf('quarter').toDate()
],
'Last year': [
moment().subtract(1, 'year').startOf('year').toDate(),
moment().subtract(1, 'year').endOf('year').toDate()
]
'Today': [today, today],
'Yesterday': [yesterday, yesterday],
'This week': [thisWeekStartDate, thisWeekEndDate],
'This month': [thisMonthStartDate, thisMonthEndDate],
'This quarter': [thisQuarterStartDate, thisQuarterEndDate],
'This year': [thisYearStartDate, thisYearEndDate],
'Last week': [lastWeekStartDate, lastWeekEndDate],
'Last month': [lastMonthStartDate, lastMonthEndDate],
'Last quarter': [lastQuarterStartDate, lastQuarterEndDate],
'Last year': [lastYearStartDate, lastYearEndDate]
}
//creating the ul element for the nav dropdown and giving it classnames

// creating the ul element for the nav dropdown and giving it classnames
const rangesNav = document.createElement('ul');

// creating the pluginData object that will hold the properties of this plugin
Expand All @@ -64,11 +71,11 @@ const quickPickPlugin = (thisRangesEndToday: boolean) => {
};

/**
* @param {string} label
* @returns HTML Element
*/
* @param {string} label
* @returns HTML Element
*/

//function for creating the range buttons in the nav
// function for creating the range buttons in the nav
const addRangeButton = (label: string) => {

// creating new elements to mimick selectable card component
Expand All @@ -88,7 +95,7 @@ const quickPickPlugin = (thisRangesEndToday: boolean) => {
// append the li item to the ul rangeNav prop
pluginData.rangesNav.appendChild(item);

// return the ranges buton prop
// return the ranges button prop
return pluginData.rangesButtons[label];
};

Expand All @@ -98,7 +105,7 @@ const quickPickPlugin = (thisRangesEndToday: boolean) => {
if (current) {
current.classList.remove('active');
}

if (selectedDates.length > 0 && activeLabel) {
pluginData.rangesButtons[activeLabel].classList.add('active');
}
Expand All @@ -109,16 +116,15 @@ const quickPickPlugin = (thisRangesEndToday: boolean) => {
selectedDates[1].toDateString() === pluginData.ranges[activeLabel][1].toDateString()
}


return {
// onReady is a hook from flatpickr that runs when calender is in a ready state
// onReady is a hook from flatpickr that runs when calendar is in a ready state
onReady(selectedDates: Array<Date>) {
// 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
for (const [label, range] of Object.entries(pluginData.ranges)) {
addRangeButton(label).addEventListener('click', function () {

const start = moment(range[0]).toDate();
const end = moment(range[1]).toDate();
const start = new Date(range[0]);
const end = new Date(range[1]);

if (!start) {
fp.clear();
Expand Down Expand Up @@ -170,4 +176,4 @@ const quickPickPlugin = (thisRangesEndToday: boolean) => {
};
}

export default quickPickPlugin;
export default quickPickPlugin;
Loading

0 comments on commit 350784e

Please sign in to comment.