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

lib-user: Use a getDefaultDateRange() utility #6420

Merged
merged 2 commits into from
Nov 12, 2024

Conversation

goplayoutside3
Copy link
Contributor

@goplayoutside3 goplayoutside3 commented Nov 4, 2024

Package

lib-user

Linked Issue

  • This is working toward an ADR and generally more documentation about how we handle date ranges for user stats and project stats.

Describe your changes

  • Define getDefaultDateRange() utility function to replace falsey date ranges like { endDate: undefined, startDate: undefined }.
  • I started this PR by documenting getDateInterval()'s handling of Javascript Dates and UTC, but realized it's much cleaner to separate out the definition of a default date range from getting the interval for the ERAS query (days, week, month, year).

How to Review

  • There's no changes needed for unit tests so all should be passing regardless of timezone
  • No UI changes either. I tested this by comparing app-root run locally and viewing my stats, homepage, and groups with no noticeable difference in the UI.

Checklist

PR Creator - Please cater the checklist to fit the review needed for your code changes.
PR Reviewer - Use the checklist during your review. Each point should be checkmarked or discussed before PR approval.

General

  • Tests are passing locally and on Github
  • Documentation is up to date and changelog has been updated if appropriate
  • You can yarn panic && yarn bootstrap or docker-compose up --build and FEM works as expected
  • FEM works in all major desktop browsers: Firefox, Chrome, Edge, Safari (Use Browserstack account as needed)

@coveralls
Copy link

coveralls commented Nov 4, 2024

Coverage Status

coverage: 77.563% (-0.02%) from 77.583%
when pulling 531e61e on default-date-range
into 5c66752 on master.

@mcbouslog mcbouslog self-assigned this Nov 7, 2024
Copy link
Contributor

@mcbouslog mcbouslog left a comment

Choose a reason for hiding this comment

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

Some comments for consideration, otherwise looks good!

thisWeekQuery.project_contributions = true
const { data: thisWeekData } = useStats({
sourceId: user?.id,
query: thisWeekQuery
})

const allTimeQuery = getDateInterval({
endDate: todayUTC,
startDate: getStatsDateString(user?.created_at)
endDate: getStatsDateString(new Date()), // Today's UTC date string like 24-09-23.
Copy link
Contributor

Choose a reason for hiding this comment

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

Pedantic, but not sure if the 20 in 2024 should be included

Suggested change
endDate: getStatsDateString(new Date()), // Today's UTC date string like 24-09-23.
endDate: getStatsDateString(new Date()), // Today's UTC date string like 2024-09-23.

@@ -1,6 +1,10 @@
/*
Stats dates are always handled in UTC strings such as 24-09-23.
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar to other comment

Suggested change
Stats dates are always handled in UTC strings such as 24-09-23.
Stats dates are always handled in UTC strings such as 2024-09-23.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Shoot I just realized I never included these suggested changes. I'll include it in #6472

return 'year'
}

export function getDateInterval({ endDate, startDate }) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if we need to 1) check endDate or startDate are the date strings we expect and/or 2) set default values (maybe based on getDefaultDateRange)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

getDateInterval() is only ever called with an object containing endDate and startDate. Checking for those strings and setting default values inside getDateInterval() is how the two actions became intertwined 1) Getting the interval of day, week, month, or year, and 2) setting a default endDate and startDate. The intertwining of those two actions is giving lib-user a bit of spaghetti code.

getDateInterval() is more like formatMyErasQueryForMe(). Instead of using it has a catch-all for cases where endDate and startDate are undefined, I'd rather it be a one-purpose function. You've already type checked endDate and startDate in app-root's App Router pages, and I assume grommet's Calendar component has its own type checking for date range variables.

Does that sound like a good plan to you? I'm definitely willing to disregard this PR in favor of keeping the old getDateInterval() if you prefer, but the above mentioned intertwining of actions and the repeated setting of default props with endDate and startDate as "undefined" feels like a bit of a code smell to me.

Copy link
Contributor

Choose a reason for hiding this comment

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

That sounds good to me! I think the separate functions are preferable.

@mcbouslog mcbouslog removed their assignment Nov 11, 2024
@github-actions github-actions bot added the approved This PR is approved for merging label Nov 11, 2024
@goplayoutside3 goplayoutside3 merged commit 2be6818 into master Nov 12, 2024
8 checks passed
@goplayoutside3 goplayoutside3 deleted the default-date-range branch November 12, 2024 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved This PR is approved for merging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants