mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-24 20:36: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,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<IResultWithStatus<{ messages: IMessage[] }>> =>
|
||||
api
|
||||
.get(API.USER.MESSAGES(username), configWithToken(access))
|
||||
.then(resultMiddleware)
|
||||
.catch(errorMiddleware);
|
||||
|
||||
export const apiAuthSendMessage = ({
|
||||
access,
|
||||
username,
|
||||
message,
|
||||
}): Promise<IResultWithStatus<{ message: IMessage }>> =>
|
||||
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<IResultWithStatus<{ message: IMessage }>> =>
|
||||
api
|
||||
.delete(API.USER.MESSAGE_DELETE(username, id), configWithToken(access))
|
||||
.then(resultMiddleware)
|
||||
.catch(errorMiddleware);
|
||||
|
||||
export const apiAuthGetUpdates = ({
|
||||
access,
|
||||
exclude_dialogs,
|
||||
|
|
|
@ -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;
|
||||
|
|
36
src/redux/messages/api.ts
Normal file
36
src/redux/messages/api.ts
Normal file
|
@ -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<IResultWithStatus<{ messages: IMessage[] }>> =>
|
||||
api
|
||||
.get(API.USER.MESSAGES(username), configWithToken(access))
|
||||
.then(resultMiddleware)
|
||||
.catch(errorMiddleware);
|
||||
|
||||
export const apiMessagesSendMessage = ({
|
||||
access,
|
||||
username,
|
||||
message,
|
||||
}): Promise<IResultWithStatus<{ message: IMessage }>> =>
|
||||
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<IResultWithStatus<{ message: IMessage }>> =>
|
||||
api
|
||||
.delete(API.USER.MESSAGE_DELETE(username, id), configWithToken(access))
|
||||
.then(resultMiddleware)
|
||||
.catch(errorMiddleware);
|
|
@ -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