custom-wizard.js.es6 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import { ajax } from 'discourse/lib/ajax';
  2. const wizardProperties = [
  3. 'name',
  4. 'background',
  5. 'save_submissions',
  6. 'multiple_submissions',
  7. 'after_signup',
  8. 'after_time',
  9. 'after_time_scheduled',
  10. 'required'
  11. ];
  12. const CustomWizard = Discourse.Model.extend({
  13. save() {
  14. return new Ember.RSVP.Promise((resolve, reject) => {
  15. const id = this.get('id');
  16. if (!id || !id.underscore()) return reject({ error: 'id_required' });
  17. let wizard = { id: id.underscore() };
  18. wizardProperties.forEach((p) => {
  19. const value = this.get(p);
  20. if (value) wizard[p] = value;
  21. });
  22. if (wizard['after_time'] && !wizard['after_time_scheduled']) {
  23. return reject({ error: 'after_time_need_time' });
  24. };
  25. const steps = this.get('steps');
  26. if (steps.length > 0) {
  27. const stepsResult = this.buildSteps(steps);
  28. if (stepsResult.error) {
  29. reject({ error: stepsResult.error });
  30. } else {
  31. wizard['steps'] = stepsResult.steps;
  32. }
  33. }
  34. if (steps.length < 1 || !wizard['steps'] || wizard['steps'].length < 1) {
  35. return reject({ error: 'steps_required' });
  36. }
  37. ajax("/admin/wizards/custom/save", {
  38. type: 'PUT',
  39. data: {
  40. wizard: JSON.stringify(wizard)
  41. }
  42. }).then((result) => {
  43. if (result.error) {
  44. reject(result);
  45. } else {
  46. resolve(result);
  47. }
  48. });
  49. });
  50. },
  51. buildSteps(stepsObj) {
  52. let steps = [];
  53. let error = null;
  54. stepsObj.some((s) => {
  55. if (!s.id || !s.id.underscore()) {
  56. error = 'id_required';
  57. return;
  58. };
  59. let step = { id: s.id.underscore() };
  60. if (s.title) step['title'] = s.title;
  61. if (s.key) step['key'] = s.key;
  62. if (s.banner) step['banner'] = s.banner;
  63. if (s.description) step['description'] = s.description;
  64. const fields = s.get('fields');
  65. if (fields.length) {
  66. step['fields'] = [];
  67. fields.some((f) => {
  68. let id = f.id;
  69. if (!id || !id.underscore()) {
  70. error = 'id_required';
  71. return;
  72. }
  73. f.set('id', id.underscore());
  74. if (f.label === '') delete f.label;
  75. if (f.description === '') delete f.description;
  76. if (f.type === 'dropdown') {
  77. const choices = f.choices;
  78. if ((!choices || choices.length < 1) && !f.choices_key && !f.choices_categories) {
  79. error = 'field.need_choices';
  80. return;
  81. }
  82. }
  83. delete f.isNew;
  84. step['fields'].push(f);
  85. });
  86. if (error) return;
  87. }
  88. const actions = s.actions;
  89. if (actions.length) {
  90. step['actions'] = [];
  91. actions.some((a) => {
  92. let id = a.get('id');
  93. if (!id || !id.underscore()) {
  94. error = 'id_required';
  95. return;
  96. }
  97. a.set('id', id.underscore());
  98. delete a.isNew;
  99. step['actions'].push(a);
  100. });
  101. if (error) return;
  102. }
  103. steps.push(step);
  104. });
  105. if (error) {
  106. return { error };
  107. } else {
  108. return { steps };
  109. };
  110. },
  111. remove() {
  112. return ajax("/admin/wizards/custom/remove", {
  113. type: 'DELETE',
  114. data: {
  115. id: this.get('id')
  116. }
  117. }).then(() => this.destroy());
  118. }
  119. });
  120. CustomWizard.reopenClass({
  121. all() {
  122. return ajax("/admin/wizards/custom/all", {
  123. type: 'GET'
  124. }).then(result => {
  125. return result.wizards.map(w => CustomWizard.create(w));
  126. });
  127. },
  128. submissions(wizardId) {
  129. return ajax(`/admin/wizards/submissions/${wizardId}`, {
  130. type: "GET"
  131. }).then(result => {
  132. return result.submissions;
  133. });
  134. },
  135. create(w) {
  136. const wizard = this._super.apply(this);
  137. let steps = Ember.A();
  138. let props = { steps };
  139. if (w) {
  140. props['id'] = w.id;
  141. props['existingId'] = true;
  142. wizardProperties.forEach((p) => {
  143. props[p] = w[p];
  144. });
  145. if (w.steps && w.steps.length) {
  146. w.steps.forEach((s) => {
  147. // clean empty strings
  148. Object.keys(s).forEach((key) => (s[key] === '') && delete s[key]);
  149. let fields = Ember.A();
  150. if (s.fields && s.fields.length) {
  151. s.fields.forEach((f) => {
  152. Object.keys(f).forEach((key) => (f[key] === '') && delete f[key]);
  153. const fieldParams = { isNew: false };
  154. let field = Ember.Object.create(Object.assign(f, fieldParams));
  155. if (f.choices) {
  156. let choices = Ember.A();
  157. f.choices.forEach((c) => {
  158. choices.pushObject(Ember.Object.create(c));
  159. });
  160. field.set('choices', choices);
  161. }
  162. fields.pushObject(field);
  163. });
  164. }
  165. let actions = Ember.A();
  166. if (s.actions && s.actions.length) {
  167. s.actions.forEach((a) => {
  168. const actionParams = { isNew: false };
  169. const action = Ember.Object.create(Object.assign(a, actionParams));
  170. actions.pushObject(action);
  171. });
  172. }
  173. steps.pushObject(Ember.Object.create({
  174. id: s.id,
  175. key: s.key,
  176. title: s.title,
  177. description: s.description,
  178. banner: s.banner,
  179. fields,
  180. actions,
  181. isNew: false
  182. }));
  183. });
  184. };
  185. } else {
  186. props['id'] = '';
  187. props['name'] = '';
  188. props['background'] = '';
  189. props['save_submissions'] = true;
  190. props['multiple_submissions'] = false;
  191. props['after_signup'] = false;
  192. props['after_time'] = false;
  193. props['required'] = false;
  194. props['steps'] = Ember.A();
  195. };
  196. wizard.setProperties(props);
  197. return wizard;
  198. }
  199. });
  200. export default CustomWizard;