Skip to content

Commit

Permalink
Remove all dApp records from BeaconSlice on removing a connection
Browse files Browse the repository at this point in the history
  • Loading branch information
asiia-trilitech committed Mar 11, 2024
1 parent 68c154f commit 23343ce
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 39 deletions.
3 changes: 0 additions & 3 deletions src/utils/beacon/BeaconPeers.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,6 @@ describe("<BeaconPeers />", () => {
[peersData[0].senderId]: {
[mockMnemonicAccount(1).address.pkh]: NetworkType.MAINNET,
},
[peersData[1].senderId]: {
[mockMnemonicAccount(5).address.pkh]: NetworkType.FLORENCENET,
},
[peersData[2].senderId]: {
[mockMnemonicAccount(2).address.pkh]: NetworkType.CUSTOM,
},
Expand Down
2 changes: 1 addition & 1 deletion src/utils/beacon/BeaconPeers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ const PeerRow = ({ peerInfo, accountPkh }: { peerInfo: ExtendedPeerInfo; account
<IconButton
aria-label="Remove Peer"
icon={<TrashIcon />}
onClick={() => removePeer(peerInfo, accountPkh)}
onClick={() => removePeer(peerInfo)}
size="xs"
variant="circle"
/>
Expand Down
13 changes: 3 additions & 10 deletions src/utils/beacon/beacon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { useQuery, useQueryClient } from "@tanstack/react-query";

import { makePeerInfo } from "./types";
import { WalletClient } from "./WalletClient";
import { RawPkh } from "../../types/Address";
import { useRemoveConnection } from "../hooks/beaconHooks";

const PEERS_QUERY_KEY = "beaconPeers";
Expand All @@ -25,16 +24,10 @@ export const useRemovePeer = () => {
const refresh = useRefreshPeers();
const removeConnectionFromBeaconSlice = useRemoveConnection();

return (peerInfo: ExtendedPeerInfo, accountPkh?: RawPkh) => {
if (!accountPkh) {
// No saved data for this dApp, just remove the peer connection.
return WalletClient.removePeer(peerInfo as ExtendedP2PPairingResponse).then(refresh);
}
// Remove both: peer connection and saved data.
return WalletClient.removePeer(peerInfo as ExtendedP2PPairingResponse)
.then(() => removeConnectionFromBeaconSlice(peerInfo.senderId, accountPkh))
return (peerInfo: ExtendedPeerInfo) =>
WalletClient.removePeer(peerInfo as ExtendedP2PPairingResponse)
.then(() => removeConnectionFromBeaconSlice(peerInfo.senderId))
.then(refresh);
};
};

export const useAddPeer = () => {
Expand Down
3 changes: 1 addition & 2 deletions src/utils/hooks/beaconHooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,9 @@ describe("useRemoveConnection", () => {
const {
result: { current: removeConnection },
} = renderHook(() => useRemoveConnection());
removeConnection(dAppId1, pkh1);
removeConnection(dAppId1);

expect(store.getState().beacon).toEqual({
[dAppId1]: { [pkh3]: NetworkType.CUSTOM },
[dAppId2]: { [pkh2]: NetworkType.GHOSTNET },
});
});
Expand Down
5 changes: 2 additions & 3 deletions src/utils/hooks/beaconHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ export const useAddConnection = () => {
};

/**
* Returns function for removing connection from {@link beaconSlice}.
* Returns function for removing connections from {@link beaconSlice}.
*/
export const useRemoveConnection = () => {
const dispatch = useDispatch();
return (dAppId: string, accountPkh: RawPkh) =>
dispatch(beaconSlice.actions.removeConnection({ dAppId, accountPkh }));
return (dAppId: string) => dispatch(beaconSlice.actions.removeConnection({ dAppId }));
};
2 changes: 1 addition & 1 deletion src/utils/redux/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ export const accountsMigrations = {
}
});
}),
3: identity,
3: identity,
} as any;
16 changes: 2 additions & 14 deletions src/utils/redux/slices/beaconSlice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,12 @@ describe("Beacon slice", () => {
});
});

it("removes account record on removing connection", () => {
it("removes dApp record on removing connection", () => {
addConnection(dAppId1, pkh1, NetworkType.MAINNET);
addConnection(dAppId2, pkh2, NetworkType.GHOSTNET);
addConnection(dAppId1, pkh3, NetworkType.CUSTOM);

store.dispatch(beaconActions.removeConnection({ dAppId: dAppId1, accountPkh: pkh1 }));

expect(store.getState().beacon).toEqual({
[dAppId1]: { [pkh3]: NetworkType.CUSTOM },
[dAppId2]: { [pkh2]: NetworkType.GHOSTNET },
});
});

it("removes dApp record on removing connections", () => {
addConnection(dAppId1, pkh1, NetworkType.MAINNET);
addConnection(dAppId2, pkh2, NetworkType.GHOSTNET);

store.dispatch(beaconActions.removeConnection({ dAppId: dAppId1, accountPkh: pkh1 }));
store.dispatch(beaconActions.removeConnection({ dAppId: dAppId1 }));

expect(store.getState().beacon).toEqual({
[dAppId2]: { [pkh2]: NetworkType.GHOSTNET },
Expand Down
7 changes: 2 additions & 5 deletions src/utils/redux/slices/beaconSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@ export const beaconSlice = createSlice({
setWith(state, [payload.dAppId, payload.accountPkh], payload.networkType);
},

removeConnection: (state, { payload }: { payload: { dAppId: string; accountPkh: RawPkh } }) => {
delete state[payload.dAppId][payload.accountPkh];
if (Object.keys(state[payload.dAppId]).length === 0) {
delete state[payload.dAppId];
}
removeConnection: (state, { payload }: { payload: { dAppId: string } }) => {
delete state[payload.dAppId];
},
},
});
Expand Down

0 comments on commit 23343ce

Please sign in to comment.