export const SHOW_DELETE_USER_MODAL = 'SHOW_DELETE_USER_MODAL'; export const HIDE_DELETE_USER_MODAL = 'HIDE_DELETE_USER_MODAL'; export const SHOW_ALL_USERS = 'SHOW_ALL_USERS'; export const ALL_USERS_ERROR = 'ALL_USERS_ERROR'; export const CREATE_NEW_USER_SUCCESS = 'CREATE_NEW_USER_SUCCESS'; export const CREATE_NEW_USER_ERROR = 'CREATE_NEW_USER_ERROR'; export const DELETE_USER_SUCCESS = 'DELETE_USER_SUCCESS'; export const DELETE_USER_ERROR = 'DELETE_USER_ERROR'; export const EDIT_USER_ERROR = 'EDIT_USER_ERROR'; export const EDIT_USER_SUCCESS = 'EDIT_USER_SUCCESS'; export const PRESS_EDIT_BUTTON = 'PRESS_EDIT_BUTTON'; const URL = process.env.REACT_APP_SERVER //Pending, Success, Error Actions export function getAllUsersAction(users) { return { type: SHOW_ALL_USERS, userList: users } } export function getAllUsersErrorAction(error) { return { type: ALL_USERS_ERROR, error: error } } export function getShowDeleteUserModalAction(deleteModalData) { return { type: SHOW_DELETE_USER_MODAL, deleteModalData: deleteModalData } } export function getHideDeleteUserModalAction() { return { type: HIDE_DELETE_USER_MODAL } } export function getDeleteUserSuccessAction() { return { type: DELETE_USER_SUCCESS } } export function getDeleteUserErrorAction(error) { return { type: DELETE_USER_ERROR, error: error } } export function getCreateNewUserSuccessAction(userData) { return { type: CREATE_NEW_USER_SUCCESS, newUserData: userData } } export function getCreateNewUserErrorAction(error) { return { type: CREATE_NEW_USER_ERROR, error: error } } export function getEditUserSuccessAction() { return { type: EDIT_USER_SUCCESS } } export function getEditUserErrorAction() { return { type: EDIT_USER_ERROR } } export function displayUsers(token) { return dispatch => { dispatch(getAllUsersAction()); getAllUsers(token) .then(users => { const action = getAllUsersAction(users) dispatch(action); }, error => { dispatch(getAllUsersErrorAction(error)); } ) .catch(error => { dispatch(getAllUsersErrorAction(error)) }) } } export function editUserWithID(updateData, userID, token) { return dispatch => { dispatch(getEditUserSuccessAction()); updateUser(updateData, userID, token) .then(user => { const action = getEditUserSuccessAction(user) dispatch(action); getAllUsers(token) .then(users => { const action = getAllUsersAction(users) dispatch(action) }) }, error => { dispatch(getEditUserErrorAction(error)); } ) .catch(error => { dispatch(getEditUserErrorAction(error)) }) } } export function passNewUser(userData, token) { return dispatch => { dispatch(getCreateNewUserSuccessAction()); createNewUser(userData, token) .then(newUser => { const action = getCreateNewUserSuccessAction(newUser) dispatch(action); getAllUsers(token) .then(users => { const action = getAllUsersAction(users) dispatch(action) }) }, error => { dispatch(getCreateNewUserErrorAction(error)); } ) .catch(error => { dispatch(getCreateNewUserErrorAction(error)) }) } } export function deleteUserWithID(userID, token) { return dispatch => { dispatch(getDeleteUserSuccessAction(userID)); deleteUser(userID, token) .then(deleteUser => { const action = getDeleteUserSuccessAction(deleteUser) dispatch(action); getAllUsers(token) .then(users => { const action = getAllUsersAction(users) dispatch(action) }) }, error => { dispatch(getDeleteUserErrorAction(error)); } ) .catch(error => { dispatch(getDeleteUserErrorAction(error)) }) } } //Backend Callings export function getAllUsers(token) { const requestOptions = { method: 'GET', headers: { 'Authorization': token } }; return fetch(URL + '/users', requestOptions) .then(response => { return response.json().then(body => { if (response.status === 200) { return body } }) }) } export function getOneUser(userID, token) { const requestOptions = { method: 'GET', headers: { 'Authorization': token } }; return fetch(URL + `/users/${userID}`, requestOptions) .then(response => { return response.json().then(body => { if (response.status === 200) { return body } }) }) } export function createNewUser(userData, token) { const requestOptions = { method: 'POST', headers: { 'Authorization': token, 'Content-Type': 'application/json' }, body: JSON.stringify(userData) } return fetch(URL + '/users', requestOptions) .then(response => { return response.json().then(body => { if (response.status === 201) { return body } }) }) } export function deleteUser(userID, token) { const requestOptions = { method: 'DELETE', headers: { 'Authorization': token } } return fetch(URL + '/users/' + userID, requestOptions) .then(response => { if (response.status === 204) { return response.status } }) } export function updateUser(updateData, userID, token) { const requestOptions = { method: 'PUT', headers: { 'Authorization': token, 'Content-Type': 'application/json' }, body: JSON.stringify(updateData) } return fetch(URL + `/users/${userID}`, requestOptions) .then(response => { return response.json().then(body => { if (response.status === 200) { return body } }) }) }