const actionPend = name => ({ type: 'IMG', status: 'PENDING', name });
const actionResolv = (name, payload) => ({ type: 'IMG', status: 'RESOLVED', name, payload });
const actionReject = (name, error) => ({ type: 'IMG', status: 'REJECTED', name, error });

const actionImg = (name, promise) =>
  async (dispatch) => {
    dispatch(actionPend(name));
    try {
      const payload = await promise;
      dispatch(actionResolv(name, payload));
      return payload;
    } catch (error) {
      dispatch(actionReject(name, error));
    }
  };

export default actionImg;
