types#ApiState TypeScript Examples
The following examples show how to use
types#ApiState.
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: loadAccounts.ts From contracts-ui with GNU General Public License v3.0 | 6 votes |
loadAccounts = (state: ApiState, dispatch: React.Dispatch<ApiAction>) => {
const { systemChainType, systemChain } = state;
dispatch({ type: 'LOAD_KEYRING' });
const asyncLoadAccounts = async () => {
try {
await web3Enable('contracts-ui');
let allAccounts = await web3Accounts();
allAccounts = allAccounts.map(({ address, meta }) => ({
address,
meta: { ...meta, name: `${meta.name} (${meta.source})` },
}));
const isDevelopment =
systemChainType.isDevelopment || systemChainType.isLocal || isTestChain(systemChain);
Keyring.loadAll({ isDevelopment }, allAccounts);
dispatch({ type: 'SET_KEYRING', payload: Keyring });
} catch (e) {
console.error(e);
dispatch({ type: 'KEYRING_ERROR' });
}
};
asyncLoadAccounts().catch(e => console.error(e));
}
Example #2
Source File: ApiContext.tsx From contracts-ui with GNU General Public License v3.0 | 6 votes |
ApiContextProvider = ({ children }: React.PropsWithChildren<Partial<ApiState>>) => {
const [searchParams] = useSearchParams();
const rpcUrl = searchParams.get('rpc');
const [preferredEndpoint, setPreferredEndpoint] = useLocalStorage<string>(
'preferredEndpoint',
RPC.LOCAL
);
const [state, dispatch] = useReducer(apiReducer, { ...INIT_STATE, endpoint: preferredEndpoint });
const { endpoint, keyringStatus, status } = state;
useEffect(() => {
if (rpcUrl && isValidWsUrl(rpcUrl) && rpcUrl !== preferredEndpoint) {
dispatch({ type: 'SET_ENDPOINT', payload: rpcUrl });
setPreferredEndpoint(rpcUrl);
window.location.reload();
}
}, [preferredEndpoint, rpcUrl, searchParams, setPreferredEndpoint]);
useEffect((): void => {
connect(endpoint, dispatch);
}, [endpoint]);
useEffect(() => {
if (status === 'READY' && !keyringStatus && !keyringLoadAll) {
keyringLoadAll = true;
loadAccounts(state, dispatch);
}
}, [state, dispatch, status, keyringStatus]);
return <ApiContext.Provider value={state}>{children}</ApiContext.Provider>;
}
Example #3
Source File: api.ts From contracts-ui with GNU General Public License v3.0 | 6 votes |
apiReducer: Reducer<ApiState, ApiAction> = (state, action) => {
switch (action.type) {
case 'SET_ENDPOINT':
return { ...state, endpoint: action.payload, error: null };
case 'CONNECT_INIT':
return { ...state, status: 'CONNECT_INIT' };
case 'CONNECT':
return { ...state, api: action.payload, error: null, status: 'CONNECTING' };
case 'CONNECT_READY':
return { ...state, ...action.payload, error: null, status: 'READY' };
case 'CONNECT_ERROR':
return { ...state, ...NULL_CHAIN_PROPERTIES, status: 'ERROR', error: action.payload };
case 'LOAD_KEYRING':
return { ...state, keyringStatus: 'LOADING' };
case 'SET_KEYRING':
return { ...state, keyring: action.payload, keyringStatus: 'READY' };
case 'KEYRING_ERROR':
return { ...state, keyringStatus: 'ERROR' };
default:
throw new Error(`Unknown action type`);
}
}
Example #4
Source File: index.ts From contracts-ui with GNU General Public License v3.0 | 5 votes |
INIT_STATE: ApiState = {
...NULL_CHAIN_PROPERTIES,
endpoint: RPC.LOCAL,
keyringStatus: null,
error: null,
status: 'CONNECT_INIT',
} as unknown as ApiState