Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 78 additions & 5 deletions frontend/src/components/pomodoroview/PomodoroView.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import React, { useState, useContext, createContext} from "react";
import React, { useState, useContext, createContext, useEffect} from "react";
import { CountdownCircleTimer } from "react-countdown-circle-timer";
import store from "../../redux-store";
import SubmitRecordService from "../../services/SubmitRecordService";
Expand Down Expand Up @@ -69,7 +69,53 @@ function RemainingTimeContextProvider(props) {
const [isClockPlaying, setIsClockPlaying] = useState(false);
const [workDurationMinutes, setWorkDurationMinutes] = useState(25);
const [breakDurationMinutes, setBreakDurationMinutes] = useState(5);
const [minute, setMinute] = useState(25);
const [second, setSecond] = useState(0);

useEffect ( () => {
loadItemFromLocalStorage("startTime", setStartTime, "string")
loadItemFromLocalStorage("isUserBreaking", setIsUserBreaking, "boolean")
loadItemFromLocalStorage("ClockKey", setClockKey, "number")
loadItemFromLocalStorage("isClockPlaying", setIsClockPlaying, "boolean")
loadItemFromLocalStorage("workDurationMinutes", setWorkDurationMinutes, "number")
loadItemFromLocalStorage("breakDurationMinutes", setBreakDurationMinutes, "number")
loadItemFromLocalStorage("Minute", setMinute, "number")
loadItemFromLocalStorage("Second", setSecond, "number")
loadItemFromLocalStorage("Second", setSecond, "number")
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Duplication

Copy link
Copy Markdown
Collaborator Author

@theSaLi theSaLi Jun 6, 2021

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.

}, [])

useEffect ( () => {
localStorage.setItem("startTime", startTime)
localStorage.setItem("isUserBreaking", isUserBreaking)
localStorage.setItem("ClockKey", ClockKey)
localStorage.setItem("isClockPlaying", isClockPlaying)
localStorage.setItem("workDurationMinutes", workDurationMinutes)
localStorage.setItem("breakDurationMinutes", breakDurationMinutes)
localStorage.setItem("Minute", minute)
localStorage.setItem("Second", second)
})

/**
* Load a item from the local storage with the key itemName and set the item with the itemSetFunc.
* itemType indicates what type the item should be converted to. Since local storage only stores strings,
* the item needs to be converted.
* @param {*} itemName
* @param {*} itemSetFunc
* @param {*} itemType ONLY choose from "string", "boolean" and "number"
*/
function loadItemFromLocalStorage(itemName, itemSetFunc, itemType) {
const data = localStorage.getItem(itemName)
if (data) {
if (itemType == "boolean") {
itemSetFunc(data == "true")
} else if (itemType == "number") {
itemSetFunc(Number(data))
} else if (itemType == "string") {
itemSetFunc(data)
}
}
}

return (
<remainingTimeContext.Provider
value={{
Expand All @@ -85,6 +131,10 @@ function RemainingTimeContextProvider(props) {
setWorkDurationMinutes,
breakDurationMinutes,
setBreakDurationMinutes,
minute,
setMinute,
second,
setSecond
}}
>
{props.children}
Expand All @@ -100,7 +150,10 @@ function ClockController() {
setIsClockPlaying,
isUserBreaking,
setIsUserBreaking,
isClockPlaying
isClockPlaying,
setMinute,
setSecond,
workDurationMinutes
} = useContext(remainingTimeContext);

const handleSwitch = (flag) => {
Expand All @@ -114,6 +167,8 @@ function ClockController() {
setClockKey((prevKey) => prevKey + 1);
setIsClockPlaying(false);
setIsUserBreaking(false);
setMinute(workDurationMinutes);
setSecond(0)
}
};

Expand All @@ -133,6 +188,8 @@ function ClockSettings() {
setIsUserBreaking,
setWorkDurationMinutes,
setBreakDurationMinutes,
setMinute,
setSecond
} = useContext(remainingTimeContext);
const handleDown = (value,variableSetter) =>{
return ()=>{
Expand All @@ -146,7 +203,7 @@ function ClockSettings() {
}
}
const handleChange = (variableSetter) => { // change value
return (event) => variableSetter(event.target.value);
return (event) => variableSetter(Number(event.target.value));
};

return (
Expand Down Expand Up @@ -181,6 +238,8 @@ function ClockSettings() {
onClick={() => {
setWorkDurationMinutes(workMinutes);
setBreakDurationMinutes(breakMinutes);
setMinute(workMinutes);
setSecond(0);
setIsUserBreaking(false);
setIsClockPlaying(false);
setClockKey((prevKey) => prevKey + 1);
Expand All @@ -195,9 +254,14 @@ function ClockSettings() {
}

function RenderTimeWork({ remainingTime }) {
const {setMinute, setSecond, breakDurationMinutes} = useContext(remainingTimeContext);
const minutes = Math.floor(remainingTime / 60);
const seconds = remainingTime % 60;

setMinute(minutes);
setSecond(seconds);
if (minutes == 0 & seconds == 0) {
setMinute(breakDurationMinutes)
}
return (
<div>
<div className="text-center"> work </div>
Expand All @@ -208,9 +272,14 @@ function RenderTimeWork({ remainingTime }) {

// in the future might be different with RenderTimeWork, now is the same
function RenderTimeBreak({ remainingTime }) {
const {setMinute, setSecond, workDurationMinutes} = useContext(remainingTimeContext);
const minutes = Math.floor(remainingTime / 60);
const seconds = remainingTime % 60;

setMinute(minutes);
setSecond(seconds);
if (minutes == 0 & seconds == 0) {
setMinute(workDurationMinutes)
}
return (
<div>
<div className="text-center"> break </div>
Expand All @@ -232,6 +301,8 @@ function PomodoroClock() {
breakDurationMinutes,
isClockPlaying,
setIsClockPlaying,
minute,
second,
} = useContext(remainingTimeContext);

// todo: make a soundService
Expand Down Expand Up @@ -264,6 +335,7 @@ function PomodoroClock() {
{...breakTimerProps}
key={100 + ClockKey}
isPlaying={isClockPlaying}
initialRemainingTime={minute * 60 + second}
duration={breakDurationMinutes * 60}
onComplete={breakCompleteHandler}
>
Expand All @@ -274,6 +346,7 @@ function PomodoroClock() {
{...workTimerProps}
key={ClockKey}
isPlaying={isClockPlaying}
initialRemainingTime={minute * 60 + second}
duration={workDurationMinutes * 60}
onComplete={workCompleteHandler}
>
Expand Down