Skip to content

Commit 36ddea9

Browse files
committed
Refine notification interface
1 parent ff9c7dc commit 36ddea9

4 files changed

Lines changed: 122 additions & 43 deletions

File tree

src/config/basic_config_component.ts

Lines changed: 84 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Component } from "preact";
22

33
import { Config } from "./config";
4-
import { Notificator, NotificationType } from "../system/notificator";
4+
import { Notificator } from "../system/notificator";
5+
import { NotificationSeverity } from "../system/notification_types";
56

67
type configState = {
78
data: Record<string, any> | null;
@@ -52,58 +53,112 @@ export abstract class BasicConfigComponent extends Component<
5253

5354
// Note: use arrow function to properly capture `this`.
5455
protected writeConfig = async () => {
55-
if (
56-
await !this.props.notificator.confirm(
57-
"Are you sure you want to update configuration?",
58-
)
59-
) {
56+
const confirmResult = this.props.notificator.confirm(
57+
"Are you sure you want to update configuration?",
58+
NotificationSeverity.Wrn,
59+
);
60+
if (confirmResult.error) {
61+
const alertResult = this.props.notificator.alert(
62+
`Unable to open confirm dialog: ${confirmResult.error}`,
63+
NotificationSeverity.Err,
64+
);
65+
if (alertResult.error) {
66+
console.error(
67+
`basic-config-component: failed to send notification: ${alertResult.error}`,
68+
);
69+
}
70+
71+
return;
72+
}
73+
74+
const resultConfirmed = await confirmResult.promise;
75+
if (!resultConfirmed) {
6076
return;
6177
}
6278

6379
const err = await this.props.config.write(this.state.data!);
6480
if (err) {
6581
console.error(`basic-config-component: error updating config: ${err}`);
6682

67-
this.props.notificator.alert(
68-
"Error updating configuration",
69-
NotificationType.ERROR,
70-
);
71-
} else {
72-
this.props.notificator.alert(
73-
"Configuration updated successfully!",
74-
NotificationType.SUCCESS,
83+
const alertResult = this.props.notificator.alert(
84+
`Error updating configuration: ${err}`,
85+
NotificationSeverity.Err,
7586
);
87+
if (alertResult.error) {
88+
console.error(
89+
`basic-config-component: failed to send notification: ${alertResult.error}`,
90+
);
91+
}
7692

77-
await this.readConfig();
93+
return;
7894
}
95+
96+
const alertResult = this.props.notificator.alert(
97+
"Configuration updated successfully!",
98+
NotificationSeverity.Inf,
99+
);
100+
if (alertResult.error) {
101+
console.error(
102+
`basic-config-component: failed to send notification: ${alertResult.error}`,
103+
);
104+
}
105+
106+
await this.readConfig();
79107
};
80108

81109
// Note: use arrow function to properly capture `this`.
82110
protected resetConfig = async () => {
83-
if (
84-
await !this.props.notificator.confirm(
85-
"Are you sure you want to reset configuration to defaults?",
86-
)
87-
) {
111+
const confirmResult = this.props.notificator.confirm(
112+
"Are you sure you want to reset configuration?",
113+
NotificationSeverity.Wrn,
114+
);
115+
if (confirmResult.error) {
116+
const alertResult = this.props.notificator.alert(
117+
`Unable to open confirm dialog: ${confirmResult.error}`,
118+
NotificationSeverity.Err,
119+
);
120+
if (alertResult.error) {
121+
console.error(
122+
`basic-config-component: failed to send notification: ${alertResult.error}`,
123+
);
124+
}
125+
126+
return;
127+
}
128+
129+
const resultConfirmed = await confirmResult.promise;
130+
if (!resultConfirmed) {
88131
return;
89132
}
90133

91134
const err = await this.props.config.reset();
92135
if (err) {
93136
console.error("basic-config-component: error resetting config:", err);
94137

95-
this.props.notificator.alert(
96-
"Error resetting configuration",
97-
NotificationType.ERROR,
98-
);
99-
} else {
100-
this.props.notificator.alert(
101-
"Configuration reset successfully!",
102-
NotificationType.SUCCESS,
138+
const alertResult = this.props.notificator.alert(
139+
`Error resetting configuration: ${err}`,
140+
NotificationSeverity.Err,
103141
);
142+
if (alertResult.error) {
143+
console.error(
144+
`basic-config-component: failed to send notification: ${alertResult.error}`,
145+
);
146+
}
104147

105-
await this.readConfig();
148+
return;
106149
}
150+
151+
const alertResult = this.props.notificator.alert(
152+
"Configuration reseted successfully!",
153+
NotificationSeverity.Inf,
154+
);
155+
if (alertResult.error) {
156+
console.error(
157+
`basic-config-component: failed to send notification: ${alertResult.error}`,
158+
);
159+
}
160+
161+
await this.readConfig();
107162
};
108163

109164
// Note: use arrow function to properly capture `this`.

src/system/default_notificator.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
1-
import { Notificator, NotificationType } from "./notificator";
1+
import { Notificator, AlertResult, ConfirmResult } from "./notificator";
2+
import { NotificationSeverity } from "./notification_types";
23

34
// Default browser-based notification.
45
export class DefaultNotificator implements Notificator {
56
// https://developer.mozilla.org/en-US/docs/Web/API/Window/alert
6-
alert(str: string, _: NotificationType): void {
7-
return alert(str);
7+
alert(message: string, _: NotificationSeverity): AlertResult {
8+
alert(message);
9+
10+
return {
11+
promise: Promise.resolve(),
12+
error: null,
13+
};
814
}
915

1016
// https://developer.mozilla.org/en-US/docs/Web/API/Window/confirm
11-
async confirm(str: string): Promise<boolean> {
12-
return confirm(str);
17+
confirm(message: string, _: NotificationSeverity): ConfirmResult {
18+
const result = confirm(message);
19+
20+
return {
21+
promise: Promise.resolve(result),
22+
error: null,
23+
};
1324
}
1425
}

src/system/notification_types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Various notification severities.
2+
export enum NotificationSeverity {
3+
Err = 0,
4+
Wrn,
5+
Inf,
6+
}

src/system/notificator.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1-
// Various notification types.
2-
export enum NotificationType {
3-
ERROR = 0,
4-
SUCCESS = 1,
5-
}
1+
import { NotificationSeverity } from "./notification_types";
2+
3+
export type AlertResult = {
4+
promise: Promise<void> | null;
5+
error: Error | null;
6+
};
7+
8+
export type ConfirmResult = {
9+
promise: Promise<boolean> | null;
10+
error: Error | null;
11+
};
612

13+
// Send notifications.
714
export interface Notificator {
8-
// Alertring the user with @p str message and @p typ notification type.
9-
alert(str: string, typ: NotificationType): void;
15+
// Send an alert dialog.
16+
alert(message: string, severity: NotificationSeverity): AlertResult;
1017

11-
// Confirm the user choice.
12-
confirm(str: string): Promise<boolean>;
18+
// Send a confirmation dialog.
19+
confirm(message: string, severity: NotificationSeverity): ConfirmResult;
1320
}

0 commit comments

Comments
 (0)