diff --git a/.travis.yml b/.travis.yml index 01ff46d8..b48ce894 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ # Uncomment tests runner when tests are added. sudo: required -#services: +#names: #- docker before_install: diff --git a/assets/javascripts/discourse/controllers/admin-wizards-api.js.es6 b/assets/javascripts/discourse/controllers/admin-wizards-api.js.es6 index ec4b5170..832bf1c4 100644 --- a/assets/javascripts/discourse/controllers/admin-wizards-api.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-wizards-api.js.es6 @@ -1,13 +1,32 @@ import { ajax } from 'discourse/lib/ajax'; import { popupAjaxError } from 'discourse/lib/ajax-error'; import CustomWizardApi from '../models/custom-wizard-api'; +import { default as computed, observes } from 'ember-addons/ember-computed-decorators'; +import DiscourseURL from 'discourse/lib/url'; export default Ember.Controller.extend({ + queryParams: ['refresh_list'], loadingSubscriptions: false, notAuthorized: Ember.computed.not('api.authorized'), authorizationTypes: ['oauth', 'basic'], isOauth: Ember.computed.equal('api.authType', 'oauth'), endpointMethods: ['GET', 'PUT', 'POST', 'PATCH', 'DELETE'], + saveDisabled: Ember.computed.empty('api.name'), + showRemove: Ember.computed.not('isNew'), + + @computed('saveDisabled', 'authType', 'authUrl') + authDisabled(saveDisabled, authType, authUrl) { + return saveDisabled || !authType || !authUrl; + }, + + @observes('api.title') + titleWatcher() { + const title = this.get('api.title'); + + if (this.get('originalTitle')) { + this.set('originalTitle', title); + } + }, actions: { addParam() { @@ -48,12 +67,25 @@ export default Ember.Controller.extend({ save() { const api = this.get('api'); - const service = api.service; + const name = api.name; + let refreshList = false; - let data = {}; + if (!name ) return; - data['auth_type'] = api.authType; - data['auth_url'] = api.authUrl; + let data = { + title: api.title + }; + + if (this.get('isNew') || (api.title !== this.get('originalTitle'))) { + refreshList = true; + } + + if (api.get('isNew')) { + data['new'] = true; + }; + + if (api.authType) data['auth_type'] = api.authType; + if (api.authUrl) data['auth_url'] = api.authUrl; if (data.auth_type === 'oauth') { data['client_id'] = api.clientId; @@ -72,23 +104,43 @@ export default Ember.Controller.extend({ } const endpoints = api.endpoints; - if (endpoints != undefined) { - if (endpoints.length) { - data['endpoints'] = JSON.stringify(endpoints); - } + if (endpoints.length) { + data['endpoints'] = JSON.stringify(endpoints); } - this.set('savingApi', true); + this.set('updating', true); - ajax(`/admin/wizards/apis/${service}`, { + ajax(`/admin/wizards/apis/${name.underscore()}`, { type: 'PUT', data }).catch(popupAjaxError) .then(result => { if (result.success) { - this.set('api', CustomWizardApi.create(result.api)); + if (refreshList) { + this.transitionToRoute('adminWizardsApi', result.api.name.dasherize()).then(() => { + this.send('refreshModel'); + }); + } else { + this.set('api', CustomWizardApi.create(result.api)); + } } - }).finally(() => this.set('savingApi', false)); + }).finally(() => this.set('updating', false)); + }, + + remove() { + const name = this.get('api.name'); + if (!name) return; + + this.set('updating', true); + + ajax(`/admin/wizards/apis/${name.underscore()}`, { + type: 'DELETE' + }).catch(popupAjaxError) + .then(result => { + if (result.success) { + DiscourseURL.routeTo('/admin/wizards/apis?refresh=true'); + } + }).finally(() => this.set('updating', false)); } } }); diff --git a/assets/javascripts/discourse/controllers/admin-wizards-apis.js.es6 b/assets/javascripts/discourse/controllers/admin-wizards-apis.js.es6 new file mode 100644 index 00000000..52748bc8 --- /dev/null +++ b/assets/javascripts/discourse/controllers/admin-wizards-apis.js.es6 @@ -0,0 +1,3 @@ +export default Ember.Controller.extend({ + queryParams: ['refresh'] +}); diff --git a/assets/javascripts/discourse/custom-wizard-admin-route-map.js.es6 b/assets/javascripts/discourse/custom-wizard-admin-route-map.js.es6 index dfe53a82..f5022153 100644 --- a/assets/javascripts/discourse/custom-wizard-admin-route-map.js.es6 +++ b/assets/javascripts/discourse/custom-wizard-admin-route-map.js.es6 @@ -9,7 +9,7 @@ export default { this.route('adminWizardSubmissions', { path: '/:wizard_id', resetNamespace: true }); }); this.route('adminWizardsApis', { path: '/apis', resetNamespace: true }, function() { - this.route('adminWizardsApi', { path: '/:service', resetNamespace: true }); + this.route('adminWizardsApi', { path: '/:name', resetNamespace: true }); }); }); } diff --git a/assets/javascripts/discourse/models/custom-wizard-api.js.es6 b/assets/javascripts/discourse/models/custom-wizard-api.js.es6 index 6817ae70..5f051cc9 100644 --- a/assets/javascripts/discourse/models/custom-wizard-api.js.es6 +++ b/assets/javascripts/discourse/models/custom-wizard-api.js.es6 @@ -2,10 +2,11 @@ import { ajax } from 'discourse/lib/ajax'; import { default as computed } from 'ember-addons/ember-computed-decorators'; const CustomWizardApi = Discourse.Model.extend({ - @computed('service') - redirectUri(service) { + @computed('name') + redirectUri(name) { + let nameParam = name.toString().dasherize(); const baseUrl = location.protocol+'//'+location.hostname+(location.port ? ':'+location.port: ''); - return baseUrl + `/admin/wizards/apis/${service}/redirect`; + return baseUrl + `/admin/wizards/apis/${nameParam}/redirect`; } }); @@ -16,7 +17,8 @@ CustomWizardApi.reopenClass({ const endpoints = params.endpoints; api.setProperties({ - service: params.service, + name: params.name, + title: params.title, authType: authorization.auth_type, authUrl: authorization.auth_url, tokenUrl: authorization.token_url, @@ -29,14 +31,15 @@ CustomWizardApi.reopenClass({ code: authorization.code, tokenExpiresAt: authorization.token_expires_at, tokenRefreshAt: authorization.token_refresh_at, - endpoints: Ember.A(endpoints) + endpoints: Ember.A(endpoints), + isNew: params.isNew }); return api; }, - find(service) { - return ajax(`/admin/wizards/apis/${service}`, { + find(name) { + return ajax(`/admin/wizards/apis/${name}`, { type: 'GET' }).then(result => { return CustomWizardApi.create(result); diff --git a/assets/javascripts/discourse/routes/admin-wizards-api.js.es6 b/assets/javascripts/discourse/routes/admin-wizards-api.js.es6 index dda7d416..58f624b5 100644 --- a/assets/javascripts/discourse/routes/admin-wizards-api.js.es6 +++ b/assets/javascripts/discourse/routes/admin-wizards-api.js.es6 @@ -1,11 +1,17 @@ import CustomWizardApi from '../models/custom-wizard-api'; export default Discourse.Route.extend({ + queryParams: { + refresh_list: { + refreshModel: true + } + }, + model(params) { - if (params.service === 'new') { - return CustomWizardApi.create(); + if (params.name === 'new') { + return CustomWizardApi.create({ isNew: true }); } else { - return CustomWizardApi.find(params.service); + return CustomWizardApi.find(params.name); } }, diff --git a/assets/javascripts/discourse/routes/admin-wizards-apis.js.es6 b/assets/javascripts/discourse/routes/admin-wizards-apis.js.es6 index b7e89e10..54174c6b 100644 --- a/assets/javascripts/discourse/routes/admin-wizards-apis.js.es6 +++ b/assets/javascripts/discourse/routes/admin-wizards-apis.js.es6 @@ -5,7 +5,27 @@ export default Discourse.Route.extend({ return CustomWizardApi.list(); }, + afterModel(model) { + const apiParams = this.paramsFor('admin-wizards-api'); + + if (model.length) { + if (!apiParams.name) { + this.transitionTo('adminWizardsApi', model[0].name.dasherize()); + } else { + return; + } + } else { + this.transitionTo('adminWizardsApi', 'new'); + } + }, + setupController(controller, model){ controller.set("model", model); + }, + + actions: { + refreshModel() { + this.refresh(); + } } }); diff --git a/assets/javascripts/discourse/templates/admin-wizards-api.hbs b/assets/javascripts/discourse/templates/admin-wizards-api.hbs index f91a4c3f..a17a0c0b 100644 --- a/assets/javascripts/discourse/templates/admin-wizards-api.hbs +++ b/assets/javascripts/discourse/templates/admin-wizards-api.hbs @@ -1,51 +1,77 @@ -