Last active
February 15, 2018 22:03
-
-
Save agnanachandran/3f0f453d2aa258c26c60e9f9d6500a26 to your computer and use it in GitHub Desktop.
stores/notifications_store.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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