Skip to content
Snippets Groups Projects
DegreeCourseManagementActions.js 7.1 KiB
Newer Older
export const SHOW_ALL_DEGREECOURSES = 'SHOW_ALL_DEGREECOURSES';
export const SHOW_ALL_DEGREECOURSES_ERROR = 'SHOW_ALL_DEGREECOURSES_ERROR';
export const CREATE_NEW_DEGREECOURSE_SUCCESS = 'CREATE_NEW_DEGREECOURSE_SUCCESS';
export const CREATE_NEW_DEGREECOURSE_ERROR = 'CREATE_NEW_DEGREECOURSE_ERROR';
export const EDIT_DEGREECOURSE_SUCCESS = 'EDIT_DEGREECOURSE_SUCCESS';
export const EDIT_DEGREECOURSE_ERROR = 'EDIT_DEGREECOURSE_ERROR';
export const DELETE_DEGREECOURSE_SUCCESS = 'DELETE_DEGREECOURSE_SUCCESS';
export const DELETE_DEGREECOURSE_ERROR = 'DELETE_DEGREECOURSE_ERROR'

export const SHOW_DELETE_DEGREECOURSE_MODAL = 'SHOW_DELETE_DEGREECOURSE_MODAL'
export const HIDE_DELETE_DEGREECOURSE_MODAL = 'HIDE_DELETE_DEGREECOURSE_MODAL'
export const PRESS_EDIT_BUTTON = 'PRESS_EDIT_BUTTON';

Orlando Piñero's avatar
Orlando Piñero committed
const URL = process.env.REACT_APP_SERVER

export function getShowDeleteDegreeCourseModalAction(deleteModalData) {
    return {
        type: SHOW_DELETE_DEGREECOURSE_MODAL,
        deleteDegreeCourseModalData: deleteModalData
    }
}

export function getHideDeleteDegreeCourseModalAction() {
    return {
        type: HIDE_DELETE_DEGREECOURSE_MODAL
    }
}


export function getCreateNewDegreeCourseSuccessAction(degreeCourseData) {
    return {
        type: CREATE_NEW_DEGREECOURSE_SUCCESS,
        newDegreeCourseData: degreeCourseData
    }
}

export function getCreateNewDegreeCourseErrorAction(error) {
    return {
        type: CREATE_NEW_DEGREECOURSE_ERROR,
        error: error
    }
}

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

Orlando Piñero's avatar
Orlando Piñero committed
    return fetch(URL + '/degreeCourses', requestOptions)
        .then(response => {
            return response.json().then(body => {
                if (response.status === 201) {
                    return body
                }
            })
        })
}

export function passNewDegreeCourse(degreeCourseData, token) {
    return dispatch => {
        dispatch(getCreateNewDegreeCourseSuccessAction());
        createNewDegreeCourse(degreeCourseData, token)
            .then(newDegreeCourse => {
                const action = getCreateNewDegreeCourseSuccessAction(newDegreeCourse)
                dispatch(action);
                getAllDegreeCourses(token)
                    .then(degreeCourses => {
                        const action = getAllDegreeCoursesAction(degreeCourses)
                        dispatch(action)
                    })
            },
                error => {
                    dispatch(getCreateNewDegreeCourseErrorAction(error));
                }
            )
            .catch(error => {
                dispatch(getCreateNewDegreeCourseErrorAction(error))
            })
    }
}




export function getAllDegreeCoursesAction(courses) {
    return {
        type: SHOW_ALL_DEGREECOURSES,
        degreeCourseList: courses
    }
}

export function getAllDegreeCoursesErrorAction(error) {
    return {
        type: SHOW_ALL_DEGREECOURSES_ERROR,
        error: error
    }
}


export function getAllDegreeCourses(token) {
    
    const requestOptions = {
        method: 'GET',
        headers: { 'Authorization': token }
    };
    
Orlando Piñero's avatar
Orlando Piñero committed
    return fetch(URL + '/degreeCourses', requestOptions)
    .then(response => {
        return response.json().then(body => {
            if (response.status === 200) {
                return body
            }
        })
    })
}

export function displayDegreeCourses(token) {
    return dispatch => {
        dispatch(getAllDegreeCoursesAction());
        getAllDegreeCourses(token)
        .then(courses => {
            const action = getAllDegreeCoursesAction(courses)
            dispatch(action);
        },
            error => {
                dispatch(getAllDegreeCoursesErrorAction(error));
            }
        )
        .catch(error => {
            dispatch(getAllDegreeCoursesErrorAction(error))
        })
    }
}


export function getEditDegreeCourseSuccessAction() {
    return {
        type: EDIT_DEGREECOURSE_SUCCESS
    }
}

export function getEditDegreeCourseErrorAction() {
    return {
        type: EDIT_DEGREECOURSE_ERROR
    }
}

export function updateDegreeCourse(updateData, degreeCourseID, token) {
    const requestOptions = {
        method: 'PUT',
        headers: {
            'Authorization': token,
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(updateData)
    }

Orlando Piñero's avatar
Orlando Piñero committed
    return fetch(URL + `/degreeCourses/${degreeCourseID}`, requestOptions)
        .then(response => {
            return response.json().then(body => {
                if (response.status === 200) {
                    return body
                }
            })
        })
}

export function editDegreeCourseWithID(updateData, degreeCourseID, token) {
    return dispatch => {
        dispatch(getEditDegreeCourseSuccessAction());
        updateDegreeCourse(updateData, degreeCourseID, token)
            .then(degreeCourse => {
                const action = getEditDegreeCourseSuccessAction(degreeCourse)
                dispatch(action);
                getAllDegreeCourses(token)
                    .then(degreeCourses => {
                        const action = getAllDegreeCoursesAction(degreeCourses)
                        dispatch(action)
                    })
            },
                error => {
                    dispatch(getEditDegreeCourseErrorAction(error));
                }
            )
            .catch(error => {
                dispatch(getEditDegreeCourseErrorAction(error))
            })
    }
}




export function getDeleteDegreeCourseSuccessAction() {
    return {
        type: DELETE_DEGREECOURSE_SUCCESS
    }
}

export function getDeleteDegreeCourseErrorAction(error) {
    return {
        type: DELETE_DEGREECOURSE_ERROR,
        error: error
    }
}

export function deleteDegreeCourse(degreeCourseID, token) {
    const requestOptions = {
        method: 'DELETE',
        headers: {
            'Authorization': token
        }
    }

Orlando Piñero's avatar
Orlando Piñero committed
    return fetch(URL + '/degreeCourses/' + degreeCourseID, requestOptions)
        .then(response => {
            if (response.status === 204) {
                return response.status
            }
        })
}

export function deleteDegreeCourseWithID(degreeCourseID, token) {
    return dispatch => {
        dispatch(getDeleteDegreeCourseSuccessAction(degreeCourseID));
        deleteDegreeCourse(degreeCourseID, token)
            .then(deleteDegreeCourse => {
                const action = getDeleteDegreeCourseSuccessAction(deleteDegreeCourse)
                dispatch(action);
                getAllDegreeCourses(token)
                    .then(degreeCourses => {
                        const action = getAllDegreeCoursesAction(degreeCourses)
                        dispatch(action)
                    })
            },
                error => {
                    dispatch(getDeleteDegreeCourseErrorAction(error));
                }
            )
            .catch(error => {
                dispatch(getDeleteDegreeCourseErrorAction(error))
            })
    }
}