-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifications.actions.ts
More file actions
74 lines (61 loc) · 2.4 KB
/
notifications.actions.ts
File metadata and controls
74 lines (61 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { ThunkAction, ThunkDispatch } from 'redux-thunk'
import { AnyAction } from 'redux'
import { AxiosResponse } from 'axios'
import { upload } from '../core/core.actions'
import IAction from 'src/interfaces/IAction'
import * as CONSTANTS from './notifications.constants'
import { STATUS_CODES } from '../../constants'
import { requestHttp, urls } from 'src/api'
import { INotification, INotificationIds } from './notifications.types'
import { getResponseErrorMessage } from 'src/helpers'
export const notificationsListRequest = (): IAction => ({
type: CONSTANTS.FETCH_NOTIFICATIONS_REQUEST,
})
export const notificationsListSuccess = (notificationsList: INotification[]): IAction => ({
type: CONSTANTS.FETCH_NOTIFICATIONS_SUCCESS,
payload: notificationsList,
})
export const notificationsListFailure = (error: string): IAction => ({
type: CONSTANTS.FETCH_NOTIFICATIONS_FAILURE,
error,
})
export const updateNotificationsStatusRequest = (notificatonIds: INotificationIds): IAction => ({
type: CONSTANTS.UPDATE_NOTIFICATIONS_REQUEST,
payload: notificatonIds,
})
export const updateNotificationsStatusSuccess = (): IAction => ({
type: CONSTANTS.UPDATE_NOTIFICATIONS_SUCCESS,
})
export const updateNotificationsStatusFailure = (error: string): IAction => ({
type: CONSTANTS.UPDATE_NOTIFICATIONS_FAILURE,
error,
})
export const fetchNotificationsList = (): ThunkAction<Promise<AxiosResponse>, {}, {}, AnyAction> => async (
dispatch: ThunkDispatch<{}, {}, AnyAction>
): Promise<AxiosResponse> => {
try {
dispatch(notificationsListRequest())
const response = await requestHttp.get(urls.getNotificationsListUrl())
const { content } = response.data.data
if (content.results && Array.isArray(content.results)) {
dispatch(notificationsListSuccess(content.results))
return content.results
}
return response
} catch (error) {
dispatch(notificationsListFailure(getResponseErrorMessage(error)))
return error
}
}
export const updateNotificationsStatus = (ids: number[]): ThunkAction<void, {}, {}, AnyAction> => async (
dispatch: ThunkDispatch<{}, {}, AnyAction>
): Promise<void> => {
try {
const response = await requestHttp.patch(urls.getNotificationsUpdateStatusUrl(), { ids })
if (response.status === 200) {
dispatch(updateNotificationsStatusSuccess())
}
} catch (error) {
dispatch(updateNotificationsStatusFailure(getResponseErrorMessage(error)))
}
}