Skip to content
Open
Changes from 1 commit
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
67 changes: 62 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,37 @@ function RemainingTimeContextProvider(props) {
const [isClockPlaying, setIsClockPlaying] = useState(false);
const [workDurationMinutes, setWorkDurationMinutes] = useState(25);
const [breakDurationMinutes, setBreakDurationMinutes] = useState(5);
const [m, setM] = useState(25);
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.

[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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

good point. corrected.

const [s, setS] = useState(0);

useEffect ( () => {
setData((data) => setStartTime(data), "startTime")
setData((data) => setIsUserBreaking(data == "true"), "isUserBreaking")
setData((data) => setClockKey(Number(data)), "ClockKey")
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.

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

Suggested change
setData((data) => setClockKey(Number(data)), "ClockKey")
setData(setIsUserBreaking, "isUserBreaking", "bool")
setData(setClockKey, "ClockKey", "number")

How do you think?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

good point, corrected.

setData((data) => setIsClockPlaying(data == "true"), "isClockPlaying")
setData((data) => setWorkDurationMinutes(Number(data)), "workDurationMinutes")
setData((data) => setBreakDurationMinutes(Number(data)), "breakDurationMinutes")
setData((data) => setM(Number(data)), "M")
setData((data) => setS(Number(data)), "S")
}, [])

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("M", m)
localStorage.setItem("S", s)
})

function setData(func, key) {
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.

setData sounds not so direct for me. and it could be more in consistent with localStorage.setItem by

Suggested change
function setData(func, key) {
function localStorage_loadItem(itemName, itemSetter, itemType) {}

Copy link
Copy Markdown
Collaborator Author

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.

const data = localStorage.getItem(key)
if (data) {
func(data)
}
}
return (
<remainingTimeContext.Provider
value={{
Expand All @@ -85,6 +115,10 @@ function RemainingTimeContextProvider(props) {
setWorkDurationMinutes,
breakDurationMinutes,
setBreakDurationMinutes,
m,
setM,
s,
setS
}}
>
{props.children}
Expand All @@ -100,7 +134,10 @@ function ClockController() {
setIsClockPlaying,
isUserBreaking,
setIsUserBreaking,
isClockPlaying
isClockPlaying,
setM,
setS,
workDurationMinutes
} = useContext(remainingTimeContext);

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

Expand All @@ -133,6 +172,8 @@ function ClockSettings() {
setIsUserBreaking,
setWorkDurationMinutes,
setBreakDurationMinutes,
setM,
setS
} = useContext(remainingTimeContext);
const handleDown = (value,variableSetter) =>{
return ()=>{
Expand All @@ -146,7 +187,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 +222,8 @@ function ClockSettings() {
onClick={() => {
setWorkDurationMinutes(workMinutes);
setBreakDurationMinutes(breakMinutes);
setM(workMinutes);
setS(0);
setIsUserBreaking(false);
setIsClockPlaying(false);
setClockKey((prevKey) => prevKey + 1);
Expand All @@ -195,9 +238,14 @@ function ClockSettings() {
}

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

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

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

setM(minutes);
setS(seconds);
if (minutes == 0 & seconds == 0) {
setM(workDurationMinutes)
}
return (
<div>
<div className="text-center"> break </div>
Expand All @@ -232,6 +285,8 @@ function PomodoroClock() {
breakDurationMinutes,
isClockPlaying,
setIsClockPlaying,
m,
s,
} = useContext(remainingTimeContext);

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