-
Notifications
You must be signed in to change notification settings - Fork 5
/
App.js
executable file
·115 lines (103 loc) · 3.01 KB
/
App.js
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
import PropTypes from 'prop-types';
import React, { useState, useEffect } from 'react';
import 'bootstrap-css-only/css/bootstrap.css';
import Container from 'react-bootstrap/Container';
import Routes from './Routes';
import SignOutButton from './Components/SignOut';
import { withAuthentication } from './Components/Session';
import Row from 'react-bootstrap/Row';
import Col from 'react-bootstrap/Col';
import Alert from 'react-bootstrap/Alert';
import HelpOverlayTrigger from './HelpOverlayTrigger';
import Collection from './Components/Firebase/Collection';
const App = (props) => {
let offlineWarning = null;
const [ authUser, setAuthUser ] = useState();
const [ user, setUser ] = useState();
useEffect(() => {
const authListener = props.firebase.auth.onAuthStateChanged((authStateUser) =>
setAuthUser(authStateUser)
);
return () => {
authListener();
};
}, [ props.firebase.auth ]);
useEffect(() => {
const userCollection = new Collection(props.firebase, '/users');
const getUser = async () => {
const userItem = await userCollection.getItem(authUser.uid);
setUser(userItem);
};
if (authUser) {
getUser();
}
return () => {
};
}, [ props.firebase, authUser ]);
if (!navigator.onLine) {
offlineWarning = (
<>
<br />
<Container>
<Alert variant={ 'warning' }>
<Alert.heading>Offline warning</Alert.heading>
You don`'`t seem to be connected to the internet
</Alert>
</Container>
</>
);
}
let AppContainer;
if (authUser) {
AppContainer = (
<>
{offlineWarning}
<Container style={ { marginBottom: '1em', marginTop: '1em' } }>
<Row>
<Col xs={ 5 }>
<h1> Digital Paper Edit </h1>
</Col>
<Col md={ { offset: 1, span: 3 } }>
Signed in as: <br></br>
<strong>
{user && user.role === 'ADMIN' ? <a href="#admin">{authUser.email}</a> : authUser.email}
</strong>
</Col>
<Col md={ { span: 3 } }>
<SignOutButton /> <HelpOverlayTrigger />
</Col>
</Row>
</Container>
<Routes authUser={ authUser } />
</>
);
} else {
AppContainer = (
<>
<Container style={ { marginBottom: '2em', marginTop: '1em' } }>
<Row>
<Col xs={ 5 }>
<h1> Digital Paper Edit </h1>
</Col>
<Col md={ { offset: 1, span: 3 } }>
Please <a href="/">sign in</a> or request login details by clicking the help button!
</Col>
<Col md={ { span: 3 } }>
<HelpOverlayTrigger />
</Col>
</Row>
</Container>
<Routes />
</>
);
}
return <>{AppContainer}</>;
};
App.propTypes = {
firebase: PropTypes.shape({
auth: PropTypes.shape({
onAuthStateChanged: PropTypes.func,
}),
}),
};
export default withAuthentication(App);