@magicbell/core
Version:
Official MagicBell API wrapper
120 lines • 4.33 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const camelize_js_1 = tslib_1.__importDefault(require("../lib/decorators/camelize.js"));
const index_js_1 = tslib_1.__importDefault(require("../models/Notification/index.js"));
const NotificationFactory_js_1 = tslib_1.__importDefault(require("../models/Notification/NotificationFactory.js"));
const NotificationRepository_js_1 = tslib_1.__importDefault(require("../models/Notification/NotificationRepository.js"));
const RemoteStore_js_1 = tslib_1.__importDefault(require("./RemoteStore.js"));
/**
* A synced collection, or store, of notifications.
*
* @example
* const store = new NotificationStore()
*/
class NotificationStore extends RemoteStore_js_1.default {
unseenCount = 0;
unreadCount = 0;
repo;
constructor(attrs = {}) {
super();
this.set(attrs);
this.repo = new NotificationRepository_js_1.default();
}
get notifications() {
return this.items;
}
/**
* Create a notification, store it in the API server and add it to the store.
*
* @param data Data of the notification.
*/
create = async (data) => {
const notification = await index_js_1.default.create(data);
this.push(notification);
return notification;
};
/**
* Mark all notifications as read. Resets the `unreadCount` attribute.
*
* @param omitRequest Update notifications locally only.
*/
markAllAsRead = async ({ omitRequest = false } = {}) => {
this.unreadCount = 0;
// @TODO Remove the deep observer so the items array changes only after
// the action is completed. In the meantime we clone the items array.
for (const notification of this.items.slice())
notification.isRead = true;
if (!omitRequest)
return this.repo.markAllAsRead();
};
/**
* Mark all notifications as seen. Resets the `unseenCount` attribute.
*
* @param omitRequest Update notifications locally only.
* @param updateItems Mark all notifications as seen.
*/
markAllAsSeen = async ({ omitRequest = false, updateItems = true } = {}) => {
this.unseenCount = 0;
if (updateItems) {
// @TODO Remove the deep observer so the items array changes only after
// the action is completed. In the meantime we clone the items array.
for (const notification of this.items.slice())
notification.isSeen = true;
}
if (!omitRequest)
this.repo.markAllAsSeen();
};
push(notification) {
const added = super.push(notification);
if (added && !notification.isRead)
this.incrementUnreadCount();
if (added && !notification.isSeen)
this.incrementUnseenCount();
return added;
}
remove(notification) {
const removed = super.remove(notification);
if (removed && !notification.isRead)
this.decrementUnreadCount();
if (removed && !notification.isSeen)
this.decrementUnseenCount();
return removed;
}
incrementUnreadCount() {
this.unreadCount = this.unreadCount + 1;
}
decrementUnreadCount() {
this.unreadCount = Math.max(0, this.unreadCount - 1);
}
incrementUnseenCount() {
this.unseenCount = this.unseenCount + 1;
}
decrementUnseenCount() {
this.unseenCount = Math.max(0, this.unseenCount - 1);
}
set(json) {
const { notifications, ...metadata } = json;
Object.assign(this, metadata);
if (notifications)
this.setItems(notifications);
}
/**
* Append notifications to the store.
*
* @param items Object containing the pagination data
*/
setItems(items) {
const notifications = items.map((notification) => this.createNotification(notification));
this.items.push(...notifications);
return this;
}
createNotification(data) {
return NotificationFactory_js_1.default.create(data);
}
}
exports.default = NotificationStore;
tslib_1.__decorate([
(0, camelize_js_1.default)()
], NotificationStore.prototype, "set", null);
//# sourceMappingURL=NotificationStore.js.map