Skip to content

Instantly share code, notes, and snippets.

@agnanachandran
Last active February 15, 2018 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agnanachandran/3f0f453d2aa258c26c60e9f9d6500a26 to your computer and use it in GitHub Desktop.
Save agnanachandran/3f0f453d2aa258c26c60e9f9d6500a26 to your computer and use it in GitHub Desktop.
stores/notifications_store.ts
class NotificationsStore {
@observable notificationsById: mobx.ObservableMap<Notification>;
static initialize(notifications: Notification[]): NotificationsStore {
const notificationsById = mobx.asMap(_.keyBy(notifications, 'id'));
return new NotificationsStore(notificationsById);
}
@computed get unreadNotifications(): Notification[] {
return _(this.notificationsById.values())
.reject(notification => notification.read)
.sortBy('time')
.reverse()
.value();
}
@computed get readNotifications(): Notification[] {
return _(this.notificationsById.values())
.filter(notification => notification.read)
.sortBy('time')
.reverse()
.value();
}
@action markNotificationAsRead = (notification: Notification): Promise<void> => {
return $.ajax(`/notifications/${notification.id}`, {
method: 'PATCH',
data: {
read: true,
},
}).done(() => {
notification.read = true;
});
}
private constructor(notificationsById: mobx.ObservableMap<Notification>) {
this.notificationsById = notificationsById;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment