mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 04:46:40 +07:00
added message delete api call
This commit is contained in:
parent
9bdeb5aa0a
commit
c2ebde069d
4 changed files with 113 additions and 47 deletions
|
@ -1,11 +1,24 @@
|
|||
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 {
|
||||
selectAuthProfile,
|
||||
selectAuthProfileUsername,
|
||||
selectAuthUpdates,
|
||||
} from '~/redux/auth/selectors';
|
||||
import {
|
||||
apiMessagesDeleteMessage,
|
||||
apiMessagesGetUserMessages,
|
||||
apiMessagesSendMessage,
|
||||
} from '~/redux/messages/api';
|
||||
import { ERRORS } from '~/constants/errors';
|
||||
import { IMessageNotification } from '~/redux/types';
|
||||
import { IMessageNotification, Unwrap } from '~/redux/types';
|
||||
import { reqWrapper } from '~/redux/auth/sagas';
|
||||
import { messagesGetMessages, messagesSendMessage, messagesSet } from '~/redux/messages/actions';
|
||||
import {
|
||||
messagesDeleteMessage,
|
||||
messagesGetMessages,
|
||||
messagesSendMessage,
|
||||
messagesSet,
|
||||
} from '~/redux/messages/actions';
|
||||
import { MESSAGES_ACTIONS } from '~/redux/messages/constants';
|
||||
import { selectMessages } from '~/redux/messages/selectors';
|
||||
|
||||
|
@ -24,7 +37,7 @@ function* getMessages({ username }: ReturnType<typeof messagesGetMessages>) {
|
|||
})
|
||||
);
|
||||
|
||||
const { error, data } = yield call(reqWrapper, apiAuthGetUserMessages, { username });
|
||||
const { error, data } = yield call(reqWrapper, apiMessagesGetUserMessages, { username });
|
||||
|
||||
if (error || !data.messages) {
|
||||
return yield put(
|
||||
|
@ -52,18 +65,22 @@ function* getMessages({ username }: ReturnType<typeof messagesGetMessages>) {
|
|||
}
|
||||
|
||||
function* sendMessage({ message, onSuccess }: ReturnType<typeof messagesSendMessage>) {
|
||||
const {
|
||||
user: { username },
|
||||
}: ReturnType<typeof selectAuthProfile> = yield select(selectAuthProfile);
|
||||
const username: ReturnType<typeof selectAuthProfileUsername> = yield select(
|
||||
selectAuthProfileUsername
|
||||
);
|
||||
|
||||
if (!username) return;
|
||||
|
||||
yield put(messagesSet({ is_sending_messages: true, messages_error: null }));
|
||||
|
||||
const { error, data } = yield call(reqWrapper, apiAuthSendMessage, {
|
||||
username,
|
||||
message,
|
||||
});
|
||||
const { error, data }: Unwrap<ReturnType<typeof apiMessagesSendMessage>> = yield call(
|
||||
reqWrapper,
|
||||
apiMessagesSendMessage,
|
||||
{
|
||||
username,
|
||||
message,
|
||||
}
|
||||
);
|
||||
|
||||
if (error || !data.message) {
|
||||
return yield put(
|
||||
|
@ -103,7 +120,52 @@ function* sendMessage({ message, onSuccess }: ReturnType<typeof messagesSendMess
|
|||
onSuccess();
|
||||
}
|
||||
|
||||
function* deleteMessage({ id }: ReturnType<typeof messagesDeleteMessage>) {
|
||||
const username: ReturnType<typeof selectAuthProfileUsername> = yield select(
|
||||
selectAuthProfileUsername
|
||||
);
|
||||
|
||||
if (!username) return;
|
||||
|
||||
yield put(messagesSet({ is_sending_messages: true, messages_error: null }));
|
||||
|
||||
const { error, data }: Unwrap<ReturnType<typeof apiMessagesDeleteMessage>> = yield call(
|
||||
reqWrapper,
|
||||
apiMessagesDeleteMessage,
|
||||
{
|
||||
username,
|
||||
id,
|
||||
}
|
||||
);
|
||||
|
||||
if (error || !data.message) {
|
||||
return yield put(
|
||||
messagesSet({
|
||||
is_sending_messages: false,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
const currentUsername: ReturnType<typeof selectAuthProfileUsername> = yield select(
|
||||
selectAuthProfileUsername
|
||||
);
|
||||
|
||||
if (currentUsername !== username) {
|
||||
return yield put(messagesSet({ is_sending_messages: false }));
|
||||
}
|
||||
|
||||
const { messages }: ReturnType<typeof selectMessages> = yield select(selectMessages);
|
||||
|
||||
yield put(
|
||||
messagesSet({
|
||||
is_sending_messages: false,
|
||||
messages: messages.map(item => (item.id === id ? data.message : item)),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
export default function*() {
|
||||
yield takeLatest(MESSAGES_ACTIONS.GET_MESSAGES, getMessages);
|
||||
yield takeLatest(MESSAGES_ACTIONS.SEND_MESSAGE, sendMessage);
|
||||
yield takeLatest(MESSAGES_ACTIONS.DELETE_MESSAGE, deleteMessage);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue