Skip to content
Snippets Groups Projects
UserManagementActions.js 7.93 KiB
Newer Older
Orlando Piñero's avatar
sth
Orlando Piñero committed
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';
Orlando Piñero's avatar
Orlando Piñero committed
export const DELETE_USER_SUCCESS = 'DELETE_USER_SUCCESS';
export const DELETE_USER_ERROR = 'DELETE_USER_ERROR';
Orlando Piñero's avatar
Orlando Piñero committed
export const EDIT_USER_ERROR = 'EDIT_USER_ERROR';
export const EDIT_USER_SUCCESS = 'EDIT_USER_SUCCESS';
export const PRESS_EDIT_BUTTON = 'PRESS_EDIT_BUTTON';

Orlando Piñero's avatar
sth
Orlando Piñero committed


//Pending, Success, Error Actions


export function getAllUsersAction(users) {
    console.log("GetAllAction Action")
    return {
        type: SHOW_ALL_USERS,
        userList: users
    }
}

export function getAllUsersErrorAction(error) {
    console.log("Error Action")
    return {
        type: ALL_USERS_ERROR,
        error: error
    }
}

Orlando Piñero's avatar
Orlando Piñero committed
export function getShowDeleteUserModalAction(deleteModalData) {
    console.log(deleteModalData)
Orlando Piñero's avatar
sth
Orlando Piñero committed
    return {
Orlando Piñero's avatar
Orlando Piñero committed
        type: SHOW_DELETE_USER_MODAL,
        deleteModalData: deleteModalData
Orlando Piñero's avatar
sth
Orlando Piñero committed
    }
}

export function getHideDeleteUserModalAction() {
    return {
        type: HIDE_DELETE_USER_MODAL
    }
}

Orlando Piñero's avatar
Orlando Piñero committed
export function getDeleteUserSuccessAction() {
    return {
        type: DELETE_USER_SUCCESS
    }
}

export function getDeleteUserErrorAction(error) {
    return {
        type: DELETE_USER_ERROR,
        error: error
    }
}
Orlando Piñero's avatar
sth
Orlando Piñero committed

export function getCreateNewUserSuccessAction(userData) {
    console.log("GetCreateNewUserSuccessAction")
    return {
        type: CREATE_NEW_USER_SUCCESS,
        newUserData: userData
    }
}

export function getCreateNewUserErrorAction(error) {
    console.log("GetCreateNewUserErrorAction")
    return {
        type: CREATE_NEW_USER_ERROR,
        error: error
    }
}


