Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use date-fns instead of moment #258

Merged
merged 2 commits into from
Sep 17, 2018
Merged

Conversation

timwis
Copy link
Contributor

@timwis timwis commented Sep 9, 2018

Helps address #152, replacing the 227 KB of moment.js with 29 KB of date-fns, a popular utility library for date functions that's structured similarly to lodash.

Please note that I don't believe all the affected functions have unit tests, and I'm not deeply familiar with this codebase, so I'd appreciate a second set of eyes on it! I would hate to have accidentally changed the logic of an otherwise minor function.

Also note that we may be able to remove timeFromNow in favour of date-fn's distanceInWordsToNow and possibly a few other DIY date functions in here, but I wanted to limit the scope of this PR.

Types of changes

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor

Checklist:

  • I've reviewed the guidelines for contributing to this repository.
  • I've checked style and lint with npm run lint.
  • I built the production version with npm run build.
  • I added tests to verify a bug fix or new functionality. (N/A)
  • All tests pass locally with npm test.
  • My change requires a change to the documentation, and I've updated the documentation accordingly. (N/A)

screen shot 2018-09-09 at 18 56 06

Copy link
Contributor

@dmethvin-gov dmethvin-gov left a comment

Choose a reason for hiding this comment

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

Awesome, thanks! This looks great to me, especially since date-fns work with tree shaking. This should save us some bytes.

Thanks for considering the tests. It would be hard to do a realistic test regarding several of these changes since they involve headers returned from the server, but they appear to be straightforward so 👍 .

@@ -235,7 +234,8 @@ export function uploadFile(file, uiOptions, onProgress, onChange, onError) {
} else {
let errorMessage = req.statusText;
if (req.status === 429) {
errorMessage = `You’ve reached the limit for the number of submissions we can accept at this time. Please try again in ${timeFromNow(moment.unix(parseInt(req.getResponseHeader('x-ratelimit-reset'), 10)))}.`;
const resetDate = new Date(req.getResponseHeader('x-ratelimit-reset') * 1000);
Copy link
Contributor

Choose a reason for hiding this comment

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

Noting that the date conversion here looks right since the original was in seconds-since-epoch and JS is milliseconds-since-epoch. There seems to be some controversy about whether this is a relative time or an absolute one. I can see the point about absolute being risky because it assumes relatively synchronized clocks. In any case, this still does whatever it did before, whether right or wrong. 😺

@@ -62,7 +61,7 @@ export default function SubmitButtons(props) {
<div className="usa-alert usa-alert-error schemaform-failure-alert">
<div className="usa-alert-body">
<p className="schemaform-warning-header"><strong>We’ve run into a problem</strong></p>
<p>We’re sorry. Your submission didn’t go through because we received too many requests from you. Please wait {timeFromNow(moment.unix(submission.extra))} and submit your request again.</p>
<p>We’re sorry. Your submission didn’t go through because we received too many requests from you. Please wait {timeFromNow(new Date(submission.extra * 1000))} and submit your request again.</p>
Copy link
Contributor

Choose a reason for hiding this comment

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

Weird, this is basically the same message but phrased a different way?

* @param date {Moment Date} The future date to check against
* @param userFromDate {Moment Date} The earlier date in the range. Defaults to today.
* @param date {Date} The future date to check against
* @param userFromDate {Date} The earlier date in the range. Defaults to today.
* @returns {string} The string description of how long until date occurs
*/
export function timeFromNow(date, userFromDate = null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just noting for future optimization that the only place where we use timeFromNow (and thus all the differenceIn* from date-fns is the "Please don't push Submit so often" message. Subtracting the two dates and expressing in terms of integer minutes is probably all we need there. I think we should land this as-is though and work on these details later.

@dmethvin-gov
Copy link
Contributor

@timwis Sorry, I waited too long to land this and it has merge conflicts. It should be fine if you rebase against current master, the raven-js dependency is gone now. Let me know if you have problems and I can do it locally.

@timwis
Copy link
Contributor Author

timwis commented Sep 15, 2018

@dmethvin-gov no worries, should be all sorted now. 👍

@dmethvin-gov dmethvin-gov merged commit 96755f4 into usds:master Sep 17, 2018
@timwis timwis deleted the refactor/date-fns branch September 17, 2018 14:36
@jcmeloni-usds jcmeloni-usds mentioned this pull request Sep 17, 2018
8 tasks
dmethvin-gov added a commit that referenced this pull request Sep 19, 2018
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.

2 participants