Skip to content

Commit 852443c

Browse files
committed
fix(#2332): fix inconsistend display of pending delegation DRep card
1 parent ce48b09 commit 852443c

File tree

5 files changed

+84
-9
lines changed

5 files changed

+84
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ changes.
1919
- Fix listing voted-on governance actions [Issue 2379](https://github.com/IntersectMBO/govtool/issues/2379)
2020
- Fix wronly displayed markdown on slider card [Issue 2263](https://github.com/IntersectMBO/govtool/issues/2316)
2121
- fix ada quantities format to avoid thousands when the total is 0 [Issue 2372](https://github.com/IntersectMBO/govtool/issues/2382)
22+
- fix inconsistent display of delegated DRep card during delegation [Issue 2332](https://github.com/IntersectMBO/govtool/issues/2332)
2223

2324
### Changed
2425

govtool/frontend/src/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export * from "./useDebounce";
44
export * from "./useDelegateToDrep";
55
export * from "./useFetchNextPageDetector";
66
export * from "./useOutsideClick";
7+
export * from "./usePrevious";
78
export * from "./useSaveScrollPosition";
89
export * from "./useScreenDimension";
910
export * from "./useSlider";
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { renderHook } from "@testing-library/react";
2+
import { describe, it, expect } from "vitest";
3+
import usePrevious from "./usePrevious";
4+
5+
describe("usePrevious hook", () => {
6+
it("should return undefined on the initial render", () => {
7+
const { result } = renderHook(() => usePrevious(0));
8+
9+
expect(result.current).toBeUndefined();
10+
});
11+
12+
it("should return the previous value after the state changes", () => {
13+
let value = 0;
14+
15+
const { result, rerender } = renderHook(() => usePrevious(value));
16+
17+
expect(result.current).toBeUndefined();
18+
19+
value = 10;
20+
rerender();
21+
22+
expect(result.current).toBe(0);
23+
24+
value = 20;
25+
rerender();
26+
27+
expect(result.current).toBe(10);
28+
});
29+
30+
it("should handle non-primitive values like objects", () => {
31+
let value = { count: 0 };
32+
33+
const { result, rerender } = renderHook(() => usePrevious(value));
34+
35+
expect(result.current).toBeUndefined();
36+
37+
value = { count: 1 };
38+
rerender();
39+
40+
expect(result.current).toEqual({ count: 0 });
41+
42+
value = { count: 2 };
43+
rerender();
44+
45+
expect(result.current).toEqual({ count: 1 });
46+
});
47+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { useEffect, useRef } from "react";
2+
3+
/**
4+
* Custom React hook to get the previous value of a prop or state.
5+
* @param value The current value to track.
6+
* @returns The previous value of the given input.
7+
*/
8+
function usePrevious<T>(value: T): T | undefined {
9+
const ref = useRef<T>();
10+
11+
useEffect(() => {
12+
ref.current = value; // Update the ref value to the current value after render
13+
}, [value]); // Only run if the value changes
14+
15+
return ref.current; // Return the previous value (ref.current holds the old value)
16+
}
17+
18+
export default usePrevious;

govtool/frontend/src/pages/DRepDirectoryContent.tsx

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FC, useEffect } from "react";
1+
import { FC, useEffect, useState } from "react";
22
import { Trans, useTranslation } from "react-i18next";
33
import { Box, CircularProgress } from "@mui/material";
44

@@ -15,11 +15,12 @@ import {
1515
import { DataActionsBar, EmptyStateDrepDirectory } from "@molecules";
1616
import { AutomatedVotingOptions, DRepCard } from "@organisms";
1717
import { correctAdaFormat, isSameDRep } from "@utils";
18-
import { DRepListSort, DRepStatus } from "@models";
18+
import { DRepData, DRepListSort, DRepStatus } from "@models";
1919
import {
2020
AutomatedVotingOptionCurrentDelegation,
2121
AutomatedVotingOptionDelegationId,
2222
} from "@/types/automatedVotingOptions";
23+
import usePrevious from "@/hooks/usePrevious";
2324

2425
interface DRepDirectoryContentProps {
2526
isConnected?: boolean;
@@ -48,6 +49,9 @@ export const DRepDirectoryContent: FC<DRepDirectoryContentProps> = ({
4849
const { chosenFilters, chosenSorting, setChosenSorting } =
4950
dataActionsBarProps;
5051

52+
const [inProgressDelegationDRepData, setInProgressDelegationDRepData] =
53+
useState<DRepData | undefined>(undefined);
54+
5155
useEffect(() => {
5256
if (!chosenSorting) setChosenSorting(DRepListSort.Random);
5357
}, [chosenSorting, setChosenSorting]);
@@ -57,6 +61,7 @@ export const DRepDirectoryContent: FC<DRepDirectoryContentProps> = ({
5761
const { votingPower } = useGetAdaHolderVotingPowerQuery(stakeKey);
5862
const { currentDelegation } = useGetAdaHolderCurrentDelegationQuery(stakeKey);
5963
const inProgressDelegation = pendingTransaction.delegate?.resourceId;
64+
const prevInProgressDelegation = usePrevious(inProgressDelegation);
6065

6166
const { dRep: myDrep } = useGetDRepDetailsQuery(currentDelegation?.dRepView, {
6267
enabled: !!inProgressDelegation || !!currentDelegation,
@@ -85,6 +90,12 @@ export const DRepDirectoryContent: FC<DRepDirectoryContentProps> = ({
8590
},
8691
);
8792

93+
useEffect(() => {
94+
if (!inProgressDelegation && prevInProgressDelegation) {
95+
setInProgressDelegationDRepData(undefined);
96+
}
97+
}, [prevInProgressDelegation, inProgressDelegation]);
98+
8899
if (
89100
(stakeKey && votingPower === undefined) ||
90101
!dRepList ||
@@ -103,12 +114,6 @@ export const DRepDirectoryContent: FC<DRepDirectoryContentProps> = ({
103114
? [yourselfDRep, ...listedDRepsWithoutYourself]
104115
: listedDRepsWithoutYourself;
105116

106-
const inProgressDelegationDRepData = dRepListToDisplay.find(
107-
(dRep) =>
108-
dRep.drepId === inProgressDelegation ||
109-
dRep.view === inProgressDelegation,
110-
);
111-
112117
const isAnAutomatedVotingOptionChosen =
113118
currentDelegation?.dRepView &&
114119
(currentDelegation?.dRepView ===
@@ -216,7 +221,10 @@ export const DRepDirectoryContent: FC<DRepDirectoryContentProps> = ({
216221
}
217222
isMe={isSameDRep(dRep, myDRepId)}
218223
isMyDrep={isSameDRep(dRep, currentDelegation?.dRepView)}
219-
onDelegate={() => delegate(dRep.drepId)}
224+
onDelegate={() => {
225+
setInProgressDelegationDRepData(dRep);
226+
delegate(dRep.drepId);
227+
}}
220228
/>
221229
</Box>
222230
))}

0 commit comments

Comments
 (0)