Allow add, edit & delete in the admin UI

This commit is contained in:
David Taylor 2017-06-29 20:19:40 +01:00
parent 61caca3c5b
commit 8d70b4ad46
9 changed files with 71 additions and 11 deletions

View File

@ -2,16 +2,27 @@ import Rule from 'discourse/plugins/discourse-chat/admin/models/rule'
import { ajax } from 'discourse/lib/ajax';
import computed from "ember-addons/ember-computed-decorators";
import showModal from 'discourse/lib/show-modal';
import { popupAjaxError } from 'discourse/lib/ajax-error';
export default Ember.Controller.extend({
modalShowing: false,
actions:{
create(provider){
this.set('modalShowing', true);
showModal('admin-plugins-chat-edit-rule', { model: this.store.createRecord('rule',{provider: provider}), admin: true });
},
edit(rule){
this.set('modalShowing', true);
showModal('admin-plugins-chat-edit-rule', { model: rule, admin: true });
},
delete(rule){
const self = this;
rule.destroyRecord().then(function() {
self.send('refresh');
}).catch(popupAjaxError)
},
}

View File

@ -18,11 +18,6 @@ export default Ember.Controller.extend(ModalFunctionality, {
this.get('model').update().then(function(result) {
self.send('closeModal');
if (result.responseJson.success) {
self.transitionToRoute('tags.show', result.responseJson.tag.id);
} else {
self.flash(extractError(result.responseJson.errors[0]), 'error');
}
}).catch(function(error) {
self.flash(extractError(error), 'error');
});

View File

@ -30,6 +30,12 @@ export default RestModel.extend({
},
updateProperties() {
return ['category_id','provider','channel', 'tags','filter'];
var prop_names = ['category_id','provider','channel', 'tags','filter'];
return this.getProperties(prop_names);
},
createProperties() {
return this.updateProperties();
}
});

View File

@ -4,7 +4,10 @@ import { ajax } from 'discourse/lib/ajax';
export default Discourse.Route.extend({
model(params, transition) {
return this.store.find('rule', {provider: params.provider});
return Ember.RSVP.hash({
rules: this.store.find('rule', {provider: params.provider}),
provider: params.provider
});
},
serialize: function(model, params) {
@ -21,5 +24,9 @@ export default Discourse.Route.extend({
return true; // Continue bubbling up, so the modal actually closes
},
refresh: function(data){
this.refresh();
}
}
});

View File

@ -3,6 +3,9 @@
<form class="form-horizontal">
<div>
Rule for provider {{model.provider}}
</div>
<div>
<label for="category">{{i18n "chat.category"}}</label>
{{category-chooser
name="category"

View File

@ -17,7 +17,8 @@
<th></th>
</tr>
{{#each model as |rule|}}
{{#each model.rules as |rule|}}
<tr class="">
<td>{{rule.channel}}</td>
<td>{{rule.filterName}}</td>
@ -44,6 +45,7 @@
{{/each}}
</table>
<hr/>
{{d-button action="testNotification"
icon="rocket"
@ -57,3 +59,6 @@
title="chat.reset_settings"
label="chat.reset_settings"}}
{{/d-button}}
<div class="pull-right">
{{d-button action="create" actionParam=model.provider icon="plus" title="chat.create_rule" label="chat.create_rule"}}
</div>

View File

@ -7,6 +7,7 @@
{{/each}}
</ul>
</div>
</div>
{{outlet}}
</div>

View File

@ -50,6 +50,14 @@
from_hash hash
end
def update(h)
[:provider, :channel, :category_id, :tags, :filter].each do |sym|
public_send("#{sym}=", h[sym]) if h[sym]
end
save
end
def save
unless @id && @id > 0
@id = self.class.alloc_id

View File

@ -71,8 +71,30 @@ after_initialize do
render_serialized rules, DiscourseChat::RuleSerializer, root: 'rules'
end
def create_rule
rule = DiscourseChat::Rule.new()
hash = params.require(:rule)
rule.update(hash)
render_serialized rule, DiscourseChat::RuleSerializer, root: 'rule'
end
def update_rule
render json: {success: false}
rule = DiscourseChat::Rule.find(params[:id].to_i)
hash = params.require(:rule)
rule.update(hash)
render_serialized rule, DiscourseChat::RuleSerializer, root: 'rule'
end
def destroy_rule
rule = DiscourseChat::Rule.find(params[:id].to_i)
rule.destroy
render json: success_json
end
end
@ -90,7 +112,9 @@ after_initialize do
get '/providers' => "chat#list_providers"
get '/rules' => "chat#list_rules"
put 'rules/:id' => "chat#update_rule"
put '/rules' => "chat#create_rule"
put '/rules/:id' => "chat#update_rule"
delete '/rules/:id' => "chat#destroy_rule"
get "/:provider" => "chat#respond"
end