diff --git a/src/redux/auth/api.ts b/src/redux/auth/api.ts index 5b54d330..18cb240c 100644 --- a/src/redux/auth/api.ts +++ b/src/redux/auth/api.ts @@ -1,8 +1,8 @@ import { api, configWithToken, errorMiddleware, resultMiddleware } from '~/utils/api'; import { API } from '~/constants/api'; -import { IMessage, INotification, IResultWithStatus } from '~/redux/types'; +import { INotification, IResultWithStatus } from '~/redux/types'; import { userLoginTransform } from '~/redux/auth/transforms'; -import { ISocialAccount, IToken, IUser } from './types'; +import { ISocialAccount, IUser } from './types'; export const apiUserLogin = ({ username, @@ -32,39 +32,6 @@ export const apiAuthGetUserProfile = ({ .then(resultMiddleware) .catch(errorMiddleware); -export const apiAuthGetUserMessages = ({ - access, - username, -}): Promise> => - api - .get(API.USER.MESSAGES(username), configWithToken(access)) - .then(resultMiddleware) - .catch(errorMiddleware); - -export const apiAuthSendMessage = ({ - access, - username, - message, -}): Promise> => - api - .post(API.USER.MESSAGE_SEND(username), { message }, configWithToken(access)) - .then(resultMiddleware) - .catch(errorMiddleware); - -export const apiAuthDeleteMessage = ({ - access, - username, - id, -}: { - access: string; - username: string; - id: number; -}): Promise> => - api - .delete(API.USER.MESSAGE_DELETE(username, id), configWithToken(access)) - .then(resultMiddleware) - .catch(errorMiddleware); - export const apiAuthGetUpdates = ({ access, exclude_dialogs, diff --git a/src/redux/auth/selectors.ts b/src/redux/auth/selectors.ts index f49a5695..aa3aa475 100644 --- a/src/redux/auth/selectors.ts +++ b/src/redux/auth/selectors.ts @@ -5,6 +5,7 @@ export const selectUser = (state: IState) => state.auth.user; export const selectToken = (state: IState) => state.auth.token; export const selectAuthLogin = (state: IState) => state.auth.login; export const selectAuthProfile = (state: IState) => state.auth.profile; +export const selectAuthProfileUsername = (state: IState) => state.auth.profile.user.username; export const selectAuthUser = (state: IState) => state.auth.user; export const selectAuthUpdates = (state: IState) => state.auth.updates; export const selectAuthRestore = (state: IState) => state.auth.restore; diff --git a/src/redux/messages/api.ts b/src/redux/messages/api.ts new file mode 100644 index 00000000..a5252022 --- /dev/null +++ b/src/redux/messages/api.ts @@ -0,0 +1,36 @@ +import { IMessage, IResultWithStatus } from '~/redux/types'; +import { api, configWithToken, errorMiddleware, resultMiddleware } from '~/utils/api'; +import { API } from '~/constants/api'; + +export const apiMessagesGetUserMessages = ({ + access, + username, +}): Promise> => + api + .get(API.USER.MESSAGES(username), configWithToken(access)) + .then(resultMiddleware) + .catch(errorMiddleware); + +export const apiMessagesSendMessage = ({ + access, + username, + message, +}): Promise> => + api + .post(API.USER.MESSAGE_SEND(username), { message }, configWithToken(access)) + .then(resultMiddleware) + .catch(errorMiddleware); + +export const apiMessagesDeleteMessage = ({ + access, + username, + id, +}: { + access: string; + username: string; + id: number; +}): Promise> => + api + .delete(API.USER.MESSAGE_DELETE(username, id), configWithToken(access)) + .then(resultMiddleware) + .catch(errorMiddleware); diff --git a/src/redux/messages/sagas.ts b/src/redux/messages/sagas.ts index 91a8e80b..f59b6e66 100644 --- a/src/redux/messages/sagas.ts +++ b/src/redux/messages/sagas.ts @@ -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) { }) ); - 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) { } function* sendMessage({ message, onSuccess }: ReturnType) { - const { - user: { username }, - }: ReturnType = yield select(selectAuthProfile); + const username: ReturnType = 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> = yield call( + reqWrapper, + apiMessagesSendMessage, + { + username, + message, + } + ); if (error || !data.message) { return yield put( @@ -103,7 +120,52 @@ function* sendMessage({ message, onSuccess }: ReturnType) { + const username: ReturnType = yield select( + selectAuthProfileUsername + ); + + if (!username) return; + + yield put(messagesSet({ is_sending_messages: true, messages_error: null })); + + const { error, data }: Unwrap> = yield call( + reqWrapper, + apiMessagesDeleteMessage, + { + username, + id, + } + ); + + if (error || !data.message) { + return yield put( + messagesSet({ + is_sending_messages: false, + }) + ); + } + + const currentUsername: ReturnType = yield select( + selectAuthProfileUsername + ); + + if (currentUsername !== username) { + return yield put(messagesSet({ is_sending_messages: false })); + } + + const { messages }: ReturnType = 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); }