Skip to content
This repository was archived by the owner on May 26, 2022. It is now read-only.

Commit 296d765

Browse files
author
Adam
committed
add notification on counted down
closes #10
1 parent dd79c40 commit 296d765

4 files changed

Lines changed: 44 additions & 1 deletion

File tree

src/assets/timer.svg

Lines changed: 4 additions & 0 deletions
Loading

src/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ import 'normalize.css'
1616
import './index.scss'
1717

1818
import { store, epicMiddleware } from '@state/index'
19+
import { requestPermission } from './utils/notifications'
1920
import { App } from './App'
2021
// import { register as registerSW } from './registerServiceWorker'
2122

23+
requestPermission()
24+
2225
render(
2326
<Provider store={store}>
2427
<DnDCProvider backend={HTML5Backend}>

src/state/epics/timerEpics.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
countDownFinished,
1414
} from '@state/actions/timerActions'
1515
import { setActiveNext } from '@state/actions/usersActions'
16+
import { notify } from '../../utils/notifications'
17+
import timer from '../../assets/timer.svg'
1618

1719
type startTimerEpicType = ActionsObservable<Actions[typeof START_TIMER]>
1820
type countDownFinishedEpicType = ActionsObservable<
@@ -51,7 +53,15 @@ export const alertEpic = (
5153
.ofType(COUNT_DOWN_FINISHED)
5254
.do(() => {
5355
const state = store.getState().users
54-
alert(`Time's up!\nUp next is ${state.list[state.activeUser]}!`)
56+
57+
notify("Time's up!", {
58+
body: 'Adam is up next!',
59+
badge: timer,
60+
icon: timer,
61+
vibrate: [2000, 2000, 2000],
62+
})
63+
64+
alert(`Time's up!\n${state.list[state.activeUser]} is up next!`)
5565
})
5666
.ignoreElements()
5767

src/utils/notifications.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Definition according to https://developer.mozilla.org/en-US/docs/Web/API/notification
2+
interface INotificationOptions {
3+
body?: string
4+
data?: any
5+
dir?: NotificationDirection
6+
icon?: string
7+
badge?: string
8+
image?: string
9+
lang?: string
10+
tag?: string
11+
vibrate?: number[]
12+
}
13+
14+
export function requestPermission() {
15+
Notification.requestPermission()
16+
}
17+
18+
export function notify(title: string, options?: INotificationOptions) {
19+
if (!('Notification' in window)) return
20+
21+
if ((Notification as any).permission !== 'granted') return
22+
23+
const notification = new Notification(title, options)
24+
25+
setTimeout(notification.close, 15 * 1000)
26+
}

0 commit comments

Comments
 (0)