2021-01-27 06:08:26 +01:00
|
|
|
import Component from "@ember/component";
|
2021-02-05 13:59:30 +01:00
|
|
|
import EmberObject from "@ember/object";
|
|
|
|
import { cloneJSON } from "discourse-common/lib/object";
|
|
|
|
import Category from "discourse/models/category";
|
2021-02-25 11:06:43 +01:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
|
|
|
import I18n from "I18n";
|
2021-01-27 06:08:26 +01:00
|
|
|
|
2021-01-30 18:46:04 +01:00
|
|
|
export default Component.extend({
|
2021-02-15 09:09:37 +01:00
|
|
|
classNames: ["realtime-validations"],
|
2021-02-25 11:06:43 +01:00
|
|
|
@discourseComputed
|
|
|
|
timeUnits() {
|
2021-03-28 11:06:49 +02:00
|
|
|
return ["days", "weeks", "months", "years"].map((unit) => {
|
|
|
|
return {
|
|
|
|
id: unit,
|
|
|
|
name: I18n.t(`admin.wizard.field.validations.time_units.${unit}`),
|
|
|
|
};
|
|
|
|
});
|
2021-02-25 11:06:43 +01:00
|
|
|
},
|
2021-01-30 18:46:04 +01:00
|
|
|
|
2021-02-15 09:09:37 +01:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
if (!this.validations) return;
|
2021-01-30 18:46:04 +01:00
|
|
|
|
2021-02-15 09:09:37 +01:00
|
|
|
if (!this.field.validations) {
|
|
|
|
const validations = {};
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2021-02-15 09:09:37 +01:00
|
|
|
this.validations.forEach((validation) => {
|
|
|
|
validations[validation] = {};
|
|
|
|
});
|
2021-02-05 13:59:30 +01:00
|
|
|
|
2021-02-15 09:09:37 +01:00
|
|
|
this.set("field.validations", EmberObject.create(validations));
|
|
|
|
}
|
2021-02-05 13:59:30 +01:00
|
|
|
|
2021-02-15 09:09:37 +01:00
|
|
|
const validationBuffer = cloneJSON(this.get("field.validations"));
|
|
|
|
let bufferCategories;
|
|
|
|
if (
|
|
|
|
validationBuffer.similar_topics &&
|
|
|
|
(bufferCategories = validationBuffer.similar_topics.categories)
|
|
|
|
) {
|
|
|
|
const categories = Category.findByIds(bufferCategories);
|
|
|
|
validationBuffer.similar_topics.categories = categories;
|
2021-01-30 18:46:04 +01:00
|
|
|
}
|
2021-02-15 09:09:37 +01:00
|
|
|
this.set("validationBuffer", validationBuffer);
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
2021-02-16 01:43:00 +01:00
|
|
|
updateValidationCategories(type, validation, categories) {
|
|
|
|
this.set(`validationBuffer.${type}.categories`, categories);
|
2021-02-15 09:09:37 +01:00
|
|
|
this.set(
|
2021-02-16 01:43:00 +01:00
|
|
|
`field.validations.${type}.categories`,
|
2021-02-15 09:09:37 +01:00
|
|
|
categories.map((category) => category.id)
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
2021-01-30 18:46:04 +01:00
|
|
|
});
|