-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuse-swap-canister-lists.ts
38 lines (34 loc) · 1.22 KB
/
use-swap-canister-lists.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { Pair, Token } from '@psychedelic/sonic-js';
import { useEffect, useState } from 'react';
import { useSwapCanisterController } from '.';
/**
* Example of creating a custom hook for swap lists usage
*/
export const useSwapCanisterLists = (): {
tokenList?: Token.MetadataList;
pairList?: Pair.List;
isLoading: boolean;
} => {
// Use the SwapCanisterController hook
const controller = useSwapCanisterController();
// Create stats for the hook
const [tokenList, setTokenList] = useState<Token.MetadataList>();
const [pairList, setPairList] = useState<Pair.List>();
const [isLoading, setIsLoading] = useState<boolean>(false);
/**
* Creating a effect that will be triggered always when controller changes
*/
useEffect(() => {
if (controller) {
setIsLoading(true);
// Getting token and pair lists using the controller and managing states
controller
.getTokenList()
.then((response) => Promise.resolve(setTokenList(response)))
.then(() => controller.getPairList())
.then((response) => setPairList(response))
.finally(() => setIsLoading(false));
}
}, [controller]);
return { tokenList, pairList, isLoading };
};