1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-25 12:56:41 +07:00

moved messages to separate reducer

This commit is contained in:
Fedor Katurov 2020-09-08 10:58:55 +07:00
parent bc09810802
commit 737a4396de
14 changed files with 212 additions and 153 deletions

View file

@ -0,0 +1,98 @@
import { authSetUpdates } from '~/redux/auth/actions';
import { call, put, select, takeLatest } from 'redux-saga/effects';
import { selectAuthProfile, selectAuthUpdates } from '~/redux/auth/selectors';
import { apiAuthGetUserMessages, apiAuthSendMessage } from '~/redux/auth/api';
import { ERRORS } from '~/constants/errors';
import { IMessageNotification } from '~/redux/types';
import { reqWrapper } from '~/redux/auth/sagas';
import { messagesGetMessages, messagesSendMessage, messagesSetMessages, } from '~/redux/messages/actions';
import { MESSAGES_ACTIONS } from '~/redux/messages/constants';
import { selectMessages } from '~/redux/messages/selectors';
function* getMessages({ username }: ReturnType<typeof messagesGetMessages>) {
const { messages }: ReturnType<typeof selectMessages> = yield select(selectMessages);
yield put(
messagesSetMessages({
is_loading_messages: true,
messages:
messages &&
messages.length > 0 &&
(messages[0].to.username === username || messages[0].from.username === username)
? messages
: [],
})
);
const { error, data } = yield call(reqWrapper, apiAuthGetUserMessages, { username });
if (error || !data.messages) {
return yield put(
messagesSetMessages({
is_loading_messages: false,
messages_error: ERRORS.EMPTY_RESPONSE,
})
);
}
yield put(messagesSetMessages({ is_loading_messages: false, messages: data.messages }));
const { notifications } = yield select(selectAuthUpdates);
// clear viewed message from notifcation list
const filtered = notifications.filter(
notification =>
notification.type !== 'message' ||
(notification as IMessageNotification).content.from.username !== username
);
if (filtered.length !== notifications.length) {
yield put(authSetUpdates({ notifications: filtered }));
}
}
function* sendMessage({ message, onSuccess }: ReturnType<typeof messagesSendMessage>) {
const {
user: { username },
}: ReturnType<typeof selectAuthProfile> = yield select(selectAuthProfile);
if (!username) return;
yield put(messagesSetMessages({ is_sending_messages: true, messages_error: null }));
const { error, data } = yield call(reqWrapper, apiAuthSendMessage, {
username,
message,
});
if (error || !data.message) {
return yield put(
messagesSetMessages({
is_sending_messages: false,
messages_error: error || ERRORS.EMPTY_RESPONSE,
})
);
}
const { user }: ReturnType<typeof selectAuthProfile> = yield select(selectAuthProfile);
if (user.username !== username) {
return yield put(messagesSetMessages({ is_sending_messages: false }));
}
const { messages }: ReturnType<typeof selectMessages> = yield select(selectMessages);
yield put(
messagesSetMessages({
is_sending_messages: false,
messages: [data.message, ...messages],
})
);
onSuccess();
}
export default function*() {
yield takeLatest(MESSAGES_ACTIONS.GET_MESSAGES, getMessages);
yield takeLatest(MESSAGES_ACTIONS.SEND_MESSAGE, sendMessage);
}