2022-03-16 12:33:34 +01:00
|
|
|
import EmberObject from "@ember/object";
|
2024-11-22 17:11:51 +01:00
|
|
|
import { later } from "@ember/runloop";
|
2022-07-26 16:18:09 +02:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2022-03-16 12:33:34 +01:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2022-07-26 16:18:09 +02:00
|
|
|
import { translationOrText } from "discourse/plugins/discourse-custom-wizard/discourse/lib/wizard";
|
2024-11-22 17:11:51 +01:00
|
|
|
import ValidState from "discourse/plugins/discourse-custom-wizard/discourse/mixins/valid-state";
|
2022-03-16 12:33:34 +01:00
|
|
|
|
|
|
|
export default EmberObject.extend(ValidState, {
|
|
|
|
id: null,
|
|
|
|
|
|
|
|
@discourseComputed("wizardId", "id")
|
|
|
|
i18nKey(wizardId, stepId) {
|
|
|
|
return `${wizardId}.${stepId}`;
|
|
|
|
},
|
|
|
|
|
|
|
|
@discourseComputed("i18nKey", "title")
|
|
|
|
translatedTitle(i18nKey, title) {
|
2022-07-26 16:18:09 +02:00
|
|
|
return translationOrText(`${i18nKey}.title`, title);
|
2022-03-16 12:33:34 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
@discourseComputed("i18nKey", "description")
|
|
|
|
translatedDescription(i18nKey, description) {
|
2022-07-26 16:18:09 +02:00
|
|
|
return translationOrText(`${i18nKey}.description`, description);
|
2022-03-16 12:33:34 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
@discourseComputed("index")
|
|
|
|
displayIndex: (index) => index + 1,
|
|
|
|
|
|
|
|
@discourseComputed("fields.[]")
|
|
|
|
fieldsById(fields) {
|
|
|
|
const lookup = {};
|
|
|
|
fields.forEach((field) => (lookup[field.get("id")] = field));
|
|
|
|
return lookup;
|
|
|
|
},
|
|
|
|
|
|
|
|
validate() {
|
|
|
|
let allValid = true;
|
|
|
|
|
|
|
|
this.fields.forEach((field) => {
|
|
|
|
allValid = allValid && field.check();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.setValid(allValid);
|
|
|
|
},
|
|
|
|
|
|
|
|
fieldError(id, description) {
|
|
|
|
const field = this.fields.findBy("id", id);
|
|
|
|
if (field) {
|
|
|
|
field.setValid(false, description);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
save() {
|
|
|
|
const wizardId = this.get("wizardId");
|
|
|
|
const fields = {};
|
|
|
|
|
|
|
|
this.get("fields").forEach((f) => {
|
|
|
|
if (f.type !== "text_only") {
|
|
|
|
fields[f.id] = f.value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return ajax({
|
|
|
|
url: `/w/${wizardId}/steps/${this.get("id")}`,
|
|
|
|
type: "PUT",
|
2024-06-20 11:25:57 +02:00
|
|
|
contentType: "application/json",
|
2024-06-20 13:06:02 +02:00
|
|
|
data: JSON.stringify({ fields }),
|
2022-03-16 12:33:34 +01:00
|
|
|
}).catch((response) => {
|
2022-08-24 11:35:17 +02:00
|
|
|
if (response.jqXHR) {
|
|
|
|
response = response.jqXHR;
|
|
|
|
}
|
2022-06-15 08:59:09 +02:00
|
|
|
if (response && response.responseJSON && response.responseJSON.errors) {
|
2022-03-16 12:33:34 +01:00
|
|
|
let wizardErrors = [];
|
|
|
|
response.responseJSON.errors.forEach((err) => {
|
|
|
|
if (err.field === wizardId) {
|
|
|
|
wizardErrors.push(err.description);
|
|
|
|
} else if (err.field) {
|
|
|
|
this.fieldError(err.field, err.description);
|
|
|
|
} else if (err) {
|
|
|
|
wizardErrors.push(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (wizardErrors.length) {
|
|
|
|
this.handleWizardError(wizardErrors.join("\n"));
|
|
|
|
}
|
|
|
|
this.animateInvalidFields();
|
|
|
|
throw response;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response && response.responseText) {
|
|
|
|
const responseText = response.responseText;
|
|
|
|
const start = responseText.indexOf(">") + 1;
|
|
|
|
const end = responseText.indexOf("plugins");
|
|
|
|
const message = responseText.substring(start, end);
|
|
|
|
this.handleWizardError(message);
|
|
|
|
throw message;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
handleWizardError(message) {
|
|
|
|
this.set("message", {
|
|
|
|
state: "error",
|
|
|
|
text: message,
|
|
|
|
});
|
2022-03-16 14:09:23 +01:00
|
|
|
later(() => this.set("message", null), 6000);
|
2022-03-16 12:33:34 +01:00
|
|
|
},
|
|
|
|
});
|