Skip to content

Commit 18c56fe

Browse files
authored
Merge pull request #1101 from kleros/fix(web)/countdown-behaviour
Fix(web): clear timeout when unmounting component
2 parents aef101e + 5692550 commit 18c56fe

File tree

2 files changed

+13
-36
lines changed

2 files changed

+13
-36
lines changed

web/src/hooks/useCountdown.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useState, useEffect } from "react";
22
import { getTimeLeft } from "utils/date";
3+
import { isUndefined } from "utils/index";
34

45
export function useCountdown(deadline?: number): number | undefined {
56
const [counter, setCounter] = useState<number | undefined>();
@@ -10,9 +11,10 @@ export function useCountdown(deadline?: number): number | undefined {
1011
}
1112
}, [deadline]);
1213
useEffect(() => {
13-
typeof counter !== "undefined" &&
14-
counter > 0 &&
15-
setTimeout(() => setCounter(counter - 1), 1000);
14+
if (!isUndefined(counter) && counter > 0) {
15+
const timeout = setTimeout(() => setCounter(counter - 1), 1000);
16+
return () => clearTimeout(timeout);
17+
} else return;
1618
}, [counter]);
1719
return counter;
1820
}

web/src/pages/Cases/CaseDetails/Timeline.tsx

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,23 @@ const Timeline: React.FC<{
1010
dispute: DisputeDetailsQuery["dispute"];
1111
currentPeriodIndex: number;
1212
}> = ({ currentPeriodIndex, dispute }) => {
13-
const currentItemIndex = currentPeriodToCurrentItem(
14-
currentPeriodIndex,
15-
dispute?.ruled
16-
);
13+
const currentItemIndex = currentPeriodToCurrentItem(currentPeriodIndex, dispute?.ruled);
1714
const items = useTimeline(dispute, currentItemIndex, currentItemIndex);
1815
return (
1916
<TimeLineContainer>
20-
<StyledSteps
21-
horizontal
22-
{...{ items, currentItemIndex, currentPeriodIndex }}
23-
/>
17+
<StyledSteps horizontal {...{ items, currentItemIndex, currentPeriodIndex }} />
2418
</TimeLineContainer>
2519
);
2620
};
2721

28-
const currentPeriodToCurrentItem = (
29-
currentPeriodIndex: number,
30-
ruled?: boolean
31-
): number => {
22+
const currentPeriodToCurrentItem = (currentPeriodIndex: number, ruled?: boolean): number => {
3223
if (currentPeriodIndex <= Periods.commit) return currentPeriodIndex;
33-
else if (currentPeriodIndex < Periods.execution)
34-
return currentPeriodIndex - 1;
24+
else if (currentPeriodIndex < Periods.execution) return currentPeriodIndex - 1;
3525
else return ruled ? 5 : currentPeriodIndex - 1;
3626
};
3727

38-
const useTimeline = (
39-
dispute: DisputeDetailsQuery["dispute"],
40-
currentItemIndex: number,
41-
currentPeriodIndex: number
42-
) => {
43-
const titles = [
44-
"Evidence Period",
45-
"Voting Period",
46-
"Appeal Period",
47-
"Executed",
48-
];
28+
const useTimeline = (dispute: DisputeDetailsQuery["dispute"], currentItemIndex: number, currentPeriodIndex: number) => {
29+
const titles = ["Evidence Period", "Voting Period", "Appeal Period", "Executed"];
4930
const deadlineCurrentPeriod = getDeadline(
5031
currentPeriodIndex,
5132
dispute?.lastPeriodChange,
@@ -77,15 +58,9 @@ const getDeadline = (
7758
lastPeriodChange?: string,
7859
timesPerPeriod?: string[]
7960
): number | undefined => {
80-
if (
81-
lastPeriodChange &&
82-
timesPerPeriod &&
83-
currentPeriodIndex < timesPerPeriod.length
84-
) {
61+
if (lastPeriodChange && timesPerPeriod && currentPeriodIndex < timesPerPeriod.length) {
8562
const parsedLastPeriodChange = parseInt(lastPeriodChange, 10);
86-
const parsedTimeCurrentPeriod = parseInt(
87-
timesPerPeriod[currentPeriodIndex]
88-
);
63+
const parsedTimeCurrentPeriod = parseInt(timesPerPeriod[currentPeriodIndex]);
8964
return parsedLastPeriodChange + parsedTimeCurrentPeriod;
9065
}
9166
return 0;

0 commit comments

Comments
 (0)