import axios from 'axios';
import { usersActionTypes } from '../../action-types/users-action.types';
/**
 * @description: get all Users based on the passed in types
 * type: String - User type. Must be one of ['workspace', 'form', 'all']. (default: workspace)
 * @param {type} getAllUsers
 */

export const getAllUsers = type => async dispatch => {
  const url = `/users?type=${type}`;

  try {
    const response = await axios.get(url);
    const { success, data } = response.data;

    if (success) {
      dispatch({
        type: usersActionTypes.GET_ALL_USERS,
        payload: data,
      });
      return data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};
