fetchHandler

You can handle error using catch on promise as we do in fetch with eventrix section or use fetchHandler. Fetch handler is small midlerware for fetch method that emit events when fetch success or fetch error.

import { fetchToStateReceiver, fetchHandler } from 'eventrix';
import axios from 'axios';

const fetchUsersMethod = (eventData, eventrixState, emit) => {
  return axios.get(https://somedomain.com/users, { params: eventData })
              .then((users) => {
                emit('fetchUsers.success', users);
                return users
              })
              .catch((error) => {
                emit('fetchUsers.error', error);
              });
};

const fetchUserMethodWithHandler = fetchHandler(
  fetchUsersMethod,
  {
    success: {
      eventName: 'fetchUsers.success',
      data: 'Users list loaded',
    },
    error: {
      eventName: 'fetchUsers.failed',
      data: 'Users list load failed',
    },
  }
);

const fetchReceiver = fetchToStateReceiver('fetchUsers', 'users', fetchUserMethodWithHandler);

eventrix.useReceiver(fetchReceiver);

eventrix.emit('fetchUsers', { search: 'johny' });

Last updated