Skip to content

Commit

Permalink
Show notification on received message
Browse files Browse the repository at this point in the history
  • Loading branch information
jmattheis committed Mar 20, 2018
1 parent 9189045 commit 584d28e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ui/src/index.js
Expand Up @@ -4,6 +4,7 @@ import Layout from './Layout';
import registerServiceWorker from './registerServiceWorker';
import {checkIfAlreadyLoggedIn} from './actions/defaultAxios';
import config from 'react-global-configuration';
import * as Notifications from './stores/Notifications';
import 'typeface-roboto';
import 'typeface-roboto-mono';

Expand All @@ -21,6 +22,8 @@ const defaultProdConfig = {
};

(function clientJS() {
Notifications.requestPermission();

if (process.env.NODE_ENV === 'production') {
config.set(window.config || defaultProdConfig);
} else {
Expand Down
38 changes: 38 additions & 0 deletions ui/src/stores/Notifications.js
@@ -0,0 +1,38 @@
import dispatcher from './dispatcher';
import Notify from 'notifyjs';

export function requestPermission() {
if (Notify.needsPermission && Notify.isSupported()) {
Notify.requestPermission(() => console.log('granted notification permissions'),
() => console.log('notification permission denied'));
}
}

function closeAndFocus(event) {
if (window.parent) {
window.parent.focus();
}
window.focus();
window.location.href = '/';
event.target.close();
}

function closeAfterTimeout(event) {
setTimeout(() => {
event.target.close();
}, 5000);
}

dispatcher.register((data) => {
if (data.type === 'ONE_MESSAGE') {
const msg = data.payload;

const notify = new Notify(msg.title, {
body: msg.message,
icon: '/static/favicon.ico',
notifyClick: closeAndFocus,
notifyShow: closeAfterTimeout,
});
notify.show();
}
});

0 comments on commit 584d28e

Please sign in to comment.