diff --git a/assets/javascripts/discourse/connectors/admin-menu/wizards-nav-button.hbs b/assets/javascripts/discourse/connectors/admin-menu/wizards-nav-button.hbs index 5424f713..5398e27d 100644 --- a/assets/javascripts/discourse/connectors/admin-menu/wizards-nav-button.hbs +++ b/assets/javascripts/discourse/connectors/admin-menu/wizards-nav-button.hbs @@ -1,3 +1,3 @@ {{#if currentUser.admin}} - {{nav-item route='adminWizards' label='admin.wizard.nav'}} + {{nav-item route='adminWizards' label='admin.wizard.nav_label'}} {{/if}} diff --git a/assets/javascripts/discourse/controllers/admin-wizard-submissions.js.es6 b/assets/javascripts/discourse/controllers/admin-wizard-submissions.js.es6 new file mode 100644 index 00000000..57b0ab57 --- /dev/null +++ b/assets/javascripts/discourse/controllers/admin-wizard-submissions.js.es6 @@ -0,0 +1,6 @@ +import Controller from "@ember/controller"; +import { fmt } from "discourse/lib/computed"; + +export default Controller.extend({ + downloadUrl: fmt("wizard.id", "/admin/wizards/submissions/%@/download") +}); \ No newline at end of file diff --git a/assets/javascripts/discourse/models/custom-wizard.js.es6 b/assets/javascripts/discourse/models/custom-wizard.js.es6 index 38768eb6..2e1decb4 100644 --- a/assets/javascripts/discourse/models/custom-wizard.js.es6 +++ b/assets/javascripts/discourse/models/custom-wizard.js.es6 @@ -198,8 +198,6 @@ CustomWizard.reopenClass({ submissions(wizardId) { return ajax(`/admin/wizards/submissions/${wizardId}`, { type: "GET" - }).then(result => { - return result.submissions; }); }, diff --git a/assets/javascripts/discourse/routes/admin-wizard-submissions.js.es6 b/assets/javascripts/discourse/routes/admin-wizard-submissions.js.es6 index 95d72ec3..b1f6b226 100644 --- a/assets/javascripts/discourse/routes/admin-wizard-submissions.js.es6 +++ b/assets/javascripts/discourse/routes/admin-wizard-submissions.js.es6 @@ -2,29 +2,54 @@ import CustomWizard from '../models/custom-wizard'; import DiscourseRoute from "discourse/routes/discourse"; export default DiscourseRoute.extend({ + beforeModel() { + const param = this.paramsFor('adminWizardSubmissions').wizard_id; + const wizards = this.modelFor('admin-wizards-submissions'); + + if (wizards.length && (param === 'first')) { + const wizard = wizards.get(`${param}Object`); + if (wizard) { + this.transitionTo('adminWizardSubmissions', wizard.id.dasherize()); + } + } + }, + model(params) { - return CustomWizard.submissions(params.wizard_id); + const wizardId = params.wizard_id; + if (wizardId && wizardId !== 'new') { + return CustomWizard.submissions(params.wizard_id); + } else { + return {}; + } }, setupController(controller, model) { - let fields = []; - model.forEach((s) => { - Object.keys(s).forEach((k) => { - if (fields.indexOf(k) < 0) { - fields.push(k); - } + if (model.submissions) { + let fields = []; + model.submissions.forEach((s) => { + Object.keys(s).forEach((k) => { + if (fields.indexOf(k) < 0) { + fields.push(k); + } + }); }); - }); - let submissions = []; - model.forEach((s) => { - let submission = {}; - fields.forEach((f) => { - submission[f] = s[f]; + let submissions = []; + model.submissions.forEach((s) => { + let submission = {}; + fields.forEach((f) => { + submission[f] = s[f]; + }); + submissions.push(submission); }); - submissions.push(submission); - }); + + console.log(model.id) - controller.setProperties({ submissions, fields }); + controller.setProperties({ + wizard: model.wizard, + submissions, + fields + }); + } } }); diff --git a/assets/javascripts/discourse/routes/admin-wizards-custom-index.js.es6 b/assets/javascripts/discourse/routes/admin-wizards-submissions-index.js.es6 similarity index 67% rename from assets/javascripts/discourse/routes/admin-wizards-custom-index.js.es6 rename to assets/javascripts/discourse/routes/admin-wizards-submissions-index.js.es6 index e01f87c6..6bfca635 100644 --- a/assets/javascripts/discourse/routes/admin-wizards-custom-index.js.es6 +++ b/assets/javascripts/discourse/routes/admin-wizards-submissions-index.js.es6 @@ -2,6 +2,6 @@ import DiscourseRoute from "discourse/routes/discourse"; export default DiscourseRoute.extend({ redirect() { - this.transitionTo('adminWizard', 'first'); + this.transitionTo('adminWizardSubmissions', 'first'); } }); diff --git a/assets/javascripts/discourse/templates/admin-wizard-submissions.hbs b/assets/javascripts/discourse/templates/admin-wizard-submissions.hbs index ec8a55ff..1265db8a 100644 --- a/assets/javascripts/discourse/templates/admin-wizard-submissions.hbs +++ b/assets/javascripts/discourse/templates/admin-wizard-submissions.hbs @@ -1,3 +1,14 @@ +
+ + + + {{d-icon 'download'}} + + {{i18n "admin.wizard.submissions.download"}} + + +
+
diff --git a/assets/javascripts/discourse/templates/admin-wizard.hbs b/assets/javascripts/discourse/templates/admin-wizard.hbs index ce813f66..c89eac36 100644 --- a/assets/javascripts/discourse/templates/admin-wizard.hbs +++ b/assets/javascripts/discourse/templates/admin-wizard.hbs @@ -27,7 +27,7 @@ name="background" value=model.background placeholderKey="admin.wizard.background_placeholder" - class="medium"}} + class="small"}} diff --git a/assets/javascripts/discourse/templates/admin-wizards-submissions.hbs b/assets/javascripts/discourse/templates/admin-wizards-submissions.hbs index f0047e88..1637b6bf 100644 --- a/assets/javascripts/discourse/templates/admin-wizards-submissions.hbs +++ b/assets/javascripts/discourse/templates/admin-wizards-submissions.hbs @@ -9,7 +9,7 @@ -
+
{{outlet}}
diff --git a/assets/javascripts/discourse/templates/admin-wizards.hbs b/assets/javascripts/discourse/templates/admin-wizards.hbs index ffe33a6f..578e4380 100644 --- a/assets/javascripts/discourse/templates/admin-wizards.hbs +++ b/assets/javascripts/discourse/templates/admin-wizards.hbs @@ -1,12 +1,12 @@ {{#admin-nav}} - {{nav-item route='adminWizardsCustom' label='admin.wizard.custom_label'}} - {{nav-item route='adminWizardsSubmissions' label='admin.wizard.submissions_label'}} + {{nav-item route='adminWizardsCustom' label='admin.wizard.nav_label'}} + {{nav-item route='adminWizardsSubmissions' label='admin.wizard.submissions.nav_label'}} {{#if siteSettings.wizard_api_features}} {{nav-item route='adminWizardsApis' label='admin.wizard.api.nav_label'}} {{/if}} {{nav-item route='adminWizardsTransfer' label='admin.wizard.transfer.nav_label'}} {{/admin-nav}} -
+
{{outlet}}
diff --git a/assets/stylesheets/common/wizard-admin.scss b/assets/stylesheets/common/wizard-admin.scss index 75cddd97..9794fba7 100644 --- a/assets/stylesheets/common/wizard-admin.scss +++ b/assets/stylesheets/common/wizard-admin.scss @@ -2,14 +2,26 @@ @import 'wizard-transfer'; @import 'wizard-api'; -body.admin-wizard { - .admin-container > .row { +.admin-wizard-container { + .row { display: flex; + + > .content { + flex: 1; + margin-top: 10px; + margin-left: 30px; + + table { + margin-top: 0; + min-width: 100%; + width: auto; + } + } } - - .boxed.white { - background-color: initial; - } +} + +.wizard-submissions { + overflow: scroll; } .wizard-list { @@ -97,11 +109,21 @@ body.admin-wizard { min-height: 31px; margin-bottom: 30px; display: flex; + justify-content: space-between; input { margin-bottom: 0; width: 350px; } + + label { + margin-bottom: 0; + } + + .download-link { + font-size: 1rem; + line-height: 20px; + } } &.medium { @@ -138,8 +160,6 @@ body.admin-wizard { } .admin-wizard.settings { - margin-top: 10px; - margin-left: 30px; [class~='setting'] { display: inline-flex; @@ -410,16 +430,6 @@ body.admin-wizard { } } -.admin-contents .wizard-submissions { - width: 100%; - margin-top: 10px; - margin-left: 30px; - - table { - margin-top: 0; - } -} - .wizard-field-composer textarea { width: 100%; min-height: 150px; diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 1b62de49..4214e5a7 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -7,12 +7,10 @@ en: admin: wizard: label: "Wizard" - nav: "Wizards" + nav_label: "Wizards" new: "New" - custom_label: "Custom" - submissions_label: "Submissions" name: "Name" - name_placeholder: "name" + name_placeholder: "wizard name" background: "Background" background_placeholder: "#hex" save_submissions: "Save" @@ -197,6 +195,11 @@ en: select_an_endpoint: "Select an endpoint" body: "Body" body_placeholder: "JSON" + + submissions: + nav_label: "Submissions" + title: "{{name}} Submissions" + download: "Download" api: label: "API" diff --git a/config/routes.rb b/config/routes.rb index 764c4e42..b586e55b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -17,10 +17,12 @@ Discourse::Application.routes.append do get 'admin/wizards/custom/new' => 'admin#index' get 'admin/wizards/custom/all' => 'admin#custom_wizards' get 'admin/wizards/custom/:wizard_id' => 'admin#find_wizard' + get 'admin/wizards/custom/:wizard_id' => 'admin#find_wizard' put 'admin/wizards/custom/save' => 'admin#save' delete 'admin/wizards/custom/remove' => 'admin#remove' get 'admin/wizards/submissions' => 'admin#index' get 'admin/wizards/submissions/:wizard_id' => 'admin#submissions' + get 'admin/wizards/submissions/:wizard_id/download' => 'admin#download_submissions' get 'admin/wizards/apis' => 'api#list' get 'admin/wizards/apis/new' => 'api#index' get 'admin/wizards/apis/:name' => 'api#find' diff --git a/controllers/custom_wizard/admin.rb b/controllers/custom_wizard/admin.rb index 5cbdf737..01df5167 100644 --- a/controllers/custom_wizard/admin.rb +++ b/controllers/custom_wizard/admin.rb @@ -1,5 +1,5 @@ -class CustomWizard::AdminController < ::ApplicationController - before_action :ensure_logged_in +class CustomWizard::AdminController < ::Admin::AdminController + skip_before_action :check_xhr, only: [:download_submissions] before_action :ensure_admin def index @@ -55,38 +55,42 @@ class CustomWizard::AdminController < ::ApplicationController def find_wizard params.require(:wizard_id) - wizard = PluginStore.get('custom_wizard', params[:wizard_id].underscore) - render json: success_json.merge(wizard: wizard) end def custom_wizards rows = PluginStoreRow.where(plugin_name: 'custom_wizard').order(:id) - wizards = [*rows].map { |r| CustomWizard::Template.new(r.value) } - render json: success_json.merge(wizards: wizards) end def submissions params.require(:wizard_id) - - rows = PluginStoreRow.where(plugin_name: "#{params[:wizard_id]}_submissions").order('id DESC') - - all_submissions = [*rows].map do |r| - submissions = ::JSON.parse(r.value) - - if user = User.find_by(id: r.key) - username = user.username - else - username = I18n.t('admin.wizard.submissions.no_user', id: r.key) - end - - submissions.map { |s| { username: username }.merge!(s.except("redirect_to")) } - end.flatten - - render json: success_json.merge(submissions: all_submissions) + + wizard_id = params[:wizard_id].underscore + wizard = PluginStore.get('custom_wizard', wizard_id) + + if wizard.present? + render json: success_json.merge( + submissions: build_submissions(wizard_id), + wizard: wizard.slice(:id, :name) + ) + else + head :ok + end + end + + def download_submissions + params.require(:wizard_id) + wizard_id = params[:wizard_id].underscore + + wizard = PluginStore.get('custom_wizard', wizard_id) + submissions = build_submissions(wizard_id).to_json + + send_data submissions, + filename: "#{Discourse.current_hostname}-wizard-submissions-#{wizard['name']}.json", + content_type: "application/json" end private @@ -244,4 +248,24 @@ class CustomWizard::AdminController < ::ApplicationController result end + + def build_submissions(wizard_id) + rows = PluginStoreRow.where(plugin_name: "#{wizard_id}_submissions").order('id DESC') + + submissions = [*rows].map do |row| + value = ::JSON.parse(row.value) + + if user = User.find_by(id: row.key) + username = user.username + else + username = I18n.t('admin.wizard.submissions.no_user', id: row.key) + end + + value.map do |submission| + { + username: username + }.merge!(submission.except("redirect_to")) + end + end.flatten + end end