Allow add, edit & delete in the admin UI
This commit is contained in:
parent
61caca3c5b
commit
8d70b4ad46
|
@ -2,16 +2,27 @@ import Rule from 'discourse/plugins/discourse-chat/admin/models/rule'
|
||||||
import { ajax } from 'discourse/lib/ajax';
|
import { ajax } from 'discourse/lib/ajax';
|
||||||
import computed from "ember-addons/ember-computed-decorators";
|
import computed from "ember-addons/ember-computed-decorators";
|
||||||
import showModal from 'discourse/lib/show-modal';
|
import showModal from 'discourse/lib/show-modal';
|
||||||
|
import { popupAjaxError } from 'discourse/lib/ajax-error';
|
||||||
|
|
||||||
|
|
||||||
export default Ember.Controller.extend({
|
export default Ember.Controller.extend({
|
||||||
modalShowing: false,
|
modalShowing: false,
|
||||||
|
|
||||||
actions:{
|
actions:{
|
||||||
|
create(provider){
|
||||||
|
this.set('modalShowing', true);
|
||||||
|
showModal('admin-plugins-chat-edit-rule', { model: this.store.createRecord('rule',{provider: provider}), admin: true });
|
||||||
|
},
|
||||||
edit(rule){
|
edit(rule){
|
||||||
this.set('modalShowing', true);
|
this.set('modalShowing', true);
|
||||||
showModal('admin-plugins-chat-edit-rule', { model: rule, admin: 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)
|
||||||
|
},
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,11 +18,6 @@ export default Ember.Controller.extend(ModalFunctionality, {
|
||||||
|
|
||||||
this.get('model').update().then(function(result) {
|
this.get('model').update().then(function(result) {
|
||||||
self.send('closeModal');
|
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) {
|
}).catch(function(error) {
|
||||||
self.flash(extractError(error), 'error');
|
self.flash(extractError(error), 'error');
|
||||||
});
|
});
|
||||||
|
|
|
@ -30,6 +30,12 @@ export default RestModel.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
updateProperties() {
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -4,7 +4,10 @@ import { ajax } from 'discourse/lib/ajax';
|
||||||
export default Discourse.Route.extend({
|
export default Discourse.Route.extend({
|
||||||
|
|
||||||
model(params, transition) {
|
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) {
|
serialize: function(model, params) {
|
||||||
|
@ -21,5 +24,9 @@ export default Discourse.Route.extend({
|
||||||
return true; // Continue bubbling up, so the modal actually closes
|
return true; // Continue bubbling up, so the modal actually closes
|
||||||
},
|
},
|
||||||
|
|
||||||
|
refresh: function(data){
|
||||||
|
this.refresh();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -3,6 +3,9 @@
|
||||||
|
|
||||||
<form class="form-horizontal">
|
<form class="form-horizontal">
|
||||||
<div>
|
<div>
|
||||||
|
Rule for provider {{model.provider}}
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
<label for="category">{{i18n "chat.category"}}</label>
|
<label for="category">{{i18n "chat.category"}}</label>
|
||||||
{{category-chooser
|
{{category-chooser
|
||||||
name="category"
|
name="category"
|
||||||
|
|
|
@ -17,7 +17,8 @@
|
||||||
<th></th>
|
<th></th>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
{{#each model as |rule|}}
|
{{#each model.rules as |rule|}}
|
||||||
|
|
||||||
<tr class="">
|
<tr class="">
|
||||||
<td>{{rule.channel}}</td>
|
<td>{{rule.channel}}</td>
|
||||||
<td>{{rule.filterName}}</td>
|
<td>{{rule.filterName}}</td>
|
||||||
|
@ -44,6 +45,7 @@
|
||||||
{{/each}}
|
{{/each}}
|
||||||
|
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
<hr/>
|
<hr/>
|
||||||
{{d-button action="testNotification"
|
{{d-button action="testNotification"
|
||||||
icon="rocket"
|
icon="rocket"
|
||||||
|
@ -57,3 +59,6 @@
|
||||||
title="chat.reset_settings"
|
title="chat.reset_settings"
|
||||||
label="chat.reset_settings"}}
|
label="chat.reset_settings"}}
|
||||||
{{/d-button}}
|
{{/d-button}}
|
||||||
|
<div class="pull-right">
|
||||||
|
{{d-button action="create" actionParam=model.provider icon="plus" title="chat.create_rule" label="chat.create_rule"}}
|
||||||
|
</div>
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
{{/each}}
|
{{/each}}
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
{{outlet}}
|
{{outlet}}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -50,6 +50,14 @@
|
||||||
from_hash hash
|
from_hash hash
|
||||||
end
|
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
|
def save
|
||||||
unless @id && @id > 0
|
unless @id && @id > 0
|
||||||
@id = self.class.alloc_id
|
@id = self.class.alloc_id
|
||||||
|
|
28
plugin.rb
28
plugin.rb
|
@ -71,8 +71,30 @@ after_initialize do
|
||||||
render_serialized rules, DiscourseChat::RuleSerializer, root: 'rules'
|
render_serialized rules, DiscourseChat::RuleSerializer, root: 'rules'
|
||||||
end
|
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
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -90,7 +112,9 @@ after_initialize do
|
||||||
get '/providers' => "chat#list_providers"
|
get '/providers' => "chat#list_providers"
|
||||||
|
|
||||||
get '/rules' => "chat#list_rules"
|
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"
|
get "/:provider" => "chat#respond"
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue