Spiegel von
https://github.com/paviliondev/discourse-custom-wizard.git
synchronisiert 2024-11-30 04:30:29 +01:00
115 Zeilen
3 KiB
JavaScript
115 Zeilen
3 KiB
JavaScript
import EmberObject from "@ember/object";
|
|
import ValidState from "wizard/mixins/valid-state";
|
|
import { ajax } from "wizard/lib/ajax";
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
|
import { translatedText } from "discourse/plugins/discourse-custom-wizard/wizard/lib/wizard-i18n";
|
|
import { later } from "@ember/runloop";
|
|
|
|
export default EmberObject.extend(ValidState, {
|
|
id: null,
|
|
|
|
@discourseComputed("wizardId", "id")
|
|
i18nKey(wizardId, stepId) {
|
|
return `${wizardId}.${stepId}`;
|
|
},
|
|
|
|
@discourseComputed("i18nKey", "title")
|
|
translatedTitle(i18nKey, title) {
|
|
return translatedText(`${i18nKey}.title`, title);
|
|
},
|
|
|
|
@discourseComputed("i18nKey", "description")
|
|
translatedDescription(i18nKey, description) {
|
|
return translatedText(`${i18nKey}.description`, description);
|
|
},
|
|
|
|
@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;
|
|
const result = { warnings: [] };
|
|
|
|
this.fields.forEach((field) => {
|
|
allValid = allValid && field.check();
|
|
const warning = field.get("warning");
|
|
if (warning) {
|
|
result.warnings.push(warning);
|
|
}
|
|
});
|
|
|
|
this.setValid(allValid);
|
|
|
|
return result;
|
|
},
|
|
|
|
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",
|
|
data: { fields },
|
|
}).catch((response) => {
|
|
if (
|
|
response &&
|
|
response.responseJSON &&
|
|
response.responseJSON.errors
|
|
) {
|
|
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,
|
|
});
|
|
later(() => this.set("message", null), 6000);
|
|
},
|
|
});
|