1
0
Fork 0
discourse-custom-wizard-unl.../assets/javascripts/discourse/models/custom-wizard.js.es6

84 Zeilen
2 KiB
Text

import { ajax } from 'discourse/lib/ajax';
2020-03-21 18:30:11 +01:00
import EmberObject from "@ember/object";
2020-04-02 07:21:57 +02:00
import { buildStepJson, buildJson, buildProperties } from '../lib/wizard-json';
2020-04-05 03:37:09 +02:00
import { Promise } from "rsvp";
2017-11-01 05:21:14 +01:00
2020-03-21 18:30:11 +01:00
const CustomWizard = EmberObject.extend({
2017-10-13 15:02:34 +02:00
save() {
2020-04-05 03:37:09 +02:00
return new Promise((resolve, reject) => {
2020-04-01 12:58:30 +02:00
let wizardJson = buildJson(this, 'wizard');
if (wizardJson.after_time && !wizardJson.after_time_scheduled) {
reject({
error: 'after_time_need_time'
});
2017-11-01 05:21:14 +01:00
};
2020-04-01 12:58:30 +02:00
if (this.steps.length > 0) {
let stepsResult = buildStepJson(this.steps);
if (stepsResult.error ||
!stepsResult.steps ||
stepsResult.steps.length < 1) {
2020-04-02 10:21:03 +02:00
2020-04-01 12:58:30 +02:00
reject({
error: stepsResult.error || 'steps_required'
});
2017-10-30 07:24:51 +01:00
} else {
2020-04-01 12:58:30 +02:00
wizardJson.steps = stepsResult.steps;
2017-10-30 07:24:51 +01:00
}
}
2020-04-01 12:58:30 +02:00
2017-10-13 15:02:34 +02:00
ajax("/admin/wizards/custom/save", {
type: 'PUT',
data: {
2020-04-01 12:58:30 +02:00
wizard: JSON.stringify(wizardJson)
2017-10-13 15:02:34 +02:00
}
2017-10-30 07:24:51 +01:00
}).then((result) => {
if (result.error) {
reject(result);
} else {
resolve(result);
}
});
2017-10-13 15:02:34 +02:00
});
2017-10-07 04:27:38 +02:00
},
2017-09-24 05:01:18 +02:00
remove() {
return ajax("/admin/wizards/custom/remove", {
2017-09-24 05:01:18 +02:00
type: 'DELETE',
data: {
id: this.get('id')
}
}).then(() => this.destroy());
2017-09-23 04:34:07 +02:00
}
});
CustomWizard.reopenClass({
2017-10-22 05:37:58 +02:00
all() {
return ajax("/admin/wizards/custom/all", {
type: 'GET'
}).then(result => {
2020-04-01 14:16:26 +02:00
return result.wizards.map(wizard => {
return CustomWizard.create(wizard);
});
2017-09-23 04:34:07 +02:00
});
},
2017-10-22 05:37:58 +02:00
submissions(wizardId) {
return ajax(`/admin/wizards/submissions/${wizardId}`, {
2017-10-05 02:36:46 +02:00
type: "GET"
}).then(result => {
return result.submissions;
});
},
2020-04-01 12:58:30 +02:00
create(wizardJson = {}) {
const wizard = this._super.apply(this);
2020-04-01 14:16:26 +02:00
wizard.setProperties(buildProperties(wizardJson));
2017-09-23 04:34:07 +02:00
return wizard;
}
});
export default CustomWizard;