-
Notifications
You must be signed in to change notification settings - Fork 1
/
settings.ts
47 lines (35 loc) · 1.33 KB
/
settings.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { Store } from "redux";
import actionCreatorFactory from "typescript-fsa";
import { reducerWithInitialState } from "typescript-fsa-reducers";
import * as notification from "../drivers/notification";
const actionCreator = actionCreatorFactory("SETTINGS");
const setAlarmVolume = actionCreator<number>("SET_ALARM_VOLUME");
const setNotificationState = actionCreator<boolean | null>(
"SET_NOTIFICATION_STATE"
);
export const actionCreators = {
setAlarmVolume,
setNotificationState
};
export type IActionCreators = typeof actionCreators;
export const initialState = {
alarmVolume: 0.5, // 0 to 1
notificationOn: notification.permission()
};
export type IState = typeof initialState;
export const reducer = reducerWithInitialState(initialState)
.case(setAlarmVolume, (state, alarmVolume) => ({ ...state, alarmVolume }))
.case(setNotificationState, (state, notificationOn) => ({
...state,
notificationOn
}));
export async function initializeStore(store: Store): Promise<void> {
const setPermission = (permission: boolean | null) =>
store.dispatch(setNotificationState(permission));
if (notification.permission() === null) {
setPermission(await notification.requestPermission());
}
setPermission(notification.permission());
notification.onPermissionChange(setPermission);
}
export const persistent = true;