Skip to content
Snippets Groups Projects
TopMenu.js 5.19 KiB
Newer Older
Orlando Piñero's avatar
Orlando Piñero committed
import React, { Component } from "react";
import Nav from 'react-bootstrap/Nav';
import Navbar from 'react-bootstrap/Navbar';
import UserSessionWidget from "./UserComponents/userSessionWidget";
Orlando Piñero's avatar
Orlando Piñero committed
import Container from 'react-bootstrap/Container';
Orlando Piñero's avatar
Orlando Piñero committed
import { LinkContainer } from 'react-router-bootstrap';
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
import * as authenticationActions from '../actions/AuthenticationActions'
import * as userManagementActions from '../actions/UserManagementActions';
import * as degreeCourseManagementActions from '../actions/DegreeCourseManagementActions';
Orlando Piñero's avatar
Orlando Piñero committed
import * as applicationManagementActions from '../actions/ApplicationManagementActions';
Orlando Piñero's avatar
Orlando Piñero committed

const mapStateToProps = state => {
    return {
        accessToken: state.AuthenticationReducer.accessToken,
Orlando Piñero's avatar
Orlando Piñero committed
        isAdmin: state.AuthenticationReducer.isAdmin,
        user: state.AuthenticationReducer.user
Orlando Piñero's avatar
Orlando Piñero committed
}

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

class TopMenu extends Component {
Orlando Piñero's avatar
Orlando Piñero committed

    constructor(props) {
        super(props)

        this.handleUserManagementButtonAction = this.handleUserManagementButtonAction.bind(this);
Orlando Piñero's avatar
Orlando Piñero committed
        this.handleDegreeCourseManagementButtonAction = this.handleDegreeCourseManagementButtonAction.bind(this);
        this.handleApplicationManagementButtonAction = this.handleApplicationManagementButtonAction.bind(this);
Orlando Piñero's avatar
Orlando Piñero committed
    };

Orlando Piñero's avatar
Orlando Piñero committed
    async handleApplicationManagementButtonAction() {
        if (this.props.isAdmin) {
            const { showAllApplications } = this.props
            await showAllApplications(this.props.accessToken);
        } else {
            const { showMyApplications } = this.props
            await showMyApplications(this.props.accessToken);
        }
    }

Orlando Piñero's avatar
Orlando Piñero committed
    async handleUserManagementButtonAction() {
        const { showAllUsers } = this.props;
        await showAllUsers(this.props.accessToken)
    }

    async handleDegreeCourseManagementButtonAction() {
        const { showAllDegreeCourses } = this.props;
        await showAllDegreeCourses(this.props.accessToken)
    }

Orlando Piñero's avatar
Orlando Piñero committed
    render() {
Orlando Piñero's avatar
Orlando Piñero committed
        let OpenManagementPageButtons;
Orlando Piñero's avatar
Orlando Piñero committed
        if (this.props.user && this.props.isAdmin) {
Orlando Piñero's avatar
Orlando Piñero committed
            OpenManagementPageButtons =
                <>
                    <LinkContainer to="/userManagement" id="OpenUserManagementPageButton" onClick={this.handleUserManagementButtonAction}>
                        <Nav.Link>User-Management</Nav.Link>
                    </LinkContainer>
                    <LinkContainer to="/degreeCourseManagement" id="OpenDegreeCourseManagementPageButton" onClick={this.handleDegreeCourseManagementButtonAction}>
                        <Nav.Link>DegreeCourse-Management</Nav.Link>
                    </LinkContainer>
Orlando Piñero's avatar
Orlando Piñero committed
                    <LinkContainer to="/degreeCourseApplicationManagement" id="OpenDegreeCourseApplicationManagementPageButton" onClick={this.handleApplicationManagementButtonAction}>
                        <Nav.Link>Studienbewerbung-Management</Nav.Link>
                    </LinkContainer>
                </>
        } else if (this.props.user) {
            OpenManagementPageButtons =
                <>
                    <LinkContainer to="/degreeCourseApplicationManagement" id="OpenDegreeCourseApplicationManagementPageButton" onClick={this.handleApplicationManagementButtonAction}>
                        <Nav.Link>Studienbewerbung-Management</Nav.Link>
                    </LinkContainer>
                    <LinkContainer to="/degreeCourseManagement" id="OpenDegreeCourseManagementPageButton" onClick={this.handleDegreeCourseManagementButtonAction}>
                        <Nav.Link>DegreeCourse-List</Nav.Link>
                    </LinkContainer>
Orlando Piñero's avatar
Orlando Piñero committed
        }

Orlando Piñero's avatar
Orlando Piñero committed
        return (
            <div>
                <Navbar bg="light" expand="lg">
Orlando Piñero's avatar
Orlando Piñero committed
                    <Container style={{ margin: 0 }}>
                        <LinkContainer to="/" id="OpenStartPageButton">
                            <Navbar.Brand>BHT</Navbar.Brand>
                        </LinkContainer>
Orlando Piñero's avatar
Orlando Piñero committed
                        <Navbar.Toggle aria-controls="basic-navbar-nav" />
Orlando Piñero's avatar
Orlando Piñero committed
                        <Navbar.Collapse id="basic-navbar-nav" style={{ display: 'flex', flexWrap: 'row', justifyContent: 'space-between', width: '100%' }}>
Orlando Piñero's avatar
Orlando Piñero committed
                            <Nav className="mr-auto">
Orlando Piñero's avatar
Orlando Piñero committed
                                <LinkContainer to="/" id="OpenStartPageButton">
                                    <Nav.Link>Home</Nav.Link>
                                </LinkContainer>
Orlando Piñero's avatar
Orlando Piñero committed
                                {OpenManagementPageButtons}
Orlando Piñero's avatar
Orlando Piñero committed
                            </Nav>
                            <UserSessionWidget />
                        </Navbar.Collapse>
                    </Container>
Orlando Piñero's avatar
Orlando Piñero committed
                </Navbar>
            </div>
        )
    }
}

Orlando Piñero's avatar
Orlando Piñero committed
const mapDispatchToProps = dispatch => bindActionCreators({
    authenticateUserAction: authenticationActions.authenticateUser,
    showAllUsers: userManagementActions.displayUsers,
Orlando Piñero's avatar
Orlando Piñero committed
    showAllDegreeCourses: degreeCourseManagementActions.displayDegreeCourses,
    showAllApplications: applicationManagementActions.displayAllApplications,
    showMyApplications: applicationManagementActions.displayMyApplications
Orlando Piñero's avatar
Orlando Piñero committed
}, dispatch)

const ConnectedTopMenu = connect(mapStateToProps, mapDispatchToProps)(TopMenu);
export default ConnectedTopMenu;