Skip to content

MPDX-6916: Replace Moment - #14

Merged
reldredge71 merged 12 commits into
mainfrom
MPDX-6916-Replace-Moment
Feb 22, 2021
Merged

MPDX-6916: Replace Moment#14
reldredge71 merged 12 commits into
mainfrom
MPDX-6916-Replace-Moment

Conversation

@reldredge71

Copy link
Copy Markdown
Contributor

I decided to go with Luxon as a replacement to MomentJS: https://moment.github.io/luxon/index.html

@vercel

vercel Bot commented Feb 17, 2021

Copy link
Copy Markdown

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/cru/mpdx-react/DJaxDwFBkPCvXLty5BSJtJ2wm18q
✅ Preview: https://mpdx-react-git-mpdx-6916-replace-moment-cru.vercel.app

Settings.resetCaches();
});

it('default', async () => {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is failing. For the WeeklyActivityTableCellDateRange, the expected value should be "Jan 26 - Feb 1", but it is now "Jan 27 - Feb 2". Unless Luxon defines a week as being Monday to Sunday, rather than Sunday to Saturday, I'm not sure why this is happening.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I just found out, that's exactly what's happening: moment/luxon@6b145bf. Would it make more sense to update the test to the new "start of week" and "end of week" values, or make WeeklyActivity offset the values coming from Luxon?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm ya the library just always returns Monday as the first day of the week. We might need to figure this out ourselves somehow. moment/luxon#373 (comment) has some code in a Gist that would work but that's annoying...

It seems weird displaying Monday as the first day of the week. But then again it probably seems weird for someone in another locale that Sunday is the first day of the week. But maybe we could consider these events as starting over every work week. Idk how we clearly show that to users as it's just a filter on the dashboard... Maybe a rolling 7 day window would make sense but idk if we can make that decision...

For explicitly setting Sunday to make US users happy, either of these could work:

  1. .set({ weekday: 0 })
  2. .startOf('week').minus({ days: 1 })

@OzzieOrca OzzieOrca left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good :) Thanks for figuring this out!

I probably didn't make this clear enough in the ticket but Tatai was migrating to date-fns as he went along I think. I like Luxon but haven't really used date-fns. Sounds like you want to move forward with Luxon too. So we probably need to migrate from date-fns to Luxon too. Having 2 date libraries doesn't seem great. At this point it's probably better to do that second migration in another PR since this one is looking good and to keep thing cleaner.

Comment thread .gitignore Outdated
Comment on lines +45 to +46
node_modules
package-lock.json

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did you get files here? Yarn shouldn't make them... Kinda looks like you ran npm.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I ran yarn but I think there was something that wasn't installing properly until I ran npm. I can give it a try again to avoid npm altogether.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I ran into this too initially. Did you set your yarn version locally to be Yarn 2? I followed the documentation to do that: https://yarnpkg.com/getting-started/install. The main command you're looking for is yarn set version berry but I had to upgrade to the newest version first in order to use yarn set.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya maybe you need a Yarn upgrade. I'm running 2.4. Yarn still uses the 1.x version even if you have 2.4 installed unless the project has v2 configured. https://github.com/CruGlobal/mpdx-react/blob/main/.yarnrc.yml#L5 should set that but maybe I have something configured wrong... As long as you have a current yarn version it should just work... Let me know if it doesn't.

describe('MockDate', () => {
beforeEach(() => {
MockDate.set(new Date(2020, 1, 1));
Settings.now = () => new Date(2020, 1, 1).valueOf();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh this is nice to have included. Mocking dates has always been finicky.

});
afterEach(() => {
MockDate.reset();
Settings.resetCaches();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do this in a global afterEach? Actually we could setup the mock date globally too...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we can probably do that. Where can I setup things to run globally on all jest tests?

Comment on lines +111 to +112
const todayDate = DateTime.local().toISODate();
const date = DateTime.fromISO(contact.lateAt).toISODate();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these variables unused?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess not! I might have left that there after trying different solutions.

Settings.resetCaches();
});

it('default', async () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm ya the library just always returns Monday as the first day of the week. We might need to figure this out ourselves somehow. moment/luxon#373 (comment) has some code in a Gist that would work but that's annoying...

It seems weird displaying Monday as the first day of the week. But then again it probably seems weird for someone in another locale that Sunday is the first day of the week. But maybe we could consider these events as starting over every work week. Idk how we clearly show that to users as it's just a filter on the dashboard... Maybe a rolling 7 day window would make sense but idk if we can make that decision...

For explicitly setting Sunday to make US users happy, either of these could work:

  1. .set({ weekday: 0 })
  2. .startOf('week').minus({ days: 1 })

Comment on lines +119 to +120
const [startOfWeek, setStartOfWeek] = useState(DateTime.local().startOf('week'));
const [endOfWeek, setEndOfWeek] = useState(DateTime.local().endOf('week'));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An Interval transformed with mapEndpoints could work here. Seems a little cleaner since it's one state variable but maybe slightly more complicated.

@reldredge71

Copy link
Copy Markdown
Contributor Author

@OzzieOrca Yeah I think the ticket was clear enough, I think in the end Luxon looks nicer to me than date-fns. I also looked into DayJS since it seems like Moment with a smaller bundle size, but I think I like Luxon's API a little better. I can create another PR after this for ripping date-fns out as well.

@OzzieOrca OzzieOrca left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good :) Couple of trivial things.

Comment thread __tests__/util/setup.ts Outdated
Comment on lines +16 to +20
global.beforeEach(() => {
Settings.now = () => new Date(2020, 1, 1).valueOf();
});

global.afterEach(() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think global is just the node global object variable (like window is in a browser). I don't think you need it as referencing a global variable directly should work. But if there are type errors or something, you can keep it.

Suggested change
global.beforeEach(() => {
Settings.now = () => new Date(2020, 1, 1).valueOf();
});
global.afterEach(() => {
beforeEach(() => {
Settings.now = () => new Date(2020, 1, 1).valueOf();
});
afterEach(() => {

@@ -1,5 +1,5 @@
import React from 'react';
import MockDate from 'mockdate';
import { Settings } from 'luxon';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is unused and is throwing a lint error.

@OzzieOrca OzzieOrca left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📅 🕐 🎉

@reldredge71
reldredge71 merged commit 2ef278f into main Feb 22, 2021
@reldredge71
reldredge71 deleted the MPDX-6916-Replace-Moment branch February 22, 2021 18:32
@kegrimes kegrimes mentioned this pull request May 27, 2026
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants