types#ApiAction TypeScript Examples

The following examples show how to use types#ApiAction. 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: connect.ts    From contracts-ui with GNU General Public License v3.0 6 votes vote down vote up
connect = (endpoint: string, dispatch: React.Dispatch<ApiAction>) => {
  if (!isValidWsUrl(endpoint)) return false;

  dispatch({ type: 'CONNECT_INIT' });

  const provider = new WsProvider(endpoint);
  const _api = new ApiPromise({ provider });

  // Set listeners for disconnection and reconnection event.
  _api.on('connected', async () => {
    dispatch({ type: 'CONNECT', payload: _api });
    // `ready` event is not emitted upon reconnection and is checked explicitly here.
    await _api.isReady;

    dispatch({
      type: 'CONNECT_READY',
      payload: await getChainProperties(_api),
    });
  });

  _api.on('ready', async () => {
    dispatch({
      type: 'CONNECT_READY',
      payload: await getChainProperties(_api),
    });
  });

  _api.on('error', err => dispatch({ type: 'CONNECT_ERROR', payload: err }));
}
Example #2
Source File: loadAccounts.ts    From contracts-ui with GNU General Public License v3.0 6 votes vote down vote up
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 #3
Source File: api.ts    From contracts-ui with GNU General Public License v3.0 6 votes vote down vote up
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`);
  }
}