Commit a0bdf1bf authored by Shpuld Shpludson's avatar Shpuld Shpludson

Merge branch 'feat/update_unread_notifications_count' into 'master'

Added action for update unread notifications count

See merge request !37
parents fee3782b 4140c330
Pipeline #27661 passed with stages
in 5 minutes and 45 seconds
......@@ -80,6 +80,18 @@ const deleteUserStatus = (state, { userId, statusId }) => {
}
}
const updateUnreadNotificationsCount = (state, { unreadNotificationsCount }) => {
return {
...state,
currentUser: {
...state.currentUser,
pleroma: {
...state.currentUser.pleroma,
unread_notifications_count: unreadNotificationsCount
}
}
}
}
const addSearchResult = (state, { request, users }) => {
const searchCache = uniq([request, ...state.searchCache])
......@@ -96,6 +108,7 @@ const reducers = {
updateCurrentUser,
addUserStatuses,
deleteUserStatus,
updateUnreadNotificationsCount,
addSearchResult
}
......@@ -136,6 +149,12 @@ const actions = {
payload: { userId, statusId }
}
},
updateUnreadNotificationsCount: ({ unreadNotificationsCount }) => {
return {
type: 'updateUnreadNotificationsCount',
payload: { unreadNotificationsCount }
}
},
addSearchResult: ({ request, users }) => {
return {
type: 'addSearchResult',
......
import notificationsApi from '../api/notifications.js'
import Notifications from '../reducers/notifications.js'
import Users from '../reducers/users.js'
import Api from '../reducers/api'
import { apiErrorCatcher, getConfig } from '../utils/api_utils'
......@@ -48,12 +49,18 @@ const notificationsThunks = {
if (!passedParams.id && !passedParams.max_id) return getState()
await notificationsApi.read({ config: getConfig(getState, config), params: passedParams })
.then(res => apiErrorCatcher(res))
if (passedParams.id) {
await dispatch(Notifications.actions.read({ notificationId: passedParams.id }))
} else {
await dispatch(Notifications.actions.readAll())
}
const state = await getState()
if (state.users && state.users.currentUser) {
const oldUnreadNotificationsCount = state.users.currentUser.pleroma.unread_notifications_count
const unreadNotificationsCount = passedParams.id && oldUnreadNotificationsCount > 1 ? oldUnreadNotificationsCount - 1 : 0
await dispatch(Users.actions.updateUnreadNotificationsCount({ unreadNotificationsCount }))
}
return getState()
}
}
......
......@@ -133,6 +133,16 @@ describe('User reducers', () => {
expect(resultState.usersByIds['1']).toEqual({ ...user, statuses: [{ id: 2, content: 'b' }] })
})
it('update unread notifications count', () => {
const currentUser = { id: '1', pleroma: { unread_notifications_count: 5 } }
const resultState = Users.reducer(
{ currentUser },
Users.actions.updateUnreadNotificationsCount({ unreadNotificationsCount: 1 })
)
expect(resultState.currentUser).toEqual({ id: '1', pleroma: { unread_notifications_count: 1 } })
})
})
it('add search result', () => {
const user = { id: '1', name: 'a' }
......
......@@ -8,7 +8,8 @@ jest.mock('cross-fetch')
const reducer = combineReducers({
api: reducers.api.reducer,
notifications: reducers.notifications.reducer
notifications: reducers.notifications.reducer,
users: reducers.users.reducer,
})
describe('Notifications thunks', () => {
......@@ -174,6 +175,14 @@ describe('Notifications thunks', () => {
'2': { id: '2', pleroma: { is_seen: false } }
},
list: ['1', '2']
},
users: {
currentUser: {
id: '3',
pleroma: {
unread_notifications_count: 3
}
}
}
} }
......@@ -196,6 +205,8 @@ describe('Notifications thunks', () => {
expect(state.notifications.list)
.toEqual(['1', '2'])
expect(state.users.currentUser.pleroma.unread_notifications_count)
.toEqual(2)
})
it('read all notification', async () => {
......@@ -205,6 +216,12 @@ describe('Notifications thunks', () => {
'1': { id: '1', pleroma: { is_seen: false } }
},
list: ['1']
},
users: {
currentUser: {
id: '2',
pleroma: { unread_notifications_count: 3 }
}
}
} }
......@@ -225,5 +242,7 @@ describe('Notifications thunks', () => {
expect(state.notifications.list)
.toEqual(['1'])
expect(state.users.currentUser.pleroma.unread_notifications_count)
.toEqual(0)
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment