2021-07-07 15:13:44 +00:00
|
|
|
import { put, select } from "redux-saga/effects";
|
2021-06-09 14:32:17 +00:00
|
|
|
import { SOCKET_EVENTS } from "./constants";
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
newCommentEvent,
|
|
|
|
|
newCommentThreadEvent,
|
|
|
|
|
updateCommentThreadEvent,
|
|
|
|
|
updateCommentEvent,
|
2021-07-07 15:13:44 +00:00
|
|
|
incrementThreadUnreadCount,
|
2021-07-28 07:02:11 +00:00
|
|
|
decrementThreadUnreadCount,
|
2021-06-09 14:32:17 +00:00
|
|
|
} from "actions/commentActions";
|
|
|
|
|
|
|
|
|
|
import { newNotificationEvent } from "actions/notificationActions";
|
2021-07-07 15:13:44 +00:00
|
|
|
import { getCurrentUser } from "selectors/usersSelectors";
|
|
|
|
|
import { getCurrentApplication } from "selectors/applicationSelectors";
|
2021-07-28 07:02:11 +00:00
|
|
|
import { commentThreadsSelector } from "selectors/commentsSelectors";
|
|
|
|
|
import { AppState } from "reducers";
|
|
|
|
|
import { CommentThread } from "entities/Comments/CommentsInterfaces";
|
2021-06-09 14:32:17 +00:00
|
|
|
|
|
|
|
|
export default function* handleSocketEvent(event: any) {
|
2021-07-07 15:13:44 +00:00
|
|
|
const currentUser = yield select(getCurrentUser);
|
|
|
|
|
const currentApplication = yield select(getCurrentApplication);
|
|
|
|
|
|
2021-06-09 14:32:17 +00:00
|
|
|
switch (event.type) {
|
|
|
|
|
// comments
|
|
|
|
|
case SOCKET_EVENTS.INSERT_COMMENT_THREAD: {
|
|
|
|
|
yield put(newCommentThreadEvent(event.payload[0]));
|
2021-07-07 15:13:44 +00:00
|
|
|
|
|
|
|
|
const { thread } = event.payload[0];
|
|
|
|
|
const isForCurrentApplication =
|
|
|
|
|
thread?.applicationId === currentApplication.id;
|
|
|
|
|
|
|
|
|
|
const isCreatedByMe = thread?.authorUsername === currentUser.username;
|
|
|
|
|
if (!isCreatedByMe && isForCurrentApplication)
|
|
|
|
|
yield put(incrementThreadUnreadCount());
|
2021-06-09 14:32:17 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case SOCKET_EVENTS.INSERT_COMMENT: {
|
|
|
|
|
yield put(newCommentEvent(event.payload[0]));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-07-28 07:02:11 +00:00
|
|
|
case SOCKET_EVENTS.REPLACE_COMMENT_THREAD:
|
2021-06-09 14:32:17 +00:00
|
|
|
case SOCKET_EVENTS.UPDATE_COMMENT_THREAD: {
|
2021-07-28 07:02:11 +00:00
|
|
|
const { thread } = event.payload[0];
|
|
|
|
|
const threadInStore: CommentThread = yield select((state: AppState) =>
|
|
|
|
|
commentThreadsSelector(thread?._id)(state),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const isThreadInStoreViewed = threadInStore?.isViewed;
|
|
|
|
|
const isThreadFromEventViewed = thread?.viewedByUsers?.includes(
|
|
|
|
|
currentUser?.username,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
yield put(
|
|
|
|
|
updateCommentThreadEvent({
|
|
|
|
|
...thread,
|
|
|
|
|
isViewed: isThreadFromEventViewed,
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (isThreadInStoreViewed && !isThreadFromEventViewed) {
|
|
|
|
|
yield put(incrementThreadUnreadCount());
|
|
|
|
|
} else if (!isThreadInStoreViewed && isThreadFromEventViewed) {
|
|
|
|
|
yield put(decrementThreadUnreadCount());
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-09 14:32:17 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case SOCKET_EVENTS.UPDATE_COMMENT: {
|
|
|
|
|
yield put(updateCommentEvent(event.payload[0].comment));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// notifications
|
|
|
|
|
case SOCKET_EVENTS.INSERT_NOTIFICATION: {
|
|
|
|
|
yield put(newNotificationEvent(event.payload[0].notification));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|