1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-19 13:19:44 +02:00
planka/client/src/models/Notification.js

89 lines
2.1 KiB
JavaScript
Raw Normal View History

2019-08-31 04:07:25 +05:00
import { Model, attr, fk } from 'redux-orm';
import ActionTypes from '../constants/ActionTypes';
export default class extends Model {
static modelName = 'Notification';
static fields = {
id: attr(),
type: attr(),
data: attr(),
isRead: attr(),
userId: fk({
to: 'User',
as: 'user',
relatedName: 'notifications',
}),
actionId: fk({
to: 'Action',
as: 'action',
relatedName: 'notifications',
}),
cardId: fk({
to: 'Card',
as: 'card',
relatedName: 'notifications',
}),
};
static reducer({ type, payload }, Notification) {
switch (type) {
case ActionTypes.LOCATION_CHANGE_HANDLE:
case ActionTypes.PROJECT_MANAGER_CREATE_HANDLE:
case ActionTypes.BOARD_MEMBERSHIP_CREATE_HANDLE:
if (payload.notifications) {
payload.notifications.forEach((notification) => {
Notification.withId(notification.id).deleteWithRelated();
});
}
break;
case ActionTypes.SOCKET_RECONNECT_HANDLE:
Notification.all().delete();
payload.notifications.forEach((notification) => {
Notification.upsert(notification);
2019-08-31 04:07:25 +05:00
});
break;
case ActionTypes.CORE_INITIALIZE:
2020-03-25 00:15:47 +05:00
payload.notifications.forEach((notification) => {
2019-08-31 04:07:25 +05:00
Notification.upsert(notification);
});
break;
case ActionTypes.NOTIFICATION_CREATE_HANDLE:
2019-08-31 04:07:25 +05:00
Notification.upsert(payload.notification);
break;
case ActionTypes.NOTIFICATION_DELETE:
Notification.withId(payload.id).deleteWithRelated();
break;
case ActionTypes.NOTIFICATION_DELETE__SUCCESS:
case ActionTypes.NOTIFICATION_DELETE_HANDLE: {
2019-08-31 04:07:25 +05:00
const notificationModel = Notification.withId(payload.notification.id);
if (notificationModel) {
notificationModel.deleteWithRelated();
2019-08-31 04:07:25 +05:00
}
break;
}
default:
}
}
deleteRelated() {
if (this.action && !this.action.isInCard) {
this.action.delete();
}
}
deleteWithRelated() {
this.deleteRelated();
this.delete();
}
2019-08-31 04:07:25 +05:00
}