Osama Sayegh a92cf019db
FIX: Make cancel and reset buttons work for file_size_restriction settings (#28347)
This commit fixes a number of bugs in `file_size_restriction` settings and does a little of refactoring to reduce duplicated code in site setting types (the refactoring is necessary to fix one of the bugs).

The bugs in `file_size_restriction` settings that are fixed in this commit:

1. Save/cancel buttons next to a `file_size_restriction` setting are shown upon navigating to the settings page without changes being made to the setting
2. Cancel button that discards changes made to the setting doesn't work
3. Reset button that resets the setting to its default doesn't work
4. Validation error message isn't cleared when resetting/cancelling changes

To repro those bugs, navigate to `/admin/site_settings/category/files` and observe the top 2 settings in the page (`max image size kb` and `max attachment size kb`).

Internal topic: t/134726.
2024-08-15 19:38:47 +03:00

117 lines
2.5 KiB
JavaScript

import Component from "@ember/component";
import { action, set } from "@ember/object";
import { isEmpty } from "@ember/utils";
import { classNameBindings } from "@ember-decorators/component";
import I18n from "discourse-i18n";
@classNameBindings(":value-list", ":secret-value-list")
export default class SecretValueList extends Component {
inputDelimiter = null;
collection = null;
values = null;
didReceiveAttrs() {
super.didReceiveAttrs(...arguments);
this.set(
"collection",
this._splitValues(this.values, this.inputDelimiter || "\n")
);
}
@action
changeKey(index, event) {
const newValue = event.target.value;
if (this._checkInvalidInput(newValue)) {
return;
}
this._replaceValue(index, newValue, "key");
}
@action
changeSecret(index, event) {
const newValue = event.target.value;
if (this._checkInvalidInput(newValue)) {
return;
}
this._replaceValue(index, newValue, "secret");
}
@action
addValue() {
if (this._checkInvalidInput([this.newKey, this.newSecret])) {
return;
}
this._addValue(this.newKey, this.newSecret);
this.setProperties({ newKey: "", newSecret: "" });
}
@action
removeValue(value) {
this._removeValue(value);
}
_checkInvalidInput(inputs) {
for (let input of inputs) {
if (isEmpty(input) || input.includes("|")) {
this.setValidationMessage(
I18n.t("admin.site_settings.secret_list.invalid_input")
);
return true;
}
}
this.setValidationMessage(null);
}
_addValue(value, secret) {
this.collection.addObject({ key: value, secret });
this._saveValues();
}
_removeValue(value) {
const collection = this.collection;
collection.removeObject(value);
this._saveValues();
}
_replaceValue(index, newValue, keyName) {
let item = this.collection[index];
set(item, keyName, newValue);
this._saveValues();
}
_saveValues() {
this.set(
"values",
this.collection
.map(function (elem) {
return `${elem.key}|${elem.secret}`;
})
.join("\n")
);
}
_splitValues(values, delimiter) {
if (values && values.length) {
const keys = ["key", "secret"];
let res = [];
values.split(delimiter).forEach(function (str) {
let object = {};
str.split("|").forEach(function (a, i) {
object[keys[i]] = a;
});
res.push(object);
});
return res;
} else {
return [];
}
}
}