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

Commit d87dae4

Browse files
committed
notification fixes
- goes away when starting timer - goes away after 120 seconds closes #12
1 parent 875f3a0 commit d87dae4

File tree

4 files changed

+48
-15
lines changed

4 files changed

+48
-15
lines changed

src/state/actions/timerActions.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const START_TIMER = 'START_TIMER'
66
export const STOP_TIMER = 'STOP_TIMER'
77
export const COUNT_DOWN_ONE_SECOND = 'COUNT_DOWN_ONE_SECOND'
88
export const COUNT_DOWN_FINISHED = 'COUNT_DOWN_FINISHED'
9+
export const ADD_NOTIFICATION = 'ADD_NOTIFICATION'
910

1011
export type Actions = {
1112
readonly SET_TIME: {
@@ -24,6 +25,10 @@ export type Actions = {
2425
readonly COUNT_DOWN_FINISHED: {
2526
type: typeof COUNT_DOWN_FINISHED
2627
}
28+
readonly ADD_NOTIFICATION: {
29+
type: typeof ADD_NOTIFICATION
30+
payload: Notification
31+
}
2732
}
2833

2934
export type RootAction = Actions[keyof Actions]
@@ -48,3 +53,10 @@ export const countDownOneSecond = (): Actions[typeof COUNT_DOWN_ONE_SECOND] => (
4853
export const countDownFinished = (): Actions[typeof COUNT_DOWN_FINISHED] => ({
4954
type: COUNT_DOWN_FINISHED,
5055
})
56+
57+
export const addNotification = (
58+
notification: Notification
59+
): Actions[typeof ADD_NOTIFICATION] => ({
60+
type: ADD_NOTIFICATION,
61+
payload: notification,
62+
})

src/state/epics/timerEpics.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
stopTimer,
1212
countDownOneSecond,
1313
countDownFinished,
14+
addNotification,
1415
} from '@state/actions/timerActions'
1516
import { setActiveNext } from '@state/actions/usersActions'
1617
import { notify } from '../../utils/notifications'
@@ -51,19 +52,19 @@ export const alertEpic = (
5152
) =>
5253
action$
5354
.ofType(COUNT_DOWN_FINISHED)
54-
.do(() => {
55+
.flatMap((): any => {
5556
const state = store.getState().users
5657

57-
notify("Time's up!", {
58-
body: `${state.list[state.activeUser]} 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!`)
58+
return [
59+
notify("Time's up!", {
60+
body: `${state.list[state.activeUser]} is up next!`,
61+
badge: timer,
62+
icon: timer,
63+
vibrate: [2000, 2000, 2000],
64+
}),
65+
]
6566
})
66-
.ignoreElements()
67+
.mergeMap(n => [addNotification(n as any)])
6768

6869
export const timerEpics = combineEpics(
6970
startTimerEpic,

src/state/reducers/timerReducers.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,22 @@ import {
55
COUNT_DOWN_ONE_SECOND,
66
START_TIMER,
77
STOP_TIMER,
8+
ADD_NOTIFICATION,
89
} from '@state/actions/timerActions'
910
import { Time } from '../../time'
1011

1112
export type IStateTimer = {
1213
readonly counting: boolean
1314
readonly timeLeft: Time
1415
readonly duration: Time
16+
readonly notifications: ReadonlyArray<Notification>
1517
}
1618

1719
const initialState: IStateTimer = {
1820
counting: false,
1921
timeLeft: new Time(),
2022
duration: new Time(),
23+
notifications: [],
2124
}
2225

2326
const cachedSettings = JSON.parse(
@@ -37,7 +40,7 @@ if (cachedSettings) {
3740
}
3841

3942
export const timerReducers = (
40-
state = Object.assign({}, initialState, cachedSettings),
43+
state: IStateTimer = Object.assign({}, initialState, cachedSettings),
4144
action: RootAction
4245
) => {
4346
switch (action.type) {
@@ -50,10 +53,13 @@ export const timerReducers = (
5053
})
5154

5255
case START_TIMER:
56+
state.notifications.forEach(notification => notification.close())
57+
5358
return Object.assign({}, state, {
5459
timeLeft:
5560
state.timeLeft.toSeconds() !== 0 ? state.timeLeft : state.duration,
5661
counting: true,
62+
notifications: [],
5763
})
5864

5965
case STOP_TIMER:
@@ -66,6 +72,11 @@ export const timerReducers = (
6672
timeLeft: state.timeLeft.reduceByOneSecond(),
6773
})
6874

75+
case ADD_NOTIFICATION:
76+
return Object.assign({}, state, {
77+
notifications: [action.payload, ...state.notifications],
78+
})
79+
6980
default:
7081
return state
7182
}

src/utils/notifications.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ interface INotificationOptions {
99
lang?: string
1010
tag?: string
1111
vibrate?: number[]
12+
onClose?: (e: Event) => any
1213
}
1314

1415
export function requestPermission() {
@@ -18,11 +19,19 @@ export function requestPermission() {
1819
}
1920

2021
export function notify(title: string, options?: INotificationOptions) {
21-
if (!('Notification' in window)) return
22+
const notification = new Notification(title, options)
2223

23-
if ((Notification as any).permission !== 'granted') return
24+
notification.onclick = e => {
25+
notification.close()
26+
}
2427

25-
const notification = new Notification(title, options)
28+
notification.onclose = e => {
29+
if (!options || typeof options.onClose !== 'function') return
30+
31+
options.onClose(e)
32+
}
33+
34+
setTimeout(() => notification.close(), 120 * 1000)
2635

27-
setTimeout(notification.close, 15 * 1000)
36+
return notification
2837
}

0 commit comments

Comments
 (0)