-
Notifications
You must be signed in to change notification settings - Fork 0
/
UpdateHandler.js
53 lines (49 loc) · 1.91 KB
/
UpdateHandler.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
import React, { Component, Fragment } from 'react';
import { Snackbar, Button } from '@material-ui/core'
class UpdateHandler extends Component {
constructor(props) {
super(props);
this.state = {
showInstalledMessage: false,
showUpdateMessage: false,
showUpdateOnRestartMessage: false,
}
}
componentDidMount() {
const { appServiceWorker } = this.props
if (appServiceWorker) {
appServiceWorker.onInstalled(() => this.setState({ showInstalledMessage: true }))
appServiceWorker.onUpdateFound(() => this.setState({ showUpdateMessage: true }))
}
}
render() {
return (
<Fragment>
{this.props.children}
<Snackbar
open={this.state.showInstalledMessage}
autoHideDuration={6000}
onClose={() => this.setState({ showInstalledMessage: false })}
message="The APP is ready to work offline."
/>
<Snackbar
open={this.state.showUpdateMessage}
onClose={() => this.setState({ showUpdateMessage: false, showUpdateOnRestartMessage: true })}
message="A new version is available."
action={[
<Button key="undo" color="secondary" size="small" onClick={() => window.location.reload()}>
UPDATE
</Button>
]}
/>
<Snackbar
open={this.state.showUpdateOnRestartMessage}
autoHideDuration={6000}
onClose={() => this.setState({ showUpdateOnRestartMessage: false })}
message="The APP will be updated on the next restart."
/>
</Fragment>
);
}
}
export default UpdateHandler;