Skip to content
This repository was archived by the owner on Jul 14, 2025. It is now read-only.

Commit 30ac835

Browse files
authored
FIX: Form validation bugs, new modal api, glimmer (#182)
1 parent bc81d30 commit 30ac835

31 files changed

+584
-693
lines changed

admin/assets/javascripts/admin/components/channel-details.hbs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@
55
@icon="pencil-alt"
66
@title="chat_integration.edit_channel"
77
@label="chat_integration.edit_channel"
8-
@action={{action @editChannel @channel}}
8+
@action={{fn @editChannel @channel}}
99
/>
1010

1111
<DButton
1212
@icon="rocket"
1313
@title="chat_integration.test_channel"
1414
@label="chat_integration.test_channel"
15-
@action={{action @test @channel}}
15+
@action={{fn @test @channel}}
1616
@class="btn-chat-test"
1717
/>
1818

1919
<DButton
2020
@icon="trash-alt"
2121
@title="chat_integration.delete_channel"
2222
@label="chat_integration.delete_channel"
23-
@action={{action this.deleteChannel @channel}}
23+
@action={{fn this.deleteChannel @channel}}
2424
@class="cancel delete-channel"
2525
/>
2626
</div>
@@ -30,7 +30,7 @@
3030
<DButton
3131
@class="delete btn-danger"
3232
@icon="exclamation-triangle"
33-
@action={{action @showError @channel}}
33+
@action={{fn @showError @channel}}
3434
/>
3535
{{/if}}
3636

@@ -56,7 +56,7 @@
5656
{{#each @channel.rules as |rule|}}
5757
<RuleRow
5858
@rule={{rule}}
59-
@edit={{action @editRuleWithChannel rule @channel}}
59+
@edit={{fn @editRuleWithChannel rule @channel}}
6060
@refresh={{@refresh}}
6161
/>
6262
{{/each}}
@@ -71,7 +71,7 @@
7171
@icon="plus"
7272
@title="chat_integration.create_rule"
7373
@label="chat_integration.create_rule"
74-
@action={{action @createRule @channel}}
74+
@action={{fn @createRule @channel}}
7575
/>
7676
</div>
7777
</div>

admin/assets/javascripts/admin/components/channel-param-row.hbs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{{i18n
55
(concat
66
"chat_integration.provider."
7-
@model.channel.provider
7+
@channel.provider
88
".param."
99
@param.key
1010
".title"
@@ -13,14 +13,14 @@
1313
</label>
1414
</td>
1515
<td>
16-
<Input
17-
name={{concat "param-" @param.key}}
18-
@value={{this.inputValue}}
19-
{{on "change" this.updateValue}}
16+
<input
17+
{{on "input" this.updateValue}}
18+
value={{get @channel.data @param.key}}
19+
type="text"
20+
name="param-{{@param.key}}"
2021
/>
2122

22-
<InputTip @validation={{this.validate}} />
23-
23+
<InputTip @validation={{this.validation}} />
2424
</td>
2525
</tr>
2626

@@ -31,7 +31,7 @@
3131
{{i18n
3232
(concat
3333
"chat_integration.provider."
34-
@model.channel.provider
34+
@channel.provider
3535
".param."
3636
@param.key
3737
".help"
Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,34 @@
11
import Component from "@glimmer/component";
2-
import { tracked } from "@glimmer/tracking";
3-
import EmberObject, { action } from "@ember/object";
2+
import { action } from "@ember/object";
43
import I18n from "I18n";
54

65
export default class ChannelParamRow extends Component {
7-
@tracked inputValue = this.args.model.channel.data[this.args.param.key] || "";
6+
get validation() {
7+
const value = this.args.channel.get(`data.${this.args.param.key}`);
88

9-
get validate() {
10-
const parameter = this.args.param;
11-
const regString = parameter.regex;
12-
const regex = new RegExp(regString);
13-
14-
if (this.inputValue === "") {
15-
// Fail silently if field blank
16-
this.args.isValidParams(false);
17-
return EmberObject.create({
18-
failed: true,
19-
});
20-
} else if (!regString) {
21-
// Pass silently if no regex available for provider
22-
this.args.isValidParams(true);
23-
return EmberObject.create({
24-
ok: true,
25-
});
26-
} else if (regex.test(this.inputValue)) {
27-
// Test against regex
28-
this.args.isValidParams(true);
29-
return EmberObject.create({
9+
if (!value?.trim()) {
10+
return { failed: true };
11+
} else if (!this.args.param.regex) {
12+
return { ok: true };
13+
} else if (new RegExp(this.args.param.regex).test(value)) {
14+
return {
3015
ok: true,
3116
reason: I18n.t(
3217
"chat_integration.edit_channel_modal.channel_validation.ok"
3318
),
34-
});
19+
};
3520
} else {
36-
// Failed regex
37-
this.args.isValidParams(false);
38-
return EmberObject.create({
21+
return {
3922
failed: true,
4023
reason: I18n.t(
4124
"chat_integration.edit_channel_modal.channel_validation.fail"
4225
),
43-
});
26+
};
4427
}
4528
}
4629

4730
@action
4831
updateValue(event) {
49-
this.args.model.channel.data[this.args.param.key] = event.target.value;
32+
this.args.channel.set(`data.${this.args.param.key}`, event.target.value);
5033
}
5134
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<DModal @closeModal={{@closeModal}} id="chat_integration_error_modal">
2+
<h4>{{i18n @model.error_key}}</h4>
3+
<pre>{{@model.error_info}}</pre>
4+
</DModal>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<DModal
2+
{{on "submit" this.save}}
3+
@title={{i18n "chat_integration.edit_channel_modal.title"}}
4+
@closeModal={{@closeModal}}
5+
@tagName="form"
6+
id="chat-integration-edit-channel-modal"
7+
>
8+
<:body>
9+
<table>
10+
<tbody>
11+
<tr class="input">
12+
<td class="label">
13+
<label for="provider">
14+
{{i18n "chat_integration.edit_channel_modal.provider"}}
15+
</label>
16+
</td>
17+
<td>
18+
{{i18n
19+
(concat
20+
"chat_integration.provider." @model.channel.provider ".title"
21+
)
22+
}}
23+
</td>
24+
</tr>
25+
26+
<tr class="chat-instructions">
27+
<td></td>
28+
<td></td>
29+
</tr>
30+
31+
{{#each @model.provider.channel_parameters as |param|}}
32+
<ChannelParamRow @param={{param}} @channel={{@model.channel}} />
33+
{{/each}}
34+
</tbody>
35+
</table>
36+
</:body>
37+
38+
<:footer>
39+
<DButton
40+
@action={{this.save}}
41+
@label="chat_integration.edit_channel_modal.save"
42+
@disabled={{not this.validParams}}
43+
type="submit"
44+
id="save-channel"
45+
class="btn-primary btn-large"
46+
/>
47+
48+
<DButton
49+
@action={{@closeModal}}
50+
@label="chat_integration.edit_channel_modal.cancel"
51+
class="btn-large"
52+
/>
53+
</:footer>
54+
</DModal>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import Component from "@glimmer/component";
2+
import { action } from "@ember/object";
3+
import { popupAjaxError } from "discourse/lib/ajax-error";
4+
5+
export default class EditChannel extends Component {
6+
get validParams() {
7+
return this.args.model.provider.channel_parameters.every((param) => {
8+
const value = this.args.model.channel.get(`data.${param.key}`);
9+
10+
if (!value?.trim()) {
11+
return false;
12+
}
13+
14+
if (!param.regex) {
15+
return true;
16+
}
17+
18+
return new RegExp(param.regex).test(value);
19+
});
20+
}
21+
22+
@action
23+
async save() {
24+
try {
25+
await this.args.model.channel.save();
26+
this.args.closeModal();
27+
} catch (e) {
28+
popupAjaxError(e);
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)