Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 4c652a7

Browse files
committed
Watch notification toggles
1 parent 1939c17 commit 4c652a7

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

src/components/views/settings/Notifications.tsx

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,36 @@ interface IState {
105105
};
106106
pushers?: IPusher[];
107107
threepids?: IThreepid[];
108+
109+
desktopNotifications: boolean;
110+
desktopShowBody: boolean;
111+
audioNotifications: boolean;
108112
}
109113

110114
export default class Notifications extends React.PureComponent<IProps, IState> {
115+
private settingWatchers: string[];
116+
111117
public constructor(props: IProps) {
112118
super(props);
113119

114120
this.state = {
115121
phase: Phase.Loading,
122+
desktopNotifications: SettingsStore.getValue("notificationsEnabled"),
123+
desktopShowBody: SettingsStore.getValue("notificationBodyEnabled"),
124+
audioNotifications: SettingsStore.getValue("audioNotificationsEnabled"),
116125
};
126+
127+
this.settingWatchers = [
128+
SettingsStore.watchSetting("notificationsEnabled", null, (...[,,,, value]) =>
129+
this.setState({ desktopNotifications: value as boolean }),
130+
),
131+
SettingsStore.watchSetting("notificationBodyEnabled", null, (...[,,,, value]) =>
132+
this.setState({ desktopShowBody: value as boolean }),
133+
),
134+
SettingsStore.watchSetting("audioNotificationsEnabled", null, (...[,,,, value]) =>
135+
this.setState({ audioNotifications: value as boolean }),
136+
),
137+
];
117138
}
118139

119140
private get isInhibited(): boolean {
@@ -129,6 +150,10 @@ export default class Notifications extends React.PureComponent<IProps, IState> {
129150
this.refreshFromServer();
130151
}
131152

153+
public componentWillUnmount() {
154+
this.settingWatchers.forEach(watcher => SettingsStore.unwatchSetting(watcher));
155+
}
156+
132157
private async refreshFromServer() {
133158
try {
134159
const newState = (await Promise.all([
@@ -137,7 +162,7 @@ export default class Notifications extends React.PureComponent<IProps, IState> {
137162
this.refreshThreepids(),
138163
])).reduce((p, c) => Object.assign(c, p), {});
139164

140-
this.setState({
165+
this.setState<keyof Omit<IState, "desktopNotifications" | "desktopShowBody" | "audioNotifications">>({
141166
...newState,
142167
phase: Phase.Ready,
143168
});
@@ -308,17 +333,14 @@ export default class Notifications extends React.PureComponent<IProps, IState> {
308333

309334
private onDesktopNotificationsChanged = async (checked: boolean) => {
310335
await SettingsStore.setValue("notificationsEnabled", null, SettingLevel.DEVICE, checked);
311-
this.forceUpdate(); // the toggle is controlled by SettingsStore#getValue()
312336
};
313337

314338
private onDesktopShowBodyChanged = async (checked: boolean) => {
315339
await SettingsStore.setValue("notificationBodyEnabled", null, SettingLevel.DEVICE, checked);
316-
this.forceUpdate(); // the toggle is controlled by SettingsStore#getValue()
317340
};
318341

319342
private onAudioNotificationsChanged = async (checked: boolean) => {
320343
await SettingsStore.setValue("audioNotificationsEnabled", null, SettingLevel.DEVICE, checked);
321-
this.forceUpdate(); // the toggle is controlled by SettingsStore#getValue()
322344
};
323345

324346
private onRadioChecked = async (rule: IVectorPushRule, checkedState: VectorState) => {
@@ -499,23 +521,23 @@ export default class Notifications extends React.PureComponent<IProps, IState> {
499521

500522
<LabelledToggleSwitch
501523
data-test-id='notif-setting-notificationsEnabled'
502-
value={SettingsStore.getValue("notificationsEnabled")}
524+
value={this.state.desktopNotifications}
503525
onChange={this.onDesktopNotificationsChanged}
504526
label={_t('Enable desktop notifications for this session')}
505527
disabled={this.state.phase === Phase.Persisting}
506528
/>
507529

508530
<LabelledToggleSwitch
509531
data-test-id='notif-setting-notificationBodyEnabled'
510-
value={SettingsStore.getValue("notificationBodyEnabled")}
532+
value={this.state.desktopShowBody}
511533
onChange={this.onDesktopShowBodyChanged}
512534
label={_t('Show message in desktop notification')}
513535
disabled={this.state.phase === Phase.Persisting}
514536
/>
515537

516538
<LabelledToggleSwitch
517539
data-test-id='notif-setting-audioNotificationsEnabled'
518-
value={SettingsStore.getValue("audioNotificationsEnabled")}
540+
value={this.state.audioNotifications}
519541
onChange={this.onAudioNotificationsChanged}
520542
label={_t('Enable audible notifications for this session')}
521543
disabled={this.state.phase === Phase.Persisting}

0 commit comments

Comments
 (0)