aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/odlux/framework/src/components/titleBar.tsx
blob: ed6eb2ccc844e40f531defd35164ef5ac62f04f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import * as React from 'react';
import { withRouter, RouteComponentProps } from 'react-router-dom';

import { withStyles, WithStyles, createStyles, Theme } from '@material-ui/core/styles';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import Typography from '@material-ui/core/Typography';
import Button from '@material-ui/core/Button';
import IconButton from '@material-ui/core/IconButton';
import MenuIcon from '@material-ui/icons/Menu';
import AccountCircle from '@material-ui/icons/AccountCircle';
import MenuItem from '@material-ui/core/MenuItem';
import Menu from '@material-ui/core/Menu';

import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';

import { UpdateAuthentication } from '../actions/authentication';

import connect, { Connect, IDispatcher } from '../flux/connect';
import Logo from './logo';

const styles = (theme: Theme) => createStyles({
  appBar: {
    zIndex: theme.zIndex.drawer + 1,
  },
  grow: {
    flexGrow: 1,
  },
  menuButton: {
    marginLeft: -12,
    marginRight: 20,
  },
  icon: {
    marginRight: 12
  }
});

const mapDispatch = (dispatcher: IDispatcher) => {
  return {
    logout: () => { dispatcher.dispatch(new UpdateAuthentication(null)); }
  }
};

type TitleBarProps = RouteComponentProps<{}> & WithStyles<typeof styles> & Connect<undefined, typeof mapDispatch>

class TitleBarComponent extends React.Component<TitleBarProps, { anchorEl: HTMLElement | null }> {

  constructor(props: TitleBarProps) {
    super(props);

    this.state = {
      anchorEl: null
    }

  }
  render(): JSX.Element {
    const { classes, state, history, location } = this.props;
    const open = !!this.state.anchorEl;

    return (
      <AppBar position="absolute" className={ classes.appBar }>
        <Toolbar>
          <IconButton className={ classes.menuButton } color="inherit" aria-label="Menu">
            <MenuIcon />
          </IconButton>
          <Logo />
          <Typography variant="title" color="inherit" className={ classes.grow }>
            { state.framework.applicationState.icon
              ? (<FontAwesomeIcon className={ classes.icon } icon={ state.framework.applicationState.icon } />)
              : null }
            { state.framework.applicationState.title }
          </Typography>
          { state.framework.authenticationState.user
            ? (<div>
              <Button
                aria-owns={ open ? 'menu-appbar' : undefined }
                aria-haspopup="true"
                onClick={ this.openMenu }
                color="inherit"
              >
                <AccountCircle />
                { state.framework.authenticationState.user.user }
              </Button>
              <Menu
                id="menu-appbar"
                anchorEl={ this.state.anchorEl }
                anchorOrigin={ {
                  vertical: 'top',
                  horizontal: 'right',
                } }
                transformOrigin={ {
                  vertical: 'top',
                  horizontal: 'right',
                } }
                open={ open }
                onClose={ this.closeMenu }
              >
                <MenuItem onClick={ this.closeMenu }>Profile</MenuItem>
                <MenuItem onClick={ () => {
                  this.props.logout();
                  this.closeMenu();
                } }>Logout</MenuItem>
              </Menu>
            </div>)
            : (<Button onClick={ () => { history.push('/login') } } color="inherit" disabled={ location.pathname == "/login" }>Login</Button>) }
        </Toolbar>
      </AppBar>
    );
  };


  private openMenu = (event: React.MouseEvent<HTMLElement>) => {
    this.setState({ anchorEl: event.currentTarget });
  };

  private closeMenu = () => {
    this.setState({ anchorEl: null });
  };
}

//todo: ggf. https://github.com/acdlite/recompose verwenden zur Vereinfachung

export const TitleBar = withStyles(styles)(withRouter(connect(undefined, mapDispatch)(TitleBarComponent)));
export default TitleBar;