custom-wizard.js.es6 5.9 KB

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