Refresh the icon in the post stream when added/removed

This commit is contained in:
Robin Ward 2016-03-11 14:09:25 -05:00
parent 9e040cabc9
commit 840be343e9
2 changed files with 29 additions and 4 deletions

View File

@ -1,7 +1,7 @@
import { default as computed, on } from 'ember-addons/ember-computed-decorators'; import { default as computed, on } from 'ember-addons/ember-computed-decorators';
import { popupAjaxError } from 'discourse/lib/ajax-error'; import { popupAjaxError } from 'discourse/lib/ajax-error';
export default Ember.Controller.extend({ const StaffNotesController = Ember.Controller.extend({
newNote: null, newNote: null,
saving: false, saving: false,
@ -18,15 +18,30 @@ export default Ember.Controller.extend({
actions: { actions: {
attachNote() { attachNote() {
const note = this.store.createRecord('staff-note'); const note = this.store.createRecord('staff-note');
const userId = parseInt(this.get('userId'));
this.set('saving', true); 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.set('newNote', '');
this.get('model').pushObject(note); 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)); }).catch(popupAjaxError).finally(() => this.set('saving', false));
}, },
removeNote(note) { 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;

View File

@ -17,9 +17,19 @@ export default {
}); });
} }
const StaffNotesController = container.lookupFactory('controller:staff-notes');
const noteCount = StaffNotesController.noteCount;
api.decorateWidget('poster-name:after', dec => { api.decorateWidget('poster-name:after', dec => {
const cfs = dec.attrs.userCustomFields || {}; 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'); return dec.attach('staff-notes-icon');
} }
}); });