text: import axios from "axios";
  import api from '../utils/api';
  import { setAlert } from "./alert"
  import { GET_PROFILE, PROFILE_ERROR, UPDATE_PROFILE, CLEAR_PROFILE, DELETE_ACCOUNT, GET_PROFILES ,GET_REPOS,NO_REPOS} from "./types"
  
  // get all profiles
  export const getAllProfiles = () => async dispatch => {
      dispatch({ type: CLEAR_PROFILE })
      try {
          const res = await axios.get("http://localhost:5000/api/profile/");
          dispatch({
              type: GET_PROFILES,
              payload: res.data
          })
      } catch (err) {
          dispatch({
              type: PROFILE_ERROR,
              payload: {
                  msg: err.response.statusText,
                  status: err.response.status
              }
  
          })
      }
  }
  
  // Get profile by ID
  export const getProfileById = userId => async dispatch => {
      try {
          const res = await axios.get(`http://localhost:5000/api/profile/user/${userId}`);
  
          dispatch({
              type: GET_PROFILE,
              payload: res.data
          });
      } catch (err) {
          dispatch({
              type: PROFILE_ERROR,
              payload: { msg: err.response.statusText, status: err.response.status }
          });
      }
  };
  
  
  
  export const getCurrentProfile = () => async dispatch => {
      try {
          const res = await axios.get("http://localhost:5000/api/profile/me");
          console.log(res.data)
          dispatch({
              type: GET_PROFILE,
              payload: res.data
          })
  
      } catch (err) {
          console.error(err.response.data)
          dispatch({
              type: PROFILE_ERROR,
              payload: {
                  msg: err.response.statusText,
                  status: err.response.status
              }
          })
      }
  
  }
  // Create or update profile
  export const createProfile = (
      formData,
      history,
      edit = false
  ) => async dispatch => {
      try {
          const config = {
              headers: {
                  'Content-Type': 'application/json'
              }
          };
  
          const res = await axios.post('http://localhost:5000/api/profile/', formData, config);
  
          dispatch({
              type: GET_PROFILE,
              payload: res.data
          });
  
          dispatch(setAlert(edit ? 'Profile Updated' : 'Profile Created', 'success'));
  
  
          history.push('/dashboard');
  
      } catch (err) {
          const errors = err.response.data.errors;
  
          if (errors) {
              errors.forEach(error => dispatch(setAlert(error.msg, 'danger')));
          }
  
          dispatch({
              type: PROFILE_ERROR,
              payload: { msg: err.response.statusText, status: err.response.status }
          });
      }
  };
  
  //add an experience
  export const addExperience = (formData, history) => async dispatch => {
      try {
          const config = {
              headers: {
                  'Content-Type': 'application/json'
              }
          };
  
          const res = await axios.put('http://localhost:5000/api/profile/experience', formData, config);
          console.log(res.data)
          dispatch({
              type: UPDATE_PROFILE,
              payload: res.data
          });
  
          dispatch(setAlert('Experience added', 'success'));
  
          history.push('/dashboard');
  
      } catch (err) {
          const errors = err.response.data.errors;
          console.error(err.response.data)
  
          if (errors) {
              errors.forEach(error => dispatch(setAlert(error.msg, 'danger')));
          }
  
          dispatch({
              type: PROFILE_ERROR,
              payload: { msg: err.response.statusText, status: err.response.status }
          });
      }
  }
  
  //add an education
  export const addEducation = (formData, history) => async dispatch => {
      try {
          const config = {
              headers: {
                  'Content-Type': 'application/json'
              }
          };
  
          const res = await axios.put('http://localhost:5000/api/profile/education', formData, config);
          console.log(res.data)
          dispatch({
              type: UPDATE_PROFILE,
              payload: res.data
          });
  
          dispatch(setAlert('Education added', 'success'));
  
          history.push('/dashboard');
  
      } catch (err) {
          const errors = err.response.data.errors;
          console.log(err.message)
          if (errors) {
              errors.forEach(error => dispatch(setAlert(error.msg, 'danger')));
          }
  
          dispatch({
              type: PROFILE_ERROR,
              payload: { msg: err.response.statusText, status: err.response.status }
          });
      }
  }
  
  // Delete an experience
  export const deleteExperience = id => async dispatch => {
      try {
          const res = await axios.delete(`http://localhost:5000/api/profile/experience/${id}`)
          dispatch({
              type: UPDATE_PROFILE,
              payload: res.data
          })
          dispatch(setAlert('Experience Deleted', 'success'));
  
      } catch (err) {
          dispatch({
              type: PROFILE_ERROR,
              payload: { msg: err.response.statusText, status: err.response.status }
          });
      }
  }
  
  // Delete an education
  export const deleteEducation = id => async dispatch => {
      try {
          const res = await axios.delete(`http://localhost:5000/api/profile/education/${id}`)
          dispatch({
              type: UPDATE_PROFILE,
              payload: res.data
          })
          dispatch(setAlert('Education Deleted', 'success'));
  
      } catch (err) {
          dispatch({
              type: PROFILE_ERROR,
              payload: { msg: err.response.statusText, status: err.response.status }
          });
      }
  }
  
  
  // Delete an account 
  export const deleteAccount = id => async dispatch => {
      if (window.confirm('Are you sure this cannot be undone?')) {
          try {
              const res = await axios.delete('http://localhost:5000/api/profile/')
              dispatch({
                  type: CLEAR_PROFILE,
              })
              dispatch({
                  type: DELETE_ACCOUNT,
              })
              dispatch(setAlert('Account has been deleted Permanently', 'success'));
          }
          catch (err) {
              dispatch({
                  type: PROFILE_ERROR,
                  payload: { msg: err.response.statusText, status: err.response.status }
              });
          }
  
      }
  
  }
  
  
  
  //to apply for collab
  export const collabApply = (formData,history, id) => async dispatch => {
      try {
          const config = {
              headers: {
                  'Content-Type': 'application/json'
              }
          };
  
          const res = await axios.put(`http://localhost:5000/api/profile/collaborate/${id}`, formData, config);
          console.log(res.data)
          dispatch({
              type: UPDATE_PROFILE,
              payload: res.data
          });
  
          dispatch(setAlert('Request Has been Sent,wait for response!!', 'success'));
  
          history.push('/dashboard');
  
      } catch (err) {
          const errors = err.response.data.errors;
          console.error(err.response.data)
  
          if (errors) {
              errors.forEach(error => dispatch(setAlert(error.msg, 'danger')));
          }
  
          dispatch({
              type: PROFILE_ERROR,
              payload: { msg: err.response.statusText, status: err.response.status }
          });
      }
  }
  
  
  // Get Github repos
  export const getGithubRepos = username => async dispatch => {
      try {
        const res = await api.get(`/profile/github/${username}`);
    
        dispatch({
          type: GET_REPOS,
          payload: res.data
        });
      } catch (err) {
        dispatch({
          type: NO_REPOS
        });
      }
    };
    