diff --git a/assets/javascripts/discourse-staff-notes/templates/connectors/user-profile-controls/show-notes-on-profile.hbs b/assets/javascripts/discourse-staff-notes/templates/connectors/user-profile-controls/show-notes-on-profile.hbs new file mode 100644 index 0000000..a40247d --- /dev/null +++ b/assets/javascripts/discourse-staff-notes/templates/connectors/user-profile-controls/show-notes-on-profile.hbs @@ -0,0 +1,7 @@ +{{#if currentUser.staff}} + {{#if staffNotesCount}} +
  • {{fa-icon "pencil"}}{{i18n 'staff_notes.show' count=staffNotesCount}}
  • + {{else}} +
  • {{fa-icon "pencil"}}{{i18n 'staff_notes.title'}}
  • + {{/if}} +{{/if}} diff --git a/assets/javascripts/discourse/controllers/staff-notes.js.es6 b/assets/javascripts/discourse/controllers/staff-notes.js.es6 index 783c9a7..8b2bdb1 100644 --- a/assets/javascripts/discourse/controllers/staff-notes.js.es6 +++ b/assets/javascripts/discourse/controllers/staff-notes.js.es6 @@ -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(); }); } } diff --git a/assets/javascripts/discourse/initializers/enable-staff-notes.js.es6 b/assets/javascripts/discourse/initializers/enable-staff-notes.js.es6 index c0e2c34..b44ee7d 100644 --- a/assets/javascripts/discourse/initializers/enable-staff-notes.js.es6 +++ b/assets/javascripts/discourse/initializers/enable-staff-notes.js.es6 @@ -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' }); diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 4b82b04..01b4df3 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -4,3 +4,4 @@ en: title: "Staff Notes" attach: "Attach Staff Note" remove: "Remove Note" + show: "Staff Notes ({{count}})" diff --git a/plugin.rb b/plugin.rb index 3da1858..e527189 100644 --- a/plugin.rb +++ b/plugin.rb @@ -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'