App.js 4.83 KB
Newer Older
dobli's avatar
dobli committed
1
2
3
4
5
6
7
8
9
import React, {Component} from 'react';
import {Helmet} from "react-helmet";
import TopAppBar, {TopAppBarFixedAdjust} from '@material/react-top-app-bar';
import Drawer, {DrawerAppContent, DrawerContent, DrawerHeader, DrawerTitle} from '@material/react-drawer';
import List, {ListItem, ListItemGraphic, ListItemText} from '@material/react-list';
import Select from '@material/react-select';
import MaterialIcon from '@material/react-material-icon';

import './App.scss';
dobli's avatar
dobli committed
10
11

class App extends Component {
dobli's avatar
dobli committed
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
125
126
127
128
129
130
131
132
133
134
135
    constructor(props) {
        super(props)
        this.state = {
            buildings: [],
            open: false,
            selectedBuilding: {},
            selectedIndex: 0
        }

    }

    componentDidMount = () => {
        fetch('pages.json')
            .then(r => r.json())
            .then(data =>{
                // map pages entries
                const buildings = data.map(entry =>
                    ({
                        label: entry.instance,
                        value: entry.instance.replace(/\s/g, "").toLowerCase(),
                        entries: entry.entries
                    })
                )

                console.log(buildings)

                this.setState ({
                    buildings: buildings,
                    open: false,
                    selectedBuilding: buildings[0],
                    selectedIndex: 0
                })

            })
    }

    logoImg = <img className="App-logo" src={process.env.PUBLIC_URL + '/logo.svg'} alt="Logo" />;

    // Drawer navigation
    navigateToIndex = (index) => {
        console.log(this.pgs)
        this.setState({open: false});
        this.setState({selectedIndex: index});
    }

    // Building navigation
    navigateBuilding = (selection) => {
        const v = selection.target.value;
        const b = this.state.buildings.find(e => e.value === v )

        // ensure selection fits index
        if(this.state.selectedIndex >= b.entries.length){
            this.setState({selectedIndex: 0});
        }
        this.setState({selectedBuilding: b}, () => this.navigateToIndex(this.state.selectedIndex) );
    }


    // Drawer List Elements
    entries = () => this.state.selectedBuilding.entries.map((entry, index) =>
        <ListItem key={index}>
            <ListItemGraphic graphic={<MaterialIcon icon={entry.icon}/>} />
            <ListItemText primaryText={entry.title} />
        </ListItem>
    )
    render() {
        const {buildings} = this.state
        return !buildings.length ? ("Loading"):(
            <div className="application">
                <Helmet>
                    <meta charSet="utf-8" />
                    <title>Building Manager</title>
                    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"  />
                </Helmet>

                <div className='drawer-container'>
            <Drawer modal open={this.state.open} onClose={() => this.setState({open: false})}>
                <DrawerHeader>
                    {this.logoImg}
                    <DrawerTitle tag='h2'>
                        <Select
                            label='Building'
                            className='instance-select'
                            options={this.state.buildings}
                            onChange={this.navigateBuilding}
                            value={this.state.selectedBuilding.value}
                        >
                            </Select>
                        </DrawerTitle>
                    </DrawerHeader>

                    <DrawerContent>
                        <List singleSelection
                            selectedIndex={this.state.selectedIndex}
                            handleSelect={this.navigateToIndex}>
                            {this.entries()}
                        </List>
                    </DrawerContent>
                </Drawer>

                <DrawerAppContent className='drawer-app-content'>
                    <TopAppBar
                        title={this.state.selectedBuilding.entries[this.state.selectedIndex].title}
                        navigationIcon={<MaterialIcon
                            icon='menu'
                            onClick={() => this.setState({open: !this.state.open})}/>}
                        />

                            <TopAppBarFixedAdjust>
                                <iframe
                                    title="External App"
                                    src={this.state.selectedBuilding.entries[this.state.selectedIndex].url}
                                    style={{ width: 100+'%', height: 100+'%' }}>
                                </iframe>
                            </TopAppBarFixedAdjust>
                        </DrawerAppContent>
                    </div>
                </div>

                );
                }
                }

                export default App;