Working fairly well now

This commit is contained in:
Robin Ward 2016-03-11 15:24:30 -05:00
parent 840be343e9
commit 69661ae6b1
5 changed files with 65 additions and 21 deletions

View File

@ -0,0 +1,7 @@
{{#if currentUser.staff}}
{{#if staffNotesCount}}
<li><a {{action "showStaffNotes"}} href class="btn">{{fa-icon "pencil"}}{{i18n 'staff_notes.show' count=staffNotesCount}}</a></li>
{{else}}
<li><a {{action "showStaffNotes"}} href class="btn">{{fa-icon "pencil"}}{{i18n 'staff_notes.title'}}</a></li>
{{/if}}
{{/if}}

View File

@ -4,10 +4,11 @@ import { popupAjaxError } from 'discourse/lib/ajax-error';
const StaffNotesController = Ember.Controller.extend({
newNote: null,
saving: false,
user: null,
@on('init')
reset() {
this.setProperties({ newNote: null, saving: false });
this.setProperties({ newNote: null, saving: false, callback: null });
},
@computed('newNote', 'saving')
@ -15,26 +16,35 @@ const StaffNotesController = Ember.Controller.extend({
return saving || !newNote || (newNote.length === 0);
},
_refreshCount() {
const callback = this.get('callback');
if (callback) {
callback(this.get('model.length'));
}
},
actions: {
attachNote() {
const note = this.store.createRecord('staff-note');
const userId = parseInt(this.get('userId'));
const noteCount = StaffNotesController.noteCount;
this.set('saving', true);
note.save({ raw: this.get('newNote'), user_id: userId }).then(() => {
this.set('newNote', '');
this.get('model').pushObject(note);
StaffNotesController.noteCount[userId] = (StaffNotesController.noteCount[userId] || 0) + 1;
this.appEvents.trigger('post-stream:refresh', { force: true });
noteCount[userId] = this.get('model.length');
this._refreshCount();
}).catch(popupAjaxError).finally(() => this.set('saving', false));
},
removeNote(note) {
note.destroyRecord().then(() => {
note.destroyRecord().then(() => {
const notes = this.get('model');
notes.removeObject(note);
StaffNotesController.noteCount[parseInt(note.get('user_id'))] = notes.get('length');
this.appEvents.trigger('post-stream:refresh', { force: true });
this._refreshCount();
});
}
}

View File

@ -7,16 +7,42 @@ export default {
const siteSettings = container.lookup('site-settings:main');
if (!siteSettings.staff_notes_enabled) { return; }
const store = container.lookup('store:main');
withPluginApi('0.2', api => {
function showStaffNotes() {
const userId = this.attrs.user_id;
return this.store.find('staff-note', { user_id: userId }).then(model => {
function showStaffNotes(userId, callback) {
return store.find('staff-note', { user_id: userId }).then(model => {
const controller = showModal('staff-notes', { model, title: 'staff_notes.title' });
controller.reset();
controller.set('userId', userId);
controller.set('callback', callback);
return controller;
});
}
function widgetShowStaffNotes() {
showStaffNotes(this.attrs.user_id, count => {
this.scheduleRerender();
});
}
const UserController = container.lookupFactory('controller:user');
UserController.reopen({
staffNotesCount: null,
_modelChanged: function() {
this.set('staffNotesCount', this.get('model.custom_fields.staff_notes_count') || 0);
}.observes('model').on('init'),
actions: {
showStaffNotes() {
const user = this.get('model');
showStaffNotes(user.get('id'), count => this.set('staffNotesCount', count));
}
}
});
const StaffNotesController = container.lookupFactory('controller:staff-notes');
const noteCount = StaffNotesController.noteCount;
@ -24,12 +50,12 @@ export default {
const cfs = dec.attrs.userCustomFields || {};
// If we know the count, use it
const c = noteCount[dec.attrs.user_id];
if (c !== undefined) {
if (c > 0) {
return dec.attach('staff-notes-icon');
}
} else if (cfs.has_staff_notes) {
let c = noteCount[dec.attrs.user_id];
if (c === undefined && cfs.staff_notes_count) {
c = cfs.staff_notes_count;
}
if (c > 0) {
return dec.attach('staff-notes-icon');
}
});
@ -42,11 +68,11 @@ export default {
});
});
api.attachWidgetAction('post-admin-menu', 'showStaffNotes', showStaffNotes);
api.attachWidgetAction('post-admin-menu', 'showStaffNotes', widgetShowStaffNotes);
api.createWidget('staff-notes-icon', {
tagName: 'span.staff-notes-icon',
click: showStaffNotes,
click: widgetShowStaffNotes,
html() {
return this.attach('emoji', { name: 'pencil' });

View File

@ -4,3 +4,4 @@ en:
title: "Staff Notes"
attach: "Attach Staff Note"
remove: "Remove Note"
show: "Staff Notes ({{count}})"

View File

@ -7,7 +7,7 @@ enabled_site_setting :staff_notes_enabled
register_asset 'stylesheets/staff_notes.scss'
STAFF_NOTES_FIELD = "has_staff_notes"
STAFF_NOTE_COUNT_FIELD = "staff_notes_count"
after_initialize do
@ -33,7 +33,7 @@ after_initialize do
notes << record
::PluginStore.set("staff_notes", key_for(user.id), notes)
user.custom_fields[STAFF_NOTES_FIELD] = true
user.custom_fields[STAFF_NOTE_COUNT_FIELD] = notes.size
user.save_custom_fields
record
@ -47,9 +47,9 @@ after_initialize do
::PluginStore.set("staff_notes", key_for(user.id), notes)
else
::PluginStore.remove("staff_notes", key_for(user.id))
user.custom_fields.delete(STAFF_NOTES_FIELD)
user.save_custom_fields
end
user.custom_fields[STAFF_NOTE_COUNT_FIELD] = notes.size
user.save_custom_fields
end
end
@ -116,7 +116,7 @@ after_initialize do
end
whitelist_staff_user_custom_field(STAFF_NOTES_FIELD)
whitelist_staff_user_custom_field(STAFF_NOTE_COUNT_FIELD)
DiscourseStaffNotes::Engine.routes.draw do
get '/' => 'staff_notes#index'