-
Notifications
You must be signed in to change notification settings - Fork 9
[refactor] extract independent Countdown class component from Hackathon page with MobX #69
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f62a40b
Initial plan
Copilot a76172e
refactor: replace countdown React hooks with MobX class approach
Copilot ac10cfb
refactor: extract Countdown as independent class component, simplify …
Copilot 867f0ca
refactor: remove label prop from Countdown, move label rendering to Hero
Copilot 5214914
refactor: replace componentDidUpdate with @reaction from mobx-react-h…
Copilot a064205
refactor: port BootCell TimeUnit algorithm, add className/onEnd props…
Copilot c4725a6
[refactor] simplify Copilot codes
TechQuery 5210837
[optimize] Count Down component folder
TechQuery 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 hidden or 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,90 @@ | ||
| import { computed, observable } from 'mobx'; | ||
| import { observer } from 'mobx-react'; | ||
| import { Component } from 'react'; | ||
|
|
||
| import styles from './Hero.module.less'; | ||
|
|
||
| export interface CountdownProps { | ||
| countdownTo?: string; | ||
| unitLabels: string[]; | ||
| } | ||
|
|
||
| @observer | ||
| export class Countdown extends Component<CountdownProps> { | ||
| @observable | ||
| accessor rest: number | null = null; | ||
|
|
||
| private timer?: number; | ||
|
|
||
| private get target() { | ||
| const { countdownTo } = this.props; | ||
| const value = countdownTo ? new Date(countdownTo).getTime() : NaN; | ||
|
|
||
| return Number.isFinite(value) ? value : NaN; | ||
| } | ||
|
|
||
| @computed | ||
| get sections() { | ||
| const { rest } = this; | ||
|
|
||
| if (rest === null) return ['--', '--', '--', '--']; | ||
|
|
||
| const totalSeconds = Math.floor(Math.max(0, rest) / 1000); | ||
| const days = Math.floor(totalSeconds / 86400); | ||
| const hours = Math.floor((totalSeconds % 86400) / 3600); | ||
| const minutes = Math.floor((totalSeconds % 3600) / 60); | ||
| const seconds = totalSeconds % 60; | ||
|
|
||
| return [days, hours, minutes, seconds].map(value => String(value).padStart(2, '0')); | ||
| } | ||
|
|
||
| tick = () => { | ||
| this.rest = Math.max(0, this.target - Date.now()); | ||
| }; | ||
|
|
||
| componentDidMount() { | ||
| if (Number.isFinite(this.target)) { | ||
| this.tick(); | ||
| this.timer = window.setInterval(this.tick, 1000); | ||
| } | ||
| } | ||
|
|
||
| componentDidUpdate(prevProps: CountdownProps) { | ||
| if (prevProps.countdownTo !== this.props.countdownTo) { | ||
| if (this.timer) { | ||
| window.clearInterval(this.timer); | ||
| this.timer = undefined; | ||
| } | ||
|
|
||
| this.rest = null; | ||
|
|
||
| if (Number.isFinite(this.target)) { | ||
| this.tick(); | ||
| this.timer = window.setInterval(this.tick, 1000); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| componentWillUnmount() { | ||
| if (this.timer) window.clearInterval(this.timer); | ||
| } | ||
|
|
||
| render() { | ||
| const { unitLabels } = this.props; | ||
| const { sections } = this; | ||
|
|
||
| return ( | ||
| <ol className={`list-unstyled ${styles.countdownGrid} m-0`}> | ||
| {sections.map((value, index) => ( | ||
| <li | ||
| key={`${index}-${unitLabels[index]}`} | ||
| className={`${styles.countdownCell} d-flex flex-column justify-content-center align-items-center`} | ||
| > | ||
| <strong>{value}</strong> | ||
| <span>{unitLabels[index]}</span> | ||
| </li> | ||
| ))} | ||
| </ol> | ||
| ); | ||
| } | ||
| } | ||
|
TechQuery marked this conversation as resolved.
|
This file contains hidden or 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
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.