Last active
June 27, 2020 10:36
-
-
Save JenyaIII-sudo/d59bc624c692a958fb351750cb69e133 to your computer and use it in GitHub Desktop.
Actions on Redux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable import/prefer-default-export */ | |
import Axios from 'axios'; | |
import { | |
ADD_USER, | |
GET_USERS, | |
GET_USERS_LOADING, | |
GET_USERS_ERROR, | |
DELETE_USER, | |
CHANGE_MODAL, | |
EDIT_FORM, | |
UPDATE_USER, | |
} from './actionTypes'; | |
export const addUser = (obj) => async (dispatch) => { | |
try { | |
await Axios.post( | |
process.env.REACT_APP_HOST + '/users', obj, | |
); | |
dispatch({ | |
type: ADD_USER, | |
payload: obj, | |
}); | |
} catch (err) { | |
dispatch({ type: GET_USERS_ERROR, payload: err }); | |
console.log(`Error: ${err}`); | |
} | |
}; | |
export const getUsers = () => async (dispatch) => { | |
// dispatch({ type: GET_USERS_LOADING }); | |
try { | |
const userData = await Axios.get( | |
process.env.REACT_APP_HOST + '/users', | |
); | |
console.log('USERDATA', userData); | |
dispatch({ | |
type: GET_USERS, | |
payload: userData, | |
}); | |
} catch (err) { | |
dispatch({ type: GET_USERS_ERROR, payload: err }); | |
console.log(`Error: ${err}`); | |
} | |
}; | |
export const getUsersLoading = () => ({ | |
type: GET_USERS_LOADING, | |
}); | |
export const deleteUser = (id, obj) => async (dispatch) => { | |
try { | |
await Axios.delete( | |
process.env.REACT_APP_HOST + `/users/${id}`, | |
); | |
dispatch({ | |
type: DELETE_USER, | |
payload: obj, | |
}); | |
} catch (err) { | |
dispatch({ type: GET_USERS_ERROR, payload: err }); | |
console.log(`Error: ${err}`); | |
} | |
}; | |
export const openOrCloseModal = (bool) => ({ | |
type: CHANGE_MODAL, | |
payload: bool, | |
}); | |
export const updateUser = (item) => ({ | |
type: UPDATE_USER, | |
payload: item, | |
}); | |
export const editUser = (item) => ({ | |
type: EDIT_FORM, | |
payload: item, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment