-
-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dialog to notify user of new version
- Loading branch information
1 parent
90d6bcf
commit 8c3380e
Showing
7 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import { useLocation } from '@reach/router'; | ||
import { FormattedMessage } from 'react-intl'; | ||
|
||
import messages from './messages'; | ||
import { Button } from './button'; | ||
|
||
const updateServiceWorker = (registration) => { | ||
if (registration && registration.waiting) { | ||
let preventReloadLoop; | ||
navigator.serviceWorker.addEventListener('controllerchange', (event) => { | ||
if (preventReloadLoop) { | ||
return; | ||
} | ||
preventReloadLoop = true; | ||
window.location.reload(); | ||
}); | ||
|
||
registration.waiting.postMessage({ type: 'SKIP_WAITING' }); | ||
} | ||
}; | ||
|
||
export const UpdateTM = () => { | ||
const location = useLocation(); | ||
const [registration, setRegistration] = useState(null); | ||
const [close, setClose] = useState(false); | ||
|
||
const isMapOrValidationPage = | ||
location.pathname.startsWith('/projects/') && | ||
(location.pathname.endsWith('/map/') || location.pathname.endsWith('/validate/')); | ||
|
||
useEffect(() => { | ||
const handleServiceWorkerEvent = (e) => setRegistration(e.detail.registration); | ||
document.addEventListener('onNewServiceWorker', handleServiceWorkerEvent); | ||
return () => document.removeEventListener('onNewServiceWorker', handleServiceWorkerEvent); | ||
}, []); | ||
return ( | ||
<> | ||
{!isMapOrValidationPage && !close && registration !== null && ( | ||
<div className="fixed left-1 bottom-1 shadow-2 ph3 pt2 pb3 br1 bg-white z-5 blue-dark fw6 ba b--grey-light"> | ||
<p className="mb3 mt2"> | ||
<FormattedMessage {...messages.newVersionAvailable} /> | ||
</p> | ||
<Button className="bg-red white" onClick={() => updateServiceWorker(registration)}> | ||
<FormattedMessage {...messages.update} /> | ||
</Button> | ||
<Button className="bg-white blue-dark" onClick={() => setClose(true)}> | ||
<FormattedMessage {...messages.remindMeLater} /> | ||
</Button> | ||
</div> | ||
)} | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters