-
Notifications
You must be signed in to change notification settings - Fork 30
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 UTC for daily stats calculations #2359
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
838618a
Use UTC for daily stats calculations
eatyourgreens 65565e9
Convert stories to CSF
eatyourgreens 87db87c
Remove inject
eatyourgreens ce889f2
Update story component name
eatyourgreens df7631f
Add the timeZone option
eatyourgreens 5fbe0f8
Fix the check for the current day number
eatyourgreens 4761e36
Refactor the daily counts
eatyourgreens 9421d3d
Fix test expectations
eatyourgreens 9d5ed08
Merge branch 'master' into daily-stats-utc
eatyourgreens File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...ents/YourStats/components/DailyClassificationsChart/DailyClassificationsChartConnector.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { MobXProviderContext, observer } from 'mobx-react' | ||
import { useContext } from 'react' | ||
|
||
import DailyClassificationsChartContainer from './DailyClassificationsChartContainer' | ||
|
||
function storeMapper(store) { | ||
const { | ||
project, | ||
user: { | ||
personalization: { | ||
counts, | ||
stats: { | ||
thisWeek | ||
} | ||
} | ||
} | ||
} = store | ||
|
||
return { | ||
counts, | ||
thisWeek, | ||
projectName: project['display_name'] | ||
} | ||
} | ||
|
||
function DailyClassificationsChartConnector() { | ||
const { store } = useContext(MobXProviderContext) | ||
const { counts, thisWeek, projectName } = storeMapper(store) | ||
return ( | ||
<DailyClassificationsChartContainer | ||
counts={counts} | ||
projectName={projectName} | ||
thisWeek={thisWeek} | ||
/> | ||
) | ||
} | ||
|
||
export default observer(DailyClassificationsChartConnector) |
65 changes: 27 additions & 38 deletions
65
...ents/YourStats/components/DailyClassificationsChart/DailyClassificationsChartContainer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,45 @@ | ||
import counterpart from 'counterpart' | ||
import { inject, observer } from 'mobx-react' | ||
import { array, number, shape, string } from 'prop-types' | ||
import { Component } from 'react' | ||
|
||
import DailyClassificationsChart from './DailyClassificationsChart' | ||
|
||
function storeMapper (stores) { | ||
const { project, user: { personalization: { counts, stats: { thisWeek } } } } = stores.store | ||
return { | ||
counts, | ||
thisWeek, | ||
projectName: project['display_name'] | ||
} | ||
const defaultCounts = { | ||
today: 0 | ||
} | ||
|
||
@inject(storeMapper) | ||
@observer | ||
class DailyClassificationsChartContainer extends Component { | ||
render () { | ||
const TODAY = new Date() | ||
const { counts, projectName, thisWeek } = this.props | ||
const stats = thisWeek.map(stat => { | ||
const day = new Date(stat.period) | ||
const locale = counterpart.getLocale() | ||
const count = (day.getDay() === TODAY.getDay()) ? counts.today : stat.count | ||
const longLabel = day.toLocaleDateString(locale, { weekday: 'long' }) | ||
const alt = `${longLabel}: ${count}` | ||
const label = day.toLocaleDateString(locale, { weekday: 'narrow' }) | ||
return Object.assign({}, stat, { alt, count, label, longLabel }) | ||
}) | ||
return ( | ||
<DailyClassificationsChart | ||
stats={stats} | ||
projectName={projectName} | ||
/> | ||
) | ||
} | ||
function DailyClassificationsChartContainer({ | ||
counts = defaultCounts, | ||
projectName, | ||
thisWeek = [] | ||
}) { | ||
const TODAY = new Date() | ||
const locale = counterpart.getLocale() | ||
const stats = thisWeek.map(({ count: statsCount, period }) => { | ||
const day = new Date(period) | ||
const isToday = day.getUTCDay() === TODAY.getDay() | ||
const count = isToday ? counts.today : statsCount | ||
const longLabel = day.toLocaleDateString(locale, { timeZone: 'UTC', weekday: 'long' }) | ||
const alt = `${longLabel}: ${count}` | ||
const label = day.toLocaleDateString(locale, { timeZone: 'UTC', weekday: 'narrow' }) | ||
return { alt, count, label, longLabel, period } | ||
}) | ||
return ( | ||
<DailyClassificationsChart | ||
stats={stats} | ||
projectName={projectName} | ||
/> | ||
) | ||
} | ||
|
||
DailyClassificationsChartContainer.propTypes = { | ||
/** Today's counts, updated as classifications are made. */ | ||
counts: shape({ | ||
today: number | ||
}), | ||
/** Project name */ | ||
projectName: string.isRequired, | ||
/** Array of daily stats from the stats server */ | ||
thisWeek: array | ||
} | ||
|
||
DailyClassificationsChartContainer.defaultProps = { | ||
counts: { | ||
today: 0 | ||
}, | ||
thisWeek: [] | ||
} | ||
|
||
export default DailyClassificationsChartContainer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...c/screens/ClassifyPage/components/YourStats/components/DailyClassificationsChart/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { default } from './DailyClassificationsChartContainer' | ||
export { default } from './DailyClassificationsChartConnector' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could put the day numbers and local names for each day in the store, so that each daily count is
{ count, dayNumber, label, longLabel, period }
. They never change, so only need to be generated once. Maybe something for a future rewrite of this component?