0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-26 11:00:28 +01:00
discourse-custom-wizard/assets/javascripts/discourse/models/custom-wizard.js.es6
Angus McLeod 0d94c1d8c6 various
2020-04-02 19:21:03 +11:00

82 Zeilen
2 KiB
JavaScript

import { ajax } from 'discourse/lib/ajax';
import EmberObject from "@ember/object";
import { buildStepJson, buildJson, buildProperties } from '../lib/wizard-json';
const CustomWizard = EmberObject.extend({
save() {
return new Ember.RSVP.Promise((resolve, reject) => {
let wizardJson = buildJson(this, 'wizard');
if (wizardJson.after_time && !wizardJson.after_time_scheduled) {
reject({
error: 'after_time_need_time'
});
};
if (this.steps.length > 0) {
let stepsResult = buildStepJson(this.steps);
if (stepsResult.error ||
!stepsResult.steps ||
stepsResult.steps.length < 1) {
reject({
error: stepsResult.error || 'steps_required'
});
} else {
wizardJson.steps = stepsResult.steps;
}
}
ajax("/admin/wizards/custom/save", {
type: 'PUT',
data: {
wizard: JSON.stringify(wizardJson)
}
}).then((result) => {
if (result.error) {
reject(result);
} else {
resolve(result);
}
});
});
},
remove() {
return ajax("/admin/wizards/custom/remove", {
type: 'DELETE',
data: {
id: this.get('id')
}
}).then(() => this.destroy());
}
});
CustomWizard.reopenClass({
all() {
return ajax("/admin/wizards/custom/all", {
type: 'GET'
}).then(result => {
return result.wizards.map(wizard => {
return CustomWizard.create(wizard);
});
});
},
submissions(wizardId) {
return ajax(`/admin/wizards/submissions/${wizardId}`, {
type: "GET"
}).then(result => {
return result.submissions;
});
},
create(wizardJson = {}) {
const wizard = this._super.apply(this);
wizard.setProperties(buildProperties(wizardJson));
return wizard;
}
});
export default CustomWizard;