types#Account TypeScript Examples
The following examples show how to use
types#Account.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Account.tsx From gear-js with GNU General Public License v3.0 | 6 votes |
function AccountProvider({ children }: Props) {
const { api } = useApi();
const [account, setAccount] = useState<Account>();
const getBalance = (balance: Balance) => {
const [value, unit] = balance.toHuman().split(' ');
return { value, unit };
};
const getAccount = (_account: InjectedAccountWithMeta, balance: Balance) => ({
..._account,
balance: getBalance(balance),
decodedAddress: GearKeyring.decodeAddress(_account.address),
});
const switchAccount = (_account: InjectedAccountWithMeta) => {
api?.balance
.findOut(_account.address)
.then((balance) => getAccount(_account, balance))
.then(setAccount);
};
const updateBalance = (balance: Balance) => {
setAccount((prevAccount) => (prevAccount ? { ...prevAccount, balance: getBalance(balance) } : prevAccount));
};
const logout = () => {
setAccount(undefined);
};
const { Provider } = AccountContext;
const value = { account, switchAccount, updateBalance, logout };
return <Provider value={value}>{children}</Provider>;
}
Example #2
Source File: Account.tsx From gear-js with GNU General Public License v3.0 | 6 votes |
function AccountProvider({ children }: ProviderProps) {
const { api } = useContext(ApiContext); // сircular dependency fix
const [account, setAccount] = useState<Account>();
const login = (_account: Account) => {
localStorage.setItem(LOCAL_STORAGE.ACCOUNT, _account.address);
setAccount(_account);
};
const switchAccount = (_account: InjectedAccountWithMeta) => {
api?.balance
.findOut(_account.address)
.then((balance) => getAccount(_account, balance))
.then(login);
};
const updateBalance = (balance: Balance) => {
setAccount((prevAccount) => (prevAccount ? { ...prevAccount, balance: getBalance(balance) } : prevAccount));
};
const logout = () => {
localStorage.removeItem(LOCAL_STORAGE.ACCOUNT);
setAccount(undefined);
};
const { Provider } = AccountContext;
const value = { account, switchAccount, updateBalance, logout };
return <Provider value={value}>{children}</Provider>;
}
Example #3
Source File: Account.tsx From gear-js with GNU General Public License v3.0 | 5 votes |
function AccountProvider({ children }: Props) {
const { api } = useApi();
const [accounts, setAccounts] = useState<InjectedAccountWithMeta[]>();
const [account, setAccount] = useState<Account>();
const getAccounts = (extensions: InjectedExtension[]) => (extensions.length > 0 ? web3Accounts() : undefined);
useEffect(() => {
setTimeout(() => {
web3Enable('Gear App').then(getAccounts).then(setAccounts);
}, 300);
}, []);
const getBalance = (balance: Balance) => {
const [value, unit] = balance.toHuman().split(' ');
return { value, unit };
};
const getAccount = (_account: InjectedAccountWithMeta, balance: Balance) => ({
..._account,
balance: getBalance(balance),
decodedAddress: GearKeyring.decodeAddress(_account.address),
});
const switchAccount = (_account: InjectedAccountWithMeta) => {
api?.balance
.findOut(_account.address)
.then((balance) => getAccount(_account, balance))
.then(setAccount);
};
const logout = () => {
setAccount(undefined);
};
useEffect(() => {
const loggedInAccount = accounts?.find(isLoggedIn);
if (loggedInAccount) switchAccount(loggedInAccount);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [api, accounts]);
const updateBalance = (balance: Balance) => {
setAccount((prevAccount) => ({ ...prevAccount!, balance: getBalance(balance) }));
};
useEffect(() => {
let unsub: UnsubscribePromise | undefined;
if (account) {
unsub = api?.gearEvents.subscribeToBalanceChange(account.address, updateBalance);
}
return () => {
if (unsub) unsub.then((callback) => callback());
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [api, account]);
const { Provider } = AccountContext;
const value = { accounts, account, switchAccount, logout };
return <Provider value={value}>{children}</Provider>;
}