import axios from 'axios';
import { settingsTypes } from '../../action-types/settings.types';
export const updatePassword = passwordDetails => async dispatch => {
  const url = 'auth/update-password';

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

    if (success) {
      dispatch({
        type: settingsTypes.RESET_PASSWORD,
        payload: data,
      });
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};
export const updateUserProfile = payload => async dispatch => {
  const url = 'profile';

  try {
    const profileData = await axios.put(url, payload);
    const { success, data } = profileData.data;

    if (success) {
      dispatch({
        type: settingsTypes.UPDATE_PROFILE,
        payload: data,
      });
      return profileData.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};
export const getUserProfile = () => async dispatch => {
  const url = 'profile';

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

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