-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui): implement redux store for wallet connections
- Loading branch information
Vu Van Duc
authored and
Vu Van Duc
committed
May 6, 2024
1 parent
07d03a9
commit 932bc10
Showing
9 changed files
with
185 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./walletConnectionsCache"; | ||
export * from "./walletConnectionsCache.types"; |
101 changes: 101 additions & 0 deletions
101
src/store/reducers/walletConnectionsCache/walletConnectionsCache.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { PayloadAction } from "@reduxjs/toolkit"; | ||
import { RootState } from "../../index"; | ||
import { | ||
getConnectedWallet, | ||
getWalletConnectionsCache, | ||
setConnectedWallet, | ||
setWalletConnectionsCache, | ||
walletConnectionsCacheSlice, | ||
} from "./walletConnectionsCache"; | ||
import { | ||
ConnectionData, | ||
WalletConnectState, | ||
} from "./walletConnectionsCache.types"; | ||
|
||
describe("walletConnectionsCacheSlice", () => { | ||
const initialState: WalletConnectState = { | ||
walletConnections: [], | ||
connectedWallet: null, | ||
}; | ||
|
||
it("should return the initial state", () => { | ||
expect( | ||
walletConnectionsCacheSlice.reducer(undefined, {} as PayloadAction) | ||
).toEqual(initialState); | ||
}); | ||
|
||
it("should handle setWalletConnectionsCache", () => { | ||
const connections: ConnectionData[] = [ | ||
{ | ||
id: 2, | ||
name: "Wallet name #2", | ||
owner: "Yoroi", | ||
url: "ED4KeyyTKFj-72B008OTGgDCrFo6y7B2B73kfyzu5Inb", | ||
}, | ||
]; | ||
const newState = walletConnectionsCacheSlice.reducer( | ||
initialState, | ||
setWalletConnectionsCache(connections) | ||
); | ||
expect(newState.walletConnections).toEqual(connections); | ||
}); | ||
it("should handle setConnectedWallet", () => { | ||
const connection: ConnectionData = { | ||
id: 2, | ||
name: "Wallet name #2", | ||
owner: "Yoroi", | ||
url: "ED4KeyyTKFj-72B008OTGgDCrFo6y7B2B73kfyzu5Inb", | ||
}; | ||
const newState = walletConnectionsCacheSlice.reducer( | ||
initialState, | ||
setConnectedWallet(connection) | ||
); | ||
expect(newState.connectedWallet).toEqual(connection); | ||
}); | ||
}); | ||
|
||
describe("Get wallet connections cache", () => { | ||
it("should return the wallet connetions cache from RootState", () => { | ||
const state = { | ||
walletConnectionsCache: { | ||
walletConnections: [ | ||
{ | ||
id: 1, | ||
name: "Wallet name #1", | ||
owner: "Nami", | ||
image: "", | ||
url: "ED4KeyyTKFj-72B008OTGgDCrFo6y7B2B73kfyzu5Inb", | ||
}, | ||
{ | ||
id: 2, | ||
name: "Wallet name #2", | ||
owner: "Yoroi", | ||
url: "ED4KeyyTKFj-72B008OTGgDCrFo6y7B2B73kfyzu5Inb", | ||
}, | ||
], | ||
}, | ||
} as RootState; | ||
const connectionCache = getWalletConnectionsCache(state); | ||
expect(connectionCache).toEqual( | ||
state.walletConnectionsCache.walletConnections | ||
); | ||
}); | ||
|
||
it("should return connected wallet from RootState", () => { | ||
const state = { | ||
walletConnectionsCache: { | ||
connectedWallet: { | ||
id: 1, | ||
name: "Wallet name #1", | ||
owner: "Nami", | ||
image: "", | ||
url: "ED4KeyyTKFj-72B008OTGgDCrFo6y7B2B73kfyzu5Inb", | ||
}, | ||
}, | ||
} as RootState; | ||
const connectionCache = getConnectedWallet(state); | ||
expect(connectionCache).toEqual( | ||
state.walletConnectionsCache.connectedWallet | ||
); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
src/store/reducers/walletConnectionsCache/walletConnectionsCache.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { createSlice, PayloadAction } from "@reduxjs/toolkit"; | ||
import { RootState } from "../../index"; | ||
import { | ||
ConnectionData, | ||
WalletConnectState, | ||
} from "./walletConnectionsCache.types"; | ||
|
||
const initialState: WalletConnectState = { | ||
walletConnections: [], | ||
connectedWallet: null, | ||
}; | ||
const walletConnectionsCacheSlice = createSlice({ | ||
name: "walletConnectionsCache", | ||
initialState, | ||
reducers: { | ||
setWalletConnectionsCache: ( | ||
state, | ||
action: PayloadAction<ConnectionData[]> | ||
) => { | ||
state.walletConnections = action.payload; | ||
}, | ||
setConnectedWallet: ( | ||
state, | ||
action: PayloadAction<ConnectionData | null> | ||
) => { | ||
state.connectedWallet = action.payload; | ||
}, | ||
}, | ||
}); | ||
|
||
export { initialState, walletConnectionsCacheSlice }; | ||
|
||
export const { setWalletConnectionsCache, setConnectedWallet } = | ||
walletConnectionsCacheSlice.actions; | ||
|
||
const getWalletConnectionsCache = (state: RootState) => | ||
state.walletConnectionsCache.walletConnections; | ||
|
||
const getConnectedWallet = (state: RootState) => | ||
state.walletConnectionsCache.connectedWallet; | ||
|
||
export { getWalletConnectionsCache, getConnectedWallet }; |
15 changes: 15 additions & 0 deletions
15
src/store/reducers/walletConnectionsCache/walletConnectionsCache.types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// TODO: mock data type for connect wallet ui. Need update after core function completed. | ||
interface ConnectionData { | ||
id: number; | ||
name: string; | ||
owner: string; | ||
image?: string; | ||
url: string; | ||
} | ||
|
||
interface WalletConnectState { | ||
walletConnections: ConnectionData[]; | ||
connectedWallet: ConnectionData | null; | ||
} | ||
|
||
export type { ConnectionData, WalletConnectState }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters