|
1 | 1 | import { Component } from "preact"; |
2 | 2 |
|
3 | 3 | import { Config } from "./config"; |
4 | | -import { Notificator, NotificationType } from "../system/notificator"; |
| 4 | +import { Notificator } from "../system/notificator"; |
| 5 | +import { NotificationSeverity } from "../system/notification_types"; |
5 | 6 |
|
6 | 7 | type configState = { |
7 | 8 | data: Record<string, any> | null; |
@@ -52,58 +53,112 @@ export abstract class BasicConfigComponent extends Component< |
52 | 53 |
|
53 | 54 | // Note: use arrow function to properly capture `this`. |
54 | 55 | 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) { |
60 | 76 | return; |
61 | 77 | } |
62 | 78 |
|
63 | 79 | const err = await this.props.config.write(this.state.data!); |
64 | 80 | if (err) { |
65 | 81 | console.error(`basic-config-component: error updating config: ${err}`); |
66 | 82 |
|
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, |
75 | 86 | ); |
| 87 | + if (alertResult.error) { |
| 88 | + console.error( |
| 89 | + `basic-config-component: failed to send notification: ${alertResult.error}`, |
| 90 | + ); |
| 91 | + } |
76 | 92 |
|
77 | | - await this.readConfig(); |
| 93 | + return; |
78 | 94 | } |
| 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(); |
79 | 107 | }; |
80 | 108 |
|
81 | 109 | // Note: use arrow function to properly capture `this`. |
82 | 110 | 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) { |
88 | 131 | return; |
89 | 132 | } |
90 | 133 |
|
91 | 134 | const err = await this.props.config.reset(); |
92 | 135 | if (err) { |
93 | 136 | console.error("basic-config-component: error resetting config:", err); |
94 | 137 |
|
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, |
103 | 141 | ); |
| 142 | + if (alertResult.error) { |
| 143 | + console.error( |
| 144 | + `basic-config-component: failed to send notification: ${alertResult.error}`, |
| 145 | + ); |
| 146 | + } |
104 | 147 |
|
105 | | - await this.readConfig(); |
| 148 | + return; |
106 | 149 | } |
| 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(); |
107 | 162 | }; |
108 | 163 |
|
109 | 164 | // Note: use arrow function to properly capture `this`. |
|
0 commit comments