From 25792f92e48c3b03ead81b1b4ae66cf56163cdc1 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Tue, 6 Aug 2019 16:38:05 +0530 Subject: [PATCH] Fixed code formatting, tweaked css, moved remaining hardcoded strings to translations and removed all console.log calls --- .../controllers/admin-wizards-transfer.js.es6 | 16 ++--- .../templates/admin-wizards-transfer.hbs | 69 ++++++++----------- .../stylesheets/wizard/wizard_transfer.scss | 19 ----- assets/stylesheets/wizard_custom_admin.scss | 23 ++++++- config/locales/client.en.yml | 11 ++- controllers/transfer.rb | 8 +-- plugin.rb | 1 - 7 files changed, 70 insertions(+), 77 deletions(-) delete mode 100644 assets/stylesheets/wizard/wizard_transfer.scss diff --git a/assets/javascripts/discourse/controllers/admin-wizards-transfer.js.es6 b/assets/javascripts/discourse/controllers/admin-wizards-transfer.js.es6 index 5fd428e0..91bf2638 100644 --- a/assets/javascripts/discourse/controllers/admin-wizards-transfer.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-wizards-transfer.js.es6 @@ -1,4 +1,4 @@ -import {ajax} from 'discourse/lib/ajax'; +import { ajax } from 'discourse/lib/ajax'; export default Ember.Controller.extend({ init() { @@ -7,16 +7,15 @@ export default Ember.Controller.extend({ this.set('filePath', Ember.A()); }, - actions: { checkChanged(event) { + this.set('noneSelected','') let selected = this.get('selected'); if (event.target.checked) { selected.addObject(event.target.id) } else if (!event.target.checked) { selected.removeObject(event.target.id) } - console.log(selected); this.set('selected', selected) }, @@ -26,7 +25,7 @@ export default Ember.Controller.extend({ let route = '/admin/wizards/transfer/export'; url += route + '?'; if (!wizards.length) { - this.set('noneSelected', "Please select atleast one wizard") + this.set('noneSelected', I18n.t("admin.wizard.transfer.export.noneSelected")) } else { this.set('noneSelected', '') wizards.forEach((wizard) => { @@ -39,6 +38,7 @@ export default Ember.Controller.extend({ }, setFilePath(event) { + this.set('noFile', '') // 512 kb is the max file size let maxFileSize = 512 * 1024; if (event.target.files[0] === undefined) { @@ -46,24 +46,22 @@ export default Ember.Controller.extend({ return } if (maxFileSize < event.target.files[0].size) { - this.set('fileError', 'The file size is too big') + this.set('fileSizeError', I18n.t('admin.wizard.transfer.import.fileSizeError')) } else { + this.set('fileSizeError', '') // emptying the array as we allow only one file upload at a time this.get('filePath').length = 0 // interestingly, this.get gives us the actual reference to the object so modifying it // actually modifies the actual value this.get('filePath').addObject(event.target.files[0]) - console.log(this.get('filePath')) } }, import() { let $formData = new FormData(); - console.log(this.get('filePath')) if (this.get('filePath').length) { this.set('noFile', '') $formData.append('file', this.get('filePath')[0]); - console.log($formData); ajax('/admin/wizards/transfer/import', { type: 'POST', data: $formData, @@ -78,7 +76,7 @@ export default Ember.Controller.extend({ } }) } else { - this.set('noFile', 'Please choose a file to export') + this.set('noFile',I18n.t("admin.wizard.transfer.import.noFile")) } } } diff --git a/assets/javascripts/discourse/templates/admin-wizards-transfer.hbs b/assets/javascripts/discourse/templates/admin-wizards-transfer.hbs index de856753..ad8df8d4 100644 --- a/assets/javascripts/discourse/templates/admin-wizards-transfer.hbs +++ b/assets/javascripts/discourse/templates/admin-wizards-transfer.hbs @@ -1,55 +1,44 @@ -

Export

- +
+

{{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 this.noneSelected}} +

{{this.noneSelected}}

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

Import

+
+

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

{{input id='file_url' type="file" change=(action "setFilePath")}} +{{d-button id="import-button" class="btn btn-primary side" label="admin.wizard.transfer.import.label" action=(action "import")}} + {{#if this.noFile}} +

{{this.noFile}}

+ {{/if}} + {{#if this.fileSizeError}} +

{{this.fileSizeError}}

+ {{/if}} + {{#if this.success_ids}} + {{#each this.success_ids as |id|}} +

{{i18n "admin.wizard.transfer.import.success"}}

+ {{/each}} + {{/if}} -{{d-button id="import-button" class="btn btn-primary side" label="admin.wizard.transfer.import" - action=(action "import")}} - -{{#if this.noneSelected}} -

{{this.noneSelected}}

-{{/if}} - -{{#if this.noFile}} -

{{this.noFile}}

-{{/if}} -{{#if this.fileError}} -

{{this.fileError}}

-{{/if}} - + {{#if this.failure_ids}} + {{#each this.failure_ids as |id|}} +

{{i18n "admin.wizard.transfer.import.failure" id=id}}

+ {{/each}} + {{/if}} +
+{{!-- server side error--}} {{#if this.error}}

{{this.error}}

{{/if}} - -{{#if this.success_ids}} - {{#each this.success_ids as |id|}} -

Wizard: {{id}} saved successfully

- {{/each}} -{{/if}} - -{{#if this.failure_ids}} - {{#each this.failure_ids as |id|}} -

Wizard: {{id}} could not be saved

- {{/each}} -{{/if}} - - diff --git a/assets/stylesheets/wizard/wizard_transfer.scss b/assets/stylesheets/wizard/wizard_transfer.scss deleted file mode 100644 index 61c15b61..00000000 --- a/assets/stylesheets/wizard/wizard_transfer.scss +++ /dev/null @@ -1,19 +0,0 @@ -#export-button{ - margin-left: 30px; -} -#import-button{ - margin-left: 15px; -} -#file_url { - display: inline; -} - -.wizard-list-select { - display: inline-block; - -} - -.wizard-action-buttons{ - display: inline-flex; - flex-direction: column; -} diff --git a/assets/stylesheets/wizard_custom_admin.scss b/assets/stylesheets/wizard_custom_admin.scss index 3399b148..652b47df 100644 --- a/assets/stylesheets/wizard_custom_admin.scss +++ b/assets/stylesheets/wizard_custom_admin.scss @@ -224,11 +224,11 @@ .required-data .setting-value { flex-flow: wrap; - + .custom-inputs { margin-bottom: 20px; } - + .required-data-message .label { margin-bottom: 5px; } @@ -480,3 +480,22 @@ .wizard-step-contents{ height: unset !important; } + +// Tansfer tab +#file_url { + display: block; + margin-bottom: 10px; +} + +.wizard-list-select { + list-style-type: none; +} + +.wizard-action-buttons{ + flex-direction: column; +} + +.container{ + margin-bottom: 20px; +} + diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index fad2628c..b2364c7e 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -217,8 +217,15 @@ en: transfer: nav_label: "Transfer" - export: "Export" - import: "Import" + export: + label: "Export" + noneSelected: "Please select atleast one wizard" + import: + label: "Import" + success: "Wizard: {{id}} saved successfully" + failure: "Wizard: {{id}} could not be saved" + noFile: "Please choose a file to import" + fileSizeError: "The file size is too big" wizard_js: location: diff --git a/controllers/transfer.rb b/controllers/transfer.rb index 61da5c75..bcf95299 100644 --- a/controllers/transfer.rb +++ b/controllers/transfer.rb @@ -9,6 +9,7 @@ class CustomWizard::TransferController < ::ApplicationController def export wizards = params['wizards'] wizard_objects = [] + if wizards.nil? render json: {error: I18n.t('wizard.export.error.select_one')} return @@ -19,7 +20,6 @@ class CustomWizard::TransferController < ::ApplicationController end send_data wizard_objects.to_json, type: "application/json", disposition: 'attachment', filename: 'wizards.json' - end def is_json(string) @@ -32,12 +32,15 @@ class CustomWizard::TransferController < ::ApplicationController def import file = File.read(params['file'].tempfile) + if file.nil? render json: {error: I18n.t('wizard.import.error.no_file')} return end + fileSize = file.size maxFileSize = 512 * 1024 + if maxFileSize < fileSize render json: {error: I18n.t('wizard.import.error.file_large')} return @@ -49,7 +52,6 @@ class CustomWizard::TransferController < ::ApplicationController end jsonObject = JSON.parse file - countValid = 0 success_ids = [] failed_ids = [] @@ -71,6 +73,4 @@ class CustomWizard::TransferController < ::ApplicationController render json: {success: success_ids, failed: failed_ids} end end - - end diff --git a/plugin.rb b/plugin.rb index 20260e32..1acacf99 100644 --- a/plugin.rb +++ b/plugin.rb @@ -5,7 +5,6 @@ # url: https://github.com/angusmcleod/discourse-custom-wizard register_asset 'stylesheets/wizard_custom_admin.scss' -register_asset 'stylesheets/wizard/wizard_transfer.scss' register_asset 'lib/jquery.timepicker.min.js' register_asset 'lib/jquery.timepicker.scss'