Orlando Piñero's avatar
Orlando Piñero committed
export function getEditUserSuccessAction(editUserData) {
    console.log("GetEditUserSuccessAction")
    return {
Orlando Piñero's avatar
Orlando Piñero committed
        type: EDIT_USER_SUCCESS
Orlando Piñero's avatar
Orlando Piñero committed
export function getEditUserErrorAction() {
Orlando Piñero's avatar
Orlando Piñero committed
    console.log("GetEditUserSuccessAction")
    return {
Orlando Piñero's avatar
Orlando Piñero committed
        type: EDIT_USER_ERROR
Orlando Piñero's avatar
sth
Orlando Piñero committed

//Frontend Connections

export function displayUsers(token) {
    console.log("displaying users")

    return dispatch => {
        dispatch(getAllUsersAction());
        getAllUsers(token)
            .then(users => {
                console.log(users)
                const action = getAllUsersAction(users)
                dispatch(action);
            },
                error => {
                    dispatch(getAllUsersErrorAction(error));
                }
            )
            .catch(error => {
                dispatch(getAllUsersErrorAction(error))
            })
    }
}

Orlando Piñero's avatar
Orlando Piñero committed
export function editUserWithID(updateData, userID, token) {
Orlando Piñero's avatar
Orlando Piñero committed
    console.log("editing user")

    return dispatch => {
        dispatch(getEditUserSuccessAction());
Orlando Piñero's avatar
Orlando Piñero committed
        updateUser(updateData, userID, token)
Orlando Piñero's avatar
Orlando Piñero committed
            .then(user => {
                console.log(user)
                const action = getEditUserSuccessAction(user)
                dispatch(action);
Orlando Piñero's avatar
Orlando Piñero committed
                getAllUsers(token)
                    .then(users => {
                        const action = getAllUsersAction(users)
                        dispatch(action)
                    })
Orlando Piñero's avatar
Orlando Piñero committed
            },
                error => {
                    dispatch(getEditUserErrorAction(error));
                }
            )
            .catch(error => {
                dispatch(getEditUserErrorAction(error))
            })
    }
}

Orlando Piñero's avatar
sth
Orlando Piñero committed

export function passNewUser(userData, token) {
    console.log("passing new user")

    return dispatch => {
        dispatch(getCreateNewUserSuccessAction());
        createNewUser(userData, token)
            .then(newUser => {
                console.log(newUser)
                const action = getCreateNewUserSuccessAction(newUser)
                dispatch(action);
Orlando Piñero's avatar
Orlando Piñero committed
                getAllUsers(token)
                    .then(users => {
                        const action = getAllUsersAction(users)
                        dispatch(action)
                    })
Orlando Piñero's avatar
sth
Orlando Piñero committed
            },
                error => {
                    dispatch(getCreateNewUserErrorAction(error));
                }
            )
            .catch(error => {
                dispatch(getCreateNewUserErrorAction(error))
            })
    }
Orlando Piñero's avatar
Orlando Piñero committed
}

export function deleteUserWithID(userID, token) {
    console.log("deleting user")
Orlando Piñero's avatar
sth
Orlando Piñero committed

Orlando Piñero's avatar
Orlando Piñero committed
    return dispatch => {
        dispatch(getDeleteUserSuccessAction(userID));
        deleteUser(userID, token)
            .then(deleteUser => {
                console.log(deleteUser)
                const action = getDeleteUserSuccessAction(deleteUser)
                dispatch(action);
Orlando Piñero's avatar
Orlando Piñero committed
                getAllUsers(token)
                    .then(users => {
                        const action = getAllUsersAction(users)
                        dispatch(action)
                    })
Orlando Piñero's avatar
Orlando Piñero committed
            },
                error => {
Orlando Piñero's avatar
Orlando Piñero committed
                    console.log(error)
Orlando Piñero's avatar
Orlando Piñero committed
                    dispatch(getDeleteUserErrorAction(error));
                }
            )
            .catch(error => {
                dispatch(getDeleteUserErrorAction(error))
            })
    }
Orlando Piñero's avatar
sth
Orlando Piñero committed
}


//Backend Callings

export function getAllUsers(token) {
    console.log("get all users")
    console.log(token)

    const requestOptions = {
        method: 'GET',
        headers: { 'Authorization': token }
    };

    return fetch('https://localhost/api/users', requestOptions)
        .then(response => {
            return response.json().then(body => {
                if (response.status === 200) {
                    console.log(body)
                    return body
                }
            })
        })
}

Orlando Piñero's avatar
Orlando Piñero committed
export function getOneUser(userID, token) {
    console.log("get one user")

    const requestOptions = {
        method: 'GET',
        headers: { 'Authorization': token }
    };

    return fetch(`https://localhost/api/users/${userID}`, requestOptions)
        .then(response => {
            return response.json().then(body => {
                if (response.status === 200) {
                    console.log(body)
                    return body
                }
            })
        })
}

Orlando Piñero's avatar
sth
Orlando Piñero committed

export function createNewUser(userData, token) {
    console.log("create new user")
    console.log(userData)

    const requestOptions = {
        method: 'POST',
        headers: {
            'Authorization': token,
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(userData)
    }

    return fetch('https://localhost/api/users', requestOptions)
        .then(response => {
            return response.json().then(body => {
Orlando Piñero's avatar
Orlando Piñero committed
                if (response.status === 201) {
Orlando Piñero's avatar
sth
Orlando Piñero committed
                    console.log(body)
                    return body
                }
            })
        })
}

Orlando Piñero's avatar
Orlando Piñero committed
export function deleteUser(userID, token) {
    console.log("delete user")
    console.log(userID)

    const requestOptions = {
        method: 'DELETE',
        headers: {
            'Authorization': token
        }
    }

    return fetch('https://localhost/api/users/' + userID, requestOptions)
        .then(response => {
Orlando Piñero's avatar
Orlando Piñero committed
            console.log(response)
            if (response.status === 204) {
                return response.status
            }
Orlando Piñero's avatar
Orlando Piñero committed

export function updateUser(updateData, userID, token) {
    console.log("updating user with id: " + userID)
    console.log(userID)
    console.log(updateData)
    const requestOptions = {
        method: 'PUT',
        headers: {
            'Authorization': token,
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(updateData)
    }

    return fetch(`https://localhost/api/users/${userID}`, requestOptions)
        .then(response => {
            console.log(response)
            return response.json().then(body => {
                if (response.status === 200) {
                    console.log(body)
                    return body
                }
            })
        })
}