-
Notifications
You must be signed in to change notification settings - Fork 4
Bugfix: Pomodoro is not reset after refreshing or switching the view. #87
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
base: master
Are you sure you want to change the base?
Conversation
| const [isClockPlaying, setIsClockPlaying] = useState(false); | ||
| const [workDurationMinutes, setWorkDurationMinutes] = useState(25); | ||
| const [breakDurationMinutes, setBreakDurationMinutes] = useState(5); | ||
| const [m, setM] = useState(25); |
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.
[minute, setMinute] = useState(25):
[second, setSecond] = useState(0);
in this way, it could be more obvious for anyone who read m anywhere to know it stands for minutes.
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.
good point. corrected.
| localStorage.setItem("S", s) | ||
| }) | ||
|
|
||
| function setData(func, key) { |
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.
setData sounds not so direct for me. and it could be more in consistent with localStorage.setItem by
| function setData(func, key) { | |
| function localStorage_loadItem(itemName, itemSetter, itemType) {} |
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.
I used the name loadItemFromLocalStorage.
| useEffect ( () => { | ||
| setData((data) => setStartTime(data), "startTime") | ||
| setData((data) => setIsUserBreaking(data == "true"), "isUserBreaking") | ||
| setData((data) => setClockKey(Number(data)), "ClockKey") |
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.
I suggest to handle the data type (Number(), == "true" ) inside setData function via kwargs(function parameters).
so that the code could be easier to understand here
| setData((data) => setClockKey(Number(data)), "ClockKey") | |
| setData(setIsUserBreaking, "isUserBreaking", "bool") | |
| setData(setClockKey, "ClockKey", "number") |
How do you think?
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.
good point, corrected.
Apollo1840
left a comment
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.
It looks clear and effective, No more feedbacks from me. @changdiqing . Another option is to use redux store to do this, but I think we could leverage the pro and con a bit, and then make another PR, if the redux store is a better option.
| loadItemFromLocalStorage("breakDurationMinutes", setBreakDurationMinutes, "number") | ||
| loadItemFromLocalStorage("Minute", setMinute, "number") | ||
| loadItemFromLocalStorage("Second", setSecond, "number") | ||
| loadItemFromLocalStorage("Second", setSecond, "number") |
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.
Duplication
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.
Thanks. Fixed.
One con of local storage is that it can only store string. The item has to be converted to its original type while loading. Another option to avoid the conversion I did is to stringify and parse the item during storing and loading.
Hi, @changdiqing @Apollo1840,
I fixed the problem with useEffect and localStorage.
useEffect updates the m (minutes) and s (seconds) and saves them to localStorage.
After refreshing or switching the view, m and s are read from the localStorage and set to be the initialRemainingTime of the timer.
Please check the PR. Thanks.