From c156dcb6d14e1b0489dbc801cf04787893882bc6 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Wed, 7 Aug 2019 20:38:50 +1000 Subject: [PATCH] Componentize --- .../discourse/components/wizard-export.js.es6 | 42 ++++++ .../discourse/components/wizard-import.js.es6 | 81 ++++++++++++ .../controllers/admin-wizards-transfer.js.es6 | 124 +----------------- .../templates/admin-wizards-transfer.hbs | 64 +-------- .../templates/components/wizard-export.hbs | 25 ++++ .../templates/components/wizard-import.hbs | 32 +++++ 6 files changed, 183 insertions(+), 185 deletions(-) create mode 100644 assets/javascripts/discourse/components/wizard-export.js.es6 create mode 100644 assets/javascripts/discourse/components/wizard-import.js.es6 create mode 100644 assets/javascripts/discourse/templates/components/wizard-export.hbs create mode 100644 assets/javascripts/discourse/templates/components/wizard-import.hbs diff --git a/assets/javascripts/discourse/components/wizard-export.js.es6 b/assets/javascripts/discourse/components/wizard-export.js.es6 new file mode 100644 index 00000000..a2505d24 --- /dev/null +++ b/assets/javascripts/discourse/components/wizard-export.js.es6 @@ -0,0 +1,42 @@ +export default Ember.Component.extend({ + classNames: ['container', 'export'], + selected: Ember.A(), + + actions: { + checkChanged(event) { + this.set('exportMessage', ''); + + let selected = this.get('selected'); + + if (event.target.checked) { + selected.addObject(event.target.id); + } else if (!event.target.checked) { + selected.removeObject(event.target.id); + } + + this.set('selected', selected); + }, + + export() { + const wizards = this.get('selected'); + + if (!wizards.length) { + this.set('exportMessage', I18n.t("admin.wizard.transfer.export.none_selected")); + } else { + this.set('exportMessage', ''); + + let url = Discourse.BaseUrl; + let route = '/admin/wizards/transfer/export'; + url += route + '?'; + + wizards.forEach((wizard) => { + let step = 'wizards[]=' + wizard; + step += '&'; + url += step; + }); + + location.href = url; + } + } + } +}); \ No newline at end of file diff --git a/assets/javascripts/discourse/components/wizard-import.js.es6 b/assets/javascripts/discourse/components/wizard-import.js.es6 new file mode 100644 index 00000000..1cf6618d --- /dev/null +++ b/assets/javascripts/discourse/components/wizard-import.js.es6 @@ -0,0 +1,81 @@ +import { ajax } from 'discourse/lib/ajax'; +import { default as computed } from 'ember-addons/ember-computed-decorators'; + +export default Ember.Component.extend({ + classNames: ['container', 'import'], + hasLogs: Ember.computed.notEmpty('logs'), + + @computed('successIds', 'failureIds') + logs(successIds, failureIds) { + let logs = []; + + if (successIds) { + logs.push(...successIds.map(id => { + return { id, type: 'success' }; + })); + } + + if (failureIds) { + logs.push(...failureIds.map(id => { + return { id, type: 'failure' }; + })); + } + + return logs; + }, + + actions: { + setFilePath(event) { + this.set('importMessage', ''); + + // 512 kb is the max file size + let maxFileSize = 512 * 1024; + + if (event.target.files[0] === undefined) { + this.set('filePath', null); + return; + } + + if (maxFileSize < event.target.files[0].size) { + this.setProperties({ + importMessage: I18n.t('admin.wizard.transfer.import.file_size_error'), + filePath: null + }); + $('#file-url').val(''); + } else { + this.set('filePath', event.target.files[0]); + } + }, + + import() { + const filePath = this.get('filePath'); + let $formData = new FormData(); + + if (filePath) { + $formData.append('file', filePath); + + ajax('/admin/wizards/transfer/import', { + type: 'POST', + data: $formData, + processData: false, + contentType: false, + }).then(result => { + if (result.error) { + this.set('importMessage', result.error); + } else { + this.setProperties({ + successIds: result.success, + failureIds: result.failed, + fileName: $('#file-url')[0].files[0].name + }); + } + + this.set('filePath', null); + $('#file-url').val(''); + }); + } else { + this.set('importMessage', I18n.t("admin.wizard.transfer.import.no_file")); + } + } + } +}); \ No newline at end of file diff --git a/assets/javascripts/discourse/controllers/admin-wizards-transfer.js.es6 b/assets/javascripts/discourse/controllers/admin-wizards-transfer.js.es6 index 3afb4cc9..77c79b72 100644 --- a/assets/javascripts/discourse/controllers/admin-wizards-transfer.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-wizards-transfer.js.es6 @@ -1,123 +1 @@ -import { ajax } from 'discourse/lib/ajax'; -import { default as computed } from 'ember-addons/ember-computed-decorators'; - -export default Ember.Controller.extend({ - init() { - this._super(); - this.setProperties({ - selected: Ember.A() - }); - }, - - @computed('successIds', 'failureIds') - logs(successIds, failureIds) { - let logs = []; - - if (successIds) { - logs.push(...successIds.map(id => { - return { id, type: 'success' }; - })); - } - - if (failureIds) { - logs.push(...failureIds.map(id => { - return { id, type: 'failure' }; - })); - } - - return logs; - }, - - hasLogs: Ember.computed.notEmpty('logs'), - - actions: { - checkChanged(event) { - this.set('exportMessage', ''); - - let selected = this.get('selected'); - - if (event.target.checked) { - selected.addObject(event.target.id); - } else if (!event.target.checked) { - selected.removeObject(event.target.id); - } - - this.set('selected', selected); - }, - - export() { - const wizards = this.get('selected'); - - if (!wizards.length) { - this.set('exportMessage', I18n.t("admin.wizard.transfer.export.none_selected")); - } else { - this.set('exportMessage', ''); - - let url = Discourse.BaseUrl; - let route = '/admin/wizards/transfer/export'; - url += route + '?'; - - wizards.forEach((wizard) => { - let step = 'wizards[]=' + wizard; - step += '&'; - url += step; - }); - - location.href = url; - } - }, - - setFilePath(event) { - this.set('importMessage', ''); - - // 512 kb is the max file size - let maxFileSize = 512 * 1024; - - if (event.target.files[0] === undefined) { - this.set('filePath', null); - return; - } - - if (maxFileSize < event.target.files[0].size) { - this.setProperties({ - importMessage: I18n.t('admin.wizard.transfer.import.file_size_error'), - filePath: null - }); - $('#file-url').val(''); - } else { - this.set('filePath', event.target.files[0]); - } - }, - - import() { - const filePath = this.get('filePath'); - let $formData = new FormData(); - - if (filePath) { - $formData.append('file', filePath); - - ajax('/admin/wizards/transfer/import', { - type: 'POST', - data: $formData, - processData: false, - contentType: false, - }).then(result => { - if (result.error) { - this.set('importMessage', result.error); - } else { - this.setProperties({ - successIds: result.success, - failureIds: result.failed, - fileName: $('#file-url')[0].files[0].name - }); - } - - this.set('filePath', null); - $('#file-url').val(''); - }); - } else { - this.set('importMessage', I18n.t("admin.wizard.transfer.import.no_file")); - } - } - } -}); +export default Ember.Controller.extend(); diff --git a/assets/javascripts/discourse/templates/admin-wizards-transfer.hbs b/assets/javascripts/discourse/templates/admin-wizards-transfer.hbs index b326fd4d..ff36a823 100644 --- a/assets/javascripts/discourse/templates/admin-wizards-transfer.hbs +++ b/assets/javascripts/discourse/templates/admin-wizards-transfer.hbs @@ -1,62 +1,2 @@ -
-

{{i18n 'admin.wizard.transfer.export.label'}}

- - - - {{d-button id="export-button" - class="btn btn-primary side" - label="admin.wizard.transfer.export.label" - action=(action "export")}} - - {{#if exportMessage}} -
- {{exportMessage}} -
- {{/if}} -
- -
-

{{i18n 'admin.wizard.transfer.import.label'}}

- -
- {{input id='file-url' type="file" change=(action "setFilePath")}} - - {{#if importMessage}} -
- {{importMessage}} -
- {{/if}} - - {{d-button id="import-button" - class="btn btn-primary side" - label="admin.wizard.transfer.import.label" - action=(action "import")}} -
- - {{#if hasLogs}} -
-
- {{i18n 'admin.wizard.transfer.import.logs' fileName=fileName}} -
- -
    - {{#each logs as |l|}} -
  • - {{i18n (concat 'admin.wizard.transfer.import.' l.type) id=l.id}} -
  • - {{/each}} -
-
- {{/if}} -
+{{wizard-export wizards=model}} +{{wizard-import}} diff --git a/assets/javascripts/discourse/templates/components/wizard-export.hbs b/assets/javascripts/discourse/templates/components/wizard-export.hbs new file mode 100644 index 00000000..869b372a --- /dev/null +++ b/assets/javascripts/discourse/templates/components/wizard-export.hbs @@ -0,0 +1,25 @@ +

{{i18n 'admin.wizard.transfer.export.label'}}

+ + + +{{d-button id="export-button" + class="btn btn-primary side" + label="admin.wizard.transfer.export.label" + action=(action "export")}} + +{{#if exportMessage}} +
+ {{exportMessage}} +
+{{/if}} \ No newline at end of file diff --git a/assets/javascripts/discourse/templates/components/wizard-import.hbs b/assets/javascripts/discourse/templates/components/wizard-import.hbs new file mode 100644 index 00000000..9f46e32b --- /dev/null +++ b/assets/javascripts/discourse/templates/components/wizard-import.hbs @@ -0,0 +1,32 @@ +

{{i18n 'admin.wizard.transfer.import.label'}}

+ +
+ {{input id='file-url' type="file" change=(action "setFilePath")}} + + {{#if importMessage}} +
+ {{importMessage}} +
+ {{/if}} + + {{d-button id="import-button" + class="btn btn-primary side" + label="admin.wizard.transfer.import.label" + action=(action "import")}} +
+ +{{#if hasLogs}} +
+
+ {{i18n 'admin.wizard.transfer.import.logs' fileName=fileName}} +
+ + +
+{{/if}} \ No newline at end of file