Skip to content
Snippets Groups Projects
App.js 2.31 KiB
Newer Older
  • Learn to ignore specific revisions
  • import React from 'react';
    
    meskio's avatar
    meskio committed
    import { BrowserRouter, Switch, Route } from 'react-router-dom';
    
    meskio's avatar
    meskio committed
    import MemberList from './member';
    
    meskio's avatar
    meskio committed
    import ProductList from './ProductList';
    
    meskio's avatar
    meskio committed
    import Dashboard from './Dashboard';
    
    meskio's avatar
    meskio committed
    import AuthContext from './AuthContext';
    import SignIn from './SignIn';
    import Head from './Head';
    
    meskio's avatar
    meskio committed
    function Panel(props) {
        return (
            <div>
                <Head onLogout={props.onLogout} />
    
    meskio's avatar
    meskio committed
                <BrowserRouter>
    
    meskio's avatar
    meskio committed
                  <Switch>
    
    meskio's avatar
    meskio committed
                    <Route path="/members">
                        <MemberList />
                    </Route>
    
    meskio's avatar
    meskio committed
                    <Route path="/products">
                        <ProductList />
                    </Route>
    
    meskio's avatar
    meskio committed
                    <Route path="/">
                        <Dashboard />
                    </Route>
    
    meskio's avatar
    meskio committed
                  </Switch>
    
    meskio's avatar
    meskio committed
                </BrowserRouter>
            </div>
        );
    
    meskio's avatar
    meskio committed
    }
    
    class App extends React.Component {
        constructor(props) {
            super(props);
            this.state = {
                isLoged: false,
                num: null,
                role: null,
                token: null
            };
    
            this.logout = this.logout.bind(this);
            this.login = this.login.bind(this);
        }
    
    
        componentDidMount() {
            const token = localStorage.getItem("token");
            if (token !== "") {
                const num = localStorage.getItem("num");
                const role = localStorage.getItem("role");
                this.setState({ isLoged: true, num, role, token });
            }
        }
    
        storeState(token, num, role) {
            localStorage.setItem("token", token);
            localStorage.setItem("num", num);
            localStorage.setItem("role", role);
        }
    
    
    meskio's avatar
    meskio committed
        login(token, member) {
            this.setState({
                isLoged: true,
                num: member.num,
                role: member.role,
                token: token
            });
    
            this.storeState(token, member.num, member.role);
    
    meskio's avatar
    meskio committed
        }
    
        logout() {
            this.setState({
                isLoged: false,
                num: null,
                role: null,
                token: null
            });
    
            this.storeState("", "", "");
    
    meskio's avatar
    meskio committed
        }
    
        render() {
            const component = this.state.isLoged ?
                <Panel onLogout={this.logout} /> :
                <SignIn onLogin={this.login} />;
            return (
              <AuthContext.Provider value={this.state}>
                {component}
              </AuthContext.Provider>
            );
        }
    
    }
    
    export default App;