PromucFlow_constructor/app/client/src/sagas/NotificationsSagas.ts

116 lines
3.4 KiB
TypeScript
Raw Normal View History

2021-06-09 14:32:17 +00:00
import { call, takeLatest, put, all } from "redux-saga/effects";
import NotificationApi from "api/NotificationsAPI";
2021-06-23 15:42:07 +00:00
import {
ReduxAction,
ReduxActionErrorTypes,
ReduxActionTypes,
} from "constants/ReduxActionConstants";
2021-06-09 14:32:17 +00:00
import { validateResponse } from "./ErrorSagas";
2021-06-23 15:42:07 +00:00
import {
markAllNotificationsAsReadSuccess,
resetNotifications,
fetchNotificationsSuccess,
fetchUnreadNotificationsCountSuccess,
fetchUnreadNotificationsCountRequest,
markNotificationAsReadSuccess,
} from "actions/notificationActions";
2021-06-09 14:32:17 +00:00
2021-06-23 15:42:07 +00:00
export function* fetchNotifications(action: ReduxAction<string>) {
2021-06-09 14:32:17 +00:00
try {
2021-06-23 15:42:07 +00:00
const response = yield call(
NotificationApi.fetchNotifications,
action.payload,
);
2021-06-09 14:32:17 +00:00
const isValidResponse = yield validateResponse(response);
if (isValidResponse) {
2021-06-23 15:42:07 +00:00
yield put(fetchNotificationsSuccess({ notifications: response.data }));
2021-06-09 14:32:17 +00:00
}
} catch (error) {
2021-06-23 15:42:07 +00:00
yield put({
type: ReduxActionErrorTypes.FETCH_NOTIFICATIONS_ERROR,
payload: { error, logToSentry: true },
});
2021-06-09 14:32:17 +00:00
}
}
2021-06-23 15:42:07 +00:00
/**
* 1. mark all as read
* 2. reset notifications
* 3. reset unread notificaions count
*/
2021-06-09 14:32:17 +00:00
function* markAllNotificationsAsRead() {
2021-06-23 15:42:07 +00:00
try {
const response = yield call(NotificationApi.markAllNotificationsAsRead);
const isValidResponse = yield validateResponse(response);
if (isValidResponse) {
yield put(markAllNotificationsAsReadSuccess());
const response = yield call(NotificationApi.fetchNotifications);
const isValidResponse = yield validateResponse(response);
if (isValidResponse) {
yield put(resetNotifications({ notifications: response.data }));
}
yield put(fetchUnreadNotificationsCountRequest());
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.MARK_ALL_NOTIFICAIONS_AS_READ_ERROR,
payload: { error, logToSentry: true },
});
}
}
function* fetchUnreadNotificationsCount() {
try {
const response = yield call(NotificationApi.fetchUnreadNotificationsCount);
const isValidResponse = yield validateResponse(response);
if (isValidResponse) {
yield put(fetchUnreadNotificationsCountSuccess(response.data));
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.FETCH_UNREAD_NOTIFICATIONS_COUNT_ERROR,
payload: { error, logToSentry: true },
});
}
}
function* markNotificationAsRead(action: ReduxAction<string>) {
try {
const response = yield call(NotificationApi.markNotificationsAsRead, [
action.payload,
]);
const isValidResponse = yield validateResponse(response);
if (isValidResponse) {
yield put(fetchUnreadNotificationsCountSuccess(response.data));
yield put(markNotificationAsReadSuccess(action.payload));
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.MARK_NOTIFICATION_AS_READ_ERROR,
payload: { error, logToSentry: true },
});
}
2021-06-09 14:32:17 +00:00
}
export default function* notificationsSagas() {
yield all([
takeLatest(
ReduxActionTypes.FETCH_NOTIFICATIONS_REQUEST,
fetchNotifications,
),
takeLatest(
ReduxActionTypes.MARK_ALL_NOTIFICATIONS_AS_READ_REQUEST,
markAllNotificationsAsRead,
),
2021-06-23 15:42:07 +00:00
takeLatest(
ReduxActionTypes.FETCH_UNREAD_NOTIFICATIONS_COUNT_REQUEST,
fetchUnreadNotificationsCount,
),
takeLatest(
ReduxActionTypes.MARK_NOTIFICATION_AS_READ_REQUEST,
markNotificationAsRead,
),
2021-06-09 14:32:17 +00:00
]);
}