feat: Update notifications and unread count in real time (#4261)

This commit is contained in:
Muhsin Keloth
2022-03-28 20:01:23 +05:30
committed by GitHub
parent ec0ea0b1dc
commit ccf52a620b
11 changed files with 78 additions and 6 deletions

View File

@@ -22,6 +22,7 @@ class ActionCableConnector extends BaseActionCableConnector {
'contact.deleted': this.onContactDelete,
'contact.updated': this.onContactUpdate,
'conversation.mentioned': this.onConversationMentioned,
'notification.created': this.onNotificationCreated,
};
}
@@ -134,6 +135,10 @@ class ActionCableConnector extends BaseActionCableConnector {
onContactUpdate = data => {
this.app.$store.dispatch('contacts/updateContact', data);
};
onNotificationCreated = data => {
this.app.$store.dispatch('notifications/addNotification', data);
};
}
export default {

View File

@@ -52,4 +52,8 @@ export const actions = {
throw new Error(error);
}
},
addNotification({ commit }, data) {
commit(types.ADD_NOTIFICATION, data);
},
};

View File

@@ -45,4 +45,14 @@ export const mutations = {
Vue.set($state.records[item.id], 'read_at', true);
});
},
[types.ADD_NOTIFICATION]($state, data) {
const { notification, unread_count: unreadCount, count } = data;
Vue.set($state.records, notification.id, {
...($state.records[notification.id] || {}),
...notification,
});
Vue.set($state.meta, 'unreadCount', unreadCount);
Vue.set($state.meta, 'count', count);
},
};

View File

@@ -90,4 +90,12 @@ describe('#actions', () => {
await expect(actions.readAll({ commit })).rejects.toThrow(Error);
});
});
describe('#addNotification', () => {
it('sends correct actions if API is success', async () => {
await actions.addNotification({ commit }, { data: 1 });
expect(commit.mock.calls).toEqual([
[types.ADD_NOTIFICATION, { data: 1 }],
]);
});
});
});

View File

@@ -93,4 +93,29 @@ describe('#mutations', () => {
});
});
});
describe('#ADD_NOTIFICATION', () => {
it('add notification', () => {
const state = {
meta: { unreadCount: 4, count: 231 },
records: {
1: { id: 1, primary_actor_id: 1 },
2: { id: 2, primary_actor_id: 2 },
},
};
const data = {
notification: { id: 3, primary_actor_id: 3 },
unread_count: 5,
count: 232,
};
mutations[types.ADD_NOTIFICATION](state, data);
expect(state.records).toEqual({
1: { id: 1, primary_actor_id: 1 },
2: { id: 2, primary_actor_id: 2 },
3: { id: 3, primary_actor_id: 3 },
});
expect(state.meta.unreadCount).toEqual(5);
expect(state.meta.count).toEqual(232);
});
});
});

View File

@@ -116,6 +116,7 @@ export default {
SET_NOTIFICATIONS_UNREAD_COUNT: 'SET_NOTIFICATIONS_UNREAD_COUNT',
SET_NOTIFICATIONS_UI_FLAG: 'SET_NOTIFICATIONS_UI_FLAG',
UPDATE_NOTIFICATION: 'UPDATE_NOTIFICATION',
ADD_NOTIFICATION: 'ADD_NOTIFICATION',
UPDATE_ALL_NOTIFICATIONS: 'UPDATE_ALL_NOTIFICATIONS',
SET_NOTIFICATIONS_ITEM: 'SET_NOTIFICATIONS_ITEM',
SET_NOTIFICATIONS: 'SET_NOTIFICATIONS',