11/* eslint-disable no-param-reassign */
22import { createSlice } from '@reduxjs/toolkit' ;
33
4- export const IDLE = 'idle' ;
5- export const LOADING = 'loading' ;
6- export const LOADED = 'loaded' ;
7- export const FAILED = 'failed' ;
8- export const DENIED = 'denied' ;
4+ export const RequestStatus = {
5+ IDLE : 'idle' ,
6+ LOADING : 'in-progress' ,
7+ LOADED : 'successful' ,
8+ FAILED : 'failed' ,
9+ DENIED : 'denied' ,
10+ } ;
911
1012const initialState = {
1113 notificationStatus : 'idle' ,
12- appName : 'reminders ' ,
14+ appName : 'discussions ' ,
1315 appsId : [ ] ,
1416 apps : { } ,
1517 notifications : { } ,
@@ -26,65 +28,62 @@ const slice = createSlice({
2628 name : 'notifications' ,
2729 initialState,
2830 reducers : {
29- fetchNotificationDenied : ( state , { payload } ) => {
30- state . appName = payload . appName ;
31- state . notificationStatus = DENIED ;
31+ fetchNotificationDenied : ( state ) => {
32+ state . notificationStatus = RequestStatus . DENIED ;
3233 } ,
33- fetchNotificationFailure : ( state , { payload } ) => {
34- state . appName = payload . appName ;
35- state . notificationStatus = FAILED ;
34+ fetchNotificationFailure : ( state ) => {
35+ state . notificationStatus = RequestStatus . FAILED ;
3636 } ,
37- fetchNotificationRequest : ( state , { payload } ) => {
38- if ( state . appName !== payload . appName ) { state . apps [ payload . appName ] = [ ] ; }
39- state . appName = payload . appName ;
40- state . notificationStatus = LOADING ;
37+ fetchNotificationRequest : ( state ) => {
38+ state . notificationStatus = RequestStatus . LOADING ;
4139 } ,
4240 fetchNotificationSuccess : ( state , { payload } ) => {
43- const { notifications, numPages, currentPage } = payload ;
44- const newNotificationIds = notifications . map ( notification => notification . id . toString ( ) ) ;
41+ const {
42+ newNotificationIds, notificationsKeyValuePair, numPages, currentPage,
43+ } = payload ;
4544 const existingNotificationIds = state . apps [ state . appName ] ;
46- const notificationsKeyValuePair = notifications . reduce ( ( acc , obj ) => { acc [ obj . id ] = obj ; return acc ; } , { } ) ;
47- const currentAppCount = state . tabsCount [ state . appName ] ;
4845
4946 state . apps [ state . appName ] = Array . from ( new Set ( [ ...existingNotificationIds , ...newNotificationIds ] ) ) ;
5047 state . notifications = { ...state . notifications , ...notificationsKeyValuePair } ;
51- state . tabsCount . count -= currentAppCount ;
48+ state . tabsCount . count -= state . tabsCount [ state . appName ] ;
5249 state . tabsCount [ state . appName ] = 0 ;
53- state . notificationStatus = LOADED ;
50+ state . notificationStatus = RequestStatus . LOADED ;
5451 state . pagination . numPages = numPages ;
5552 state . pagination . currentPage = currentPage ;
5653 } ,
5754 fetchNotificationsCountDenied : ( state ) => {
58- state . notificationStatus = DENIED ;
55+ state . notificationStatus = RequestStatus . DENIED ;
5956 } ,
6057 fetchNotificationsCountFailure : ( state ) => {
61- state . notificationStatus = FAILED ;
58+ state . notificationStatus = RequestStatus . FAILED ;
6259 } ,
6360 fetchNotificationsCountRequest : ( state ) => {
64- state . notificationStatus = LOADING ;
61+ state . notificationStatus = RequestStatus . LOADING ;
6562 } ,
6663 fetchNotificationsCountSuccess : ( state , { payload } ) => {
67- const { countByAppName, count, showNotificationTray } = payload ;
64+ const {
65+ countByAppName, appIds, apps, count, showNotificationTray,
66+ } = payload ;
6867 state . tabsCount = { count, ...countByAppName } ;
69- state . appsId = Object . keys ( countByAppName ) ;
70- state . apps = Object . fromEntries ( Object . keys ( countByAppName ) . map ( key => [ key , [ ] ] ) ) ;
68+ state . appsId = appIds ;
69+ state . apps = apps ;
7170 state . showNotificationTray = showNotificationTray ;
72- state . notificationStatus = LOADED ;
71+ state . notificationStatus = RequestStatus . LOADED ;
7372 } ,
7473 markNotificationsAsSeenRequest : ( state ) => {
75- state . notificationStatus = LOADING ;
74+ state . notificationStatus = RequestStatus . LOADING ;
7675 } ,
7776 markNotificationsAsSeenSuccess : ( state ) => {
78- state . notificationStatus = LOADED ;
77+ state . notificationStatus = RequestStatus . LOADED ;
7978 } ,
8079 markNotificationsAsSeenDenied : ( state ) => {
81- state . notificationStatus = DENIED ;
80+ state . notificationStatus = RequestStatus . DENIED ;
8281 } ,
8382 markNotificationsAsSeenFailure : ( state ) => {
84- state . notificationStatus = FAILED ;
83+ state . notificationStatus = RequestStatus . FAILED ;
8584 } ,
8685 markAllNotificationsAsReadRequest : ( state ) => {
87- state . notificationStatus = LOADING ;
86+ state . notificationStatus = RequestStatus . LOADING ;
8887 } ,
8988 markAllNotificationsAsReadSuccess : ( state ) => {
9089 const updatedNotifications = Object . fromEntries (
@@ -93,27 +92,27 @@ const slice = createSlice({
9392 ] ) ,
9493 ) ;
9594 state . notifications = updatedNotifications ;
96- state . notificationStatus = LOADED ;
95+ state . notificationStatus = RequestStatus . LOADED ;
9796 } ,
9897 markAllNotificationsAsReadDenied : ( state ) => {
99- state . notificationStatus = DENIED ;
98+ state . notificationStatus = RequestStatus . DENIED ;
10099 } ,
101100 markAllNotificationsAsReadFailure : ( state ) => {
102- state . notificationStatus = FAILED ;
101+ state . notificationStatus = RequestStatus . FAILED ;
103102 } ,
104103 markNotificationsAsReadRequest : ( state ) => {
105- state . notificationStatus = LOADING ;
104+ state . notificationStatus = RequestStatus . LOADING ;
106105 } ,
107106 markNotificationsAsReadSuccess : ( state , { payload } ) => {
108107 const date = new Date ( ) . toISOString ( ) ;
109108 state . notifications [ payload . id ] = { ...state . notifications [ payload . id ] , lastRead : date } ;
110- state . notificationStatus = LOADED ;
109+ state . notificationStatus = RequestStatus . LOADED ;
111110 } ,
112111 markNotificationsAsReadDenied : ( state ) => {
113- state . notificationStatus = DENIED ;
112+ state . notificationStatus = RequestStatus . DENIED ;
114113 } ,
115114 markNotificationsAsReadFailure : ( state ) => {
116- state . notificationStatus = FAILED ;
115+ state . notificationStatus = RequestStatus . FAILED ;
117116 } ,
118117 resetNotificationStateRequest : ( ) => initialState ,
119118 updateAppNameRequest : ( state , { payload } ) => {
0 commit comments