import React from "react";
import {withRouter} from "react-router";
import {connect} from 'react-redux';
import style from './cabinet.module.sass';
import Header from "../../components/header/header";
import {checkAuthUser} from "../../actions/actionCreator";
import {roles} from "../../constants/user";
import UserCabinet from "../../components/userAndAdminCabinet/userCabinet/userCabinet";
import AdminCabinet from "../../components/userAndAdminCabinet/adminCabinet/adminCabinet";

class Cabinet extends React.Component{

    componentDidMount() {
        window.localStorage.getItem("token") && this.props.checkAuthUser();
    }

    render() {
        return(
            <div className={style.mainContainer}>
                <div className={style.container}>
                    <Header/>
                </div>
                <div className={style.container}>
                    <div className={style.cabinetMainContainer}>
                        <h1>Личный кабинет</h1>

                        {
                            this.props.role == roles.USER ?
                                <UserCabinet/> :
                                <AdminCabinet/>
                        }
                    </div>
                </div>
            </div>
        );
    }
};

const mapStateToProps = state => {
    const {role} = state.signUpReducers;

    return {
        role
    }
};

const mapDispatchToProps = (dispatch) => ({
    checkAuthUser: () => dispatch(checkAuthUser())
});

export default withRouter(connect(mapStateToProps, mapDispatchToProps)(Cabinet));
