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

added message delete api call

This commit is contained in:
Fedor Katurov 2020-09-08 12:25:52 +07:00
parent 9bdeb5aa0a
commit c2ebde069d
4 changed files with 113 additions and 47 deletions

View file

@ -1,8 +1,8 @@
import { api, configWithToken, errorMiddleware, resultMiddleware } from '~/utils/api'; import { api, configWithToken, errorMiddleware, resultMiddleware } from '~/utils/api';
import { API } from '~/constants/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 { userLoginTransform } from '~/redux/auth/transforms';
import { ISocialAccount, IToken, IUser } from './types'; import { ISocialAccount, IUser } from './types';
export const apiUserLogin = ({ export const apiUserLogin = ({
username, username,
@ -32,39 +32,6 @@ export const apiAuthGetUserProfile = ({
.then(resultMiddleware) .then(resultMiddleware)
.catch(errorMiddleware); .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 = ({ export const apiAuthGetUpdates = ({
access, access,
exclude_dialogs, exclude_dialogs,

View file

@ -5,6 +5,7 @@ export const selectUser = (state: IState) => state.auth.user;
export const selectToken = (state: IState) => state.auth.token; export const selectToken = (state: IState) => state.auth.token;
export const selectAuthLogin = (state: IState) => state.auth.login; export const selectAuthLogin = (state: IState) => state.auth.login;
export const selectAuthProfile = (state: IState) => state.auth.profile; 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 selectAuthUser = (state: IState) => state.auth.user;
export const selectAuthUpdates = (state: IState) => state.auth.updates; export const selectAuthUpdates = (state: IState) => state.auth.updates;
export const selectAuthRestore = (state: IState) => state.auth.restore; export const selectAuthRestore = (state: IState) => state.auth.restore;

36
src/redux/messages/api.ts Normal file
View 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);

View file

@ -1,11 +1,24 @@
import { authSetUpdates } from '~/redux/auth/actions'; import { authSetUpdates } from '~/redux/auth/actions';
import { call, put, select, takeLatest } from 'redux-saga/effects'; import { call, put, select, takeLatest } from 'redux-saga/effects';
import { selectAuthProfile, selectAuthUpdates } from '~/redux/auth/selectors'; import {
import { apiAuthGetUserMessages, apiAuthSendMessage } from '~/redux/auth/api'; selectAuthProfile,
selectAuthProfileUsername,
selectAuthUpdates,
} from '~/redux/auth/selectors';
import {
apiMessagesDeleteMessage,
apiMessagesGetUserMessages,
apiMessagesSendMessage,
} from '~/redux/messages/api';
import { ERRORS } from '~/constants/errors'; import { ERRORS } from '~/constants/errors';
import { IMessageNotification } from '~/redux/types'; import { IMessageNotification, Unwrap } from '~/redux/types';
import { reqWrapper } from '~/redux/auth/sagas'; 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 { MESSAGES_ACTIONS } from '~/redux/messages/constants';
import { selectMessages } from '~/redux/messages/selectors'; 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) { if (error || !data.messages) {
return yield put( return yield put(
@ -52,18 +65,22 @@ function* getMessages({ username }: ReturnType<typeof messagesGetMessages>) {
} }
function* sendMessage({ message, onSuccess }: ReturnType<typeof messagesSendMessage>) { function* sendMessage({ message, onSuccess }: ReturnType<typeof messagesSendMessage>) {
const { const username: ReturnType<typeof selectAuthProfileUsername> = yield select(
user: { username }, selectAuthProfileUsername
}: ReturnType<typeof selectAuthProfile> = yield select(selectAuthProfile); );
if (!username) return; if (!username) return;
yield put(messagesSet({ is_sending_messages: true, messages_error: null })); yield put(messagesSet({ is_sending_messages: true, messages_error: null }));
const { error, data } = yield call(reqWrapper, apiAuthSendMessage, { const { error, data }: Unwrap<ReturnType<typeof apiMessagesSendMessage>> = yield call(
reqWrapper,
apiMessagesSendMessage,
{
username, username,
message, message,
}); }
);
if (error || !data.message) { if (error || !data.message) {
return yield put( return yield put(
@ -103,7 +120,52 @@ function* sendMessage({ message, onSuccess }: ReturnType<typeof messagesSendMess
onSuccess(); 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*() { export default function*() {
yield takeLatest(MESSAGES_ACTIONS.GET_MESSAGES, getMessages); yield takeLatest(MESSAGES_ACTIONS.GET_MESSAGES, getMessages);
yield takeLatest(MESSAGES_ACTIONS.SEND_MESSAGE, sendMessage); yield takeLatest(MESSAGES_ACTIONS.SEND_MESSAGE, sendMessage);
yield takeLatest(MESSAGES_ACTIONS.DELETE_MESSAGE, deleteMessage);
} }