Skip to content

Commit dd8f927

Browse files
committed
fix(web): countdown fix
1 parent d41bb39 commit dd8f927

File tree

2 files changed

+48
-21
lines changed

2 files changed

+48
-21
lines changed

web/src/hooks/useCountdown.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import { useState, useEffect } from "react";
22
import { getTimeLeft } from "utils/date";
33

4-
export function useCountdown(deadline: number): number {
5-
const timeLeft = getTimeLeft(deadline);
6-
const [counter, setCounter] = useState<number>(timeLeft);
4+
export function useCountdown(deadline?: number): number | undefined {
5+
const [counter, setCounter] = useState<number | undefined>();
76
useEffect(() => {
8-
counter > 0 && setTimeout(() => setCounter(counter - 1), 1000);
7+
if (typeof deadline !== "undefined") {
8+
const timeLeft = getTimeLeft(deadline);
9+
setCounter(timeLeft);
10+
}
11+
}, [deadline]);
12+
useEffect(() => {
13+
typeof counter !== "undefined" &&
14+
counter > 0 &&
15+
setTimeout(() => setCounter(counter - 1), 1000);
916
}, [counter]);
1017
return counter;
1118
}

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

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Periods } from "consts/periods";
44
import { DisputeDetailsQuery } from "queries/useDisputeDetailsQuery";
55
import { Box, Steps } from "@kleros/ui-components-library";
66
import { useCountdown } from "hooks/useCountdown";
7-
import { secondsToDayHourMinute, getTimeLeft } from "utils/date";
7+
import { secondsToDayHourMinute } from "utils/date";
88

99
const Timeline: React.FC<{
1010
dispute: DisputeDetailsQuery["dispute"];
@@ -32,7 +32,7 @@ const currentPeriodToCurrentItem = (
3232
if (currentPeriodIndex <= Periods.commit) return currentPeriodIndex;
3333
else if (currentPeriodIndex < Periods.execution)
3434
return currentPeriodIndex - 1;
35-
else return ruled ? currentPeriodIndex : currentPeriodIndex - 1;
35+
else return ruled ? 5 : currentPeriodIndex - 1;
3636
};
3737

3838
const useTimeline = (
@@ -46,31 +46,51 @@ const useTimeline = (
4646
"Appeal Period",
4747
"Executed",
4848
];
49-
const parsedLastPeriodChange = parseInt(dispute?.lastPeriodChange, 10);
50-
const parsedTimeCurrentPeriod =
51-
dispute && dispute?.court.timesPerPeriod.length > currentPeriodIndex
52-
? parseInt(dispute?.court.timesPerPeriod[currentPeriodIndex])
53-
: 0;
54-
const countdown = useCountdown(
55-
parsedLastPeriodChange + parsedTimeCurrentPeriod
49+
const deadlineCurrentPeriod = getDeadline(
50+
currentPeriodIndex,
51+
dispute?.lastPeriodChange,
52+
dispute?.court.timesPerPeriod
5653
);
54+
const countdown = useCountdown(deadlineCurrentPeriod);
5755
const getSubitems = (index: number): string[] => {
58-
if (index < currentItemIndex) {
59-
return ["Done!"];
60-
} else if (index === 3) {
61-
return currentItemIndex === 3 ? ["Pending"] : [];
62-
} else if (index === currentItemIndex) {
63-
return [secondsToDayHourMinute(countdown)];
64-
} else {
65-
return [secondsToDayHourMinute(dispute?.court.timesPerPeriod[index])];
56+
if (typeof countdown !== "undefined" && dispute) {
57+
if (index < currentItemIndex) {
58+
return ["Done!"];
59+
} else if (index === 3) {
60+
return currentItemIndex === 3 ? ["Pending"] : [];
61+
} else if (index === currentItemIndex) {
62+
return [secondsToDayHourMinute(countdown)];
63+
} else {
64+
return [secondsToDayHourMinute(dispute?.court.timesPerPeriod[index])];
65+
}
6666
}
67+
return ["Loading..."];
6768
};
6869
return titles.map((title, i) => ({
6970
title,
7071
subitems: getSubitems(i),
7172
}));
7273
};
7374

75+
const getDeadline = (
76+
currentPeriodIndex: number,
77+
lastPeriodChange?: string,
78+
timesPerPeriod?: string[]
79+
): number | undefined => {
80+
if (
81+
lastPeriodChange &&
82+
timesPerPeriod &&
83+
currentPeriodIndex < timesPerPeriod.length
84+
) {
85+
const parsedLastPeriodChange = parseInt(lastPeriodChange, 10);
86+
const parsedTimeCurrentPeriod = parseInt(
87+
timesPerPeriod[currentPeriodIndex]
88+
);
89+
return parsedLastPeriodChange + parsedTimeCurrentPeriod;
90+
}
91+
return undefined;
92+
};
93+
7494
const TimeLineContainer = styled(Box)`
7595
width: 100%;
7696
height: 100px;

0 commit comments

Comments
 (0)