Skip to content

Commit

Permalink
feat: adds the functionality to stop the countdown
Browse files Browse the repository at this point in the history
  • Loading branch information
devgustavosantos committed Aug 8, 2024
1 parent fe4bc48 commit a334859
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 16 deletions.
25 changes: 12 additions & 13 deletions src/components/Countdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@ function CountdownComponent() {
const secondesToDisplayed = String(secondsRemaining).padStart(2, '0');

useEffect(() => {
function changeTime() {
if (!currentTask) return;
const shouldRemainingTimeBeReset = !currentTask && remainingTime;
if (shouldRemainingTimeBeReset) {
setRemainingTime(0);

return;
}

if (!currentTask) return;

const timeout = setTimeout(() => {
const currentDate = new Date();
const distanceBetweenDates =
currentDate.getTime() - currentTask?.createdAt.getTime();
Expand All @@ -34,23 +41,15 @@ function CountdownComponent() {
const secondsRemaining =
desiredTimeConvertedToSeconds - distanceConvertedToSeconds;

if (secondsRemaining <= 0) {
setRemainingTime(0);

return;
}
if (secondsRemaining <= 0) return;

setRemainingTime(secondsRemaining);

return setTimeout(changeTime, 1000);
}

const timeout = changeTime();
}, 1000);

return () => {
clearTimeout(timeout);
};
}, [currentTask]);
}, [currentTask, remainingTime, setRemainingTime]);

return (
<S.Container>
Expand Down
4 changes: 3 additions & 1 deletion src/contexts/Form/Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { FormContext } from './';
import { FormSchema, FormType, FormProviderProps } from './types';

export function FormProvider({ children }: FormProviderProps) {
const { register, handleSubmit, setValue, watch } = useForm<FormType>({
const { register, handleSubmit, setValue, watch, reset } = useForm<FormType>({
resolver: zodResolver(FormSchema),
defaultValues: {
desiredTime: 0,
Expand Down Expand Up @@ -89,6 +89,8 @@ export function FormProvider({ children }: FormProviderProps) {
desiredTime: data.desiredTime,
createdAt: new Date(),
});

reset();
}

return (
Expand Down
21 changes: 20 additions & 1 deletion src/contexts/Tasks/Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,28 @@ import { TasksProviderProps } from './types';

export function TasksProvider({ children }: TasksProviderProps) {
const [currentTask, setCurrentTask] = useState<TaskType | null>(null);
const [tasks, setTasks] = useState<TaskType[]>([]);

function interruptTask() {
if (!currentTask) return;

setTasks((prevState) => {
return [
...prevState,
{
...currentTask,
interruptedAt: new Date(),
},
];
});

setCurrentTask(null);
}

return (
<TasksContext.Provider value={{ currentTask, setCurrentTask }}>
<TasksContext.Provider
value={{ currentTask, setCurrentTask, interruptTask, tasks }}
>
{children}
</TasksContext.Provider>
);
Expand Down
2 changes: 2 additions & 0 deletions src/contexts/Tasks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ interface TasksProviderProps {
interface TasksContextType {
currentTask: TaskType | null;
setCurrentTask: Dispatch<SetStateAction<TaskType | null>>;
interruptTask: () => void;
tasks: TaskType[];
}

export type { TasksProviderProps, TasksContextType };
6 changes: 5 additions & 1 deletion src/pages/Home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Minus, Plus } from '@phosphor-icons/react';
import { Countdown } from '@/components/Countdown';
import { StartStopButton } from '@/components/StartStopButton';
import { useFormContext } from '@/contexts/Form';
import { useTasksContext } from '@/contexts/Tasks';
import { TASK } from '@/utils/task';

import * as S from './styles';
Expand All @@ -18,6 +19,8 @@ export function Home() {
handleSubmit,
} = useFormContext();

const { currentTask, interruptTask } = useTasksContext();

return (
<S.Container>
<S.Form onSubmit={handleSubmit(onSubmit)}>
Expand Down Expand Up @@ -66,7 +69,8 @@ export function Home() {

<StartStopButton
disabled={!isAvailableStartCountdown}
isStartButton={true}
isStartButton={!currentTask}
onClick={interruptTask}
/>
</S.Form>
</S.Container>
Expand Down

0 comments on commit a334859

Please sign in to comment.