Migrate browser notifications to new structure

This commit is contained in:
Jannis Mattheis 2018-10-21 16:18:40 +02:00
parent 6e629a9fc4
commit 7370539fb4
3 changed files with 19 additions and 25 deletions

View File

@ -6,7 +6,7 @@ import {initAxios} from './actions/axios';
import * as config from './config'; import * as config from './config';
import Layout from './layout/Layout'; import Layout from './layout/Layout';
import registerServiceWorker from './registerServiceWorker'; import registerServiceWorker from './registerServiceWorker';
import * as Notifications from './stores/Notifications'; import * as Notifications from './snack/browserNotification';
import {CurrentUser} from './CurrentUser'; import {CurrentUser} from './CurrentUser';
import {AppStore} from './application/AppStore'; import {AppStore} from './application/AppStore';
import {reaction} from 'mobx'; import {reaction} from 'mobx';
@ -44,7 +44,7 @@ const initStores = (): StoreMapping => {
const messagesStore = new MessagesStore(appStore, snackManager.snack); const messagesStore = new MessagesStore(appStore, snackManager.snack);
const currentUser = new CurrentUser(snackManager.snack); const currentUser = new CurrentUser(snackManager.snack);
const clientStore = new ClientStore(snackManager.snack); const clientStore = new ClientStore(snackManager.snack);
const wsStore = new WebSocketStore(snackManager.snack, currentUser, messagesStore); const wsStore = new WebSocketStore(snackManager.snack, currentUser);
return { return {
appStore, appStore,
@ -71,7 +71,10 @@ const initStores = (): StoreMapping => {
() => stores.currentUser.loggedIn, () => stores.currentUser.loggedIn,
(loggedIn) => { (loggedIn) => {
if (loggedIn) { if (loggedIn) {
stores.wsStore.listen(); stores.wsStore.listen((message) => {
stores.messagesStore.publishSingleMessage(message);
Notifications.notifyNewMessage(message);
});
} else { } else {
stores.wsStore.close(); stores.wsStore.close();
} }

View File

@ -1,7 +1,6 @@
import {SnackReporter} from '../snack/SnackManager'; import {SnackReporter} from '../snack/SnackManager';
import {CurrentUser} from '../CurrentUser'; import {CurrentUser} from '../CurrentUser';
import * as config from '../config'; import * as config from '../config';
import {MessagesStore} from './MessagesStore';
export class WebSocketStore { export class WebSocketStore {
private wsActive = false; private wsActive = false;
@ -9,11 +8,10 @@ export class WebSocketStore {
public constructor( public constructor(
private readonly snack: SnackReporter, private readonly snack: SnackReporter,
private readonly currentUser: CurrentUser, private readonly currentUser: CurrentUser
private readonly messagesStore: MessagesStore
) {} ) {}
public listen = () => { public listen = (callback: (msg: IMessage) => void) => {
if (!this.currentUser.token() || this.wsActive) { if (!this.currentUser.token() || this.wsActive) {
return; return;
} }
@ -30,7 +28,7 @@ export class WebSocketStore {
console.log('WebSocket connection errored', e); console.log('WebSocket connection errored', e);
}; };
ws.onmessage = (data) => this.messagesStore.publishSingleMessage(JSON.parse(data.data)); ws.onmessage = (data) => callback(JSON.parse(data.data));
ws.onclose = () => { ws.onclose = () => {
this.wsActive = false; this.wsActive = false;

View File

@ -1,5 +1,4 @@
import Notify from 'notifyjs'; import Notify from 'notifyjs';
import dispatcher, {IEvent} from './dispatcher';
export function requestPermission() { export function requestPermission() {
if (Notify.needsPermission && Notify.isSupported()) { if (Notify.needsPermission && Notify.isSupported()) {
@ -10,6 +9,16 @@ export function requestPermission() {
} }
} }
export function notifyNewMessage(msg: IMessage) {
const notify = new Notify(msg.title, {
body: msg.message,
icon: msg.image,
notifyClick: closeAndFocus,
notifyShow: closeAfterTimeout,
});
notify.show();
}
function closeAndFocus(event: Event) { function closeAndFocus(event: Event) {
if (window.parent) { if (window.parent) {
window.parent.focus(); window.parent.focus();
@ -26,19 +35,3 @@ function closeAfterTimeout(event: Event) {
target.close(); target.close();
}, 5000); }, 5000);
} }
dispatcher.register(
(data: IEvent): void => {
if (data.type === 'ONE_MESSAGE') {
const msg = data.payload;
const notify = new Notify(msg.title, {
body: msg.message,
icon: msg.image,
notifyClick: closeAndFocus,
notifyShow: closeAfterTimeout,
});
notify.show();
}
}
);