diff --git a/assets/javascripts/discourse/controllers/staff-notes.js.es6 b/assets/javascripts/discourse/controllers/staff-notes.js.es6 index 01d1cc3..783c9a7 100644 --- a/assets/javascripts/discourse/controllers/staff-notes.js.es6 +++ b/assets/javascripts/discourse/controllers/staff-notes.js.es6 @@ -1,7 +1,7 @@ import { default as computed, on } from 'ember-addons/ember-computed-decorators'; import { popupAjaxError } from 'discourse/lib/ajax-error'; -export default Ember.Controller.extend({ +const StaffNotesController = Ember.Controller.extend({ newNote: null, saving: false, @@ -18,15 +18,30 @@ export default Ember.Controller.extend({ actions: { attachNote() { const note = this.store.createRecord('staff-note'); + const userId = parseInt(this.get('userId')); + this.set('saving', true); - note.save({ raw: this.get('newNote'), user_id: this.get('userId') }).then(() => { + 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 }); }).catch(popupAjaxError).finally(() => this.set('saving', false)); }, removeNote(note) { - note.destroyRecord().then(() => this.get('model').removeObject(note)); + 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 }); + }); } } }); + +StaffNotesController.reopenClass({ + noteCount: {} +}); + +export default StaffNotesController; diff --git a/assets/javascripts/discourse/initializers/enable-staff-notes.js.es6 b/assets/javascripts/discourse/initializers/enable-staff-notes.js.es6 index b7a7c7c..c0e2c34 100644 --- a/assets/javascripts/discourse/initializers/enable-staff-notes.js.es6 +++ b/assets/javascripts/discourse/initializers/enable-staff-notes.js.es6 @@ -17,9 +17,19 @@ export default { }); } + const StaffNotesController = container.lookupFactory('controller:staff-notes'); + const noteCount = StaffNotesController.noteCount; + api.decorateWidget('poster-name:after', dec => { const cfs = dec.attrs.userCustomFields || {}; - if (cfs.has_staff_notes) { + + // 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) { return dec.attach('staff-notes-icon'); } });