0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-10 04:12:53 +01:00
discourse-custom-wizard/assets/javascripts/discourse/models/custom-wizard.js.es6
Angus McLeod 2046648862 various
2020-04-01 21:58:30 +11:00

84 Zeilen
1,9 KiB
JavaScript

import { ajax } from 'discourse/lib/ajax';
import EmberObject from "@ember/object";
import { buildStepJson, buildJson, buildWizardProperties } from '../lib/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(w => CustomWizard.create(w));
});
},
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(buildWizardProperties(wizardJson));
return wizard;
}
});
export default CustomWizard;