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

messages: refactored reducer

This commit is contained in:
Fedor Katurov 2021-03-02 16:35:46 +07:00
parent 64b27e517f
commit be7829f130
3 changed files with 176 additions and 171 deletions

View file

@ -1,48 +1,29 @@
import { IMessage, IResultWithStatus } from '~/redux/types'; import { api, cleanResult } from '~/utils/api';
import { api, configWithToken, errorMiddleware, resultMiddleware } from '~/utils/api';
import { API } from '~/constants/api'; import { API } from '~/constants/api';
import {
ApiDeleteMessageRequest,
ApiDeleteMessageResult,
ApiGetUserMessagesRequest,
ApiGetUserMessagesResponse,
ApiSendMessageRequest,
ApiSendMessageResult,
} from '~/redux/messages/types';
export const apiMessagesGetUserMessages = ({ export const apiGetUserMessages = ({ username, after, before }: ApiGetUserMessagesRequest) =>
access,
username,
after,
before,
}: {
access: string;
username: string;
after?: string;
before?: string;
}): Promise<IResultWithStatus<{ messages: IMessage[] }>> =>
api api
.get(API.USER.MESSAGES(username), configWithToken(access, { params: { after, before } })) .get<ApiGetUserMessagesResponse>(API.USER.MESSAGES(username), {
.then(resultMiddleware) params: { after, before },
.catch(errorMiddleware); })
.then(cleanResult);
export const apiMessagesSendMessage = ({ export const apiSendMessage = ({ username, message }: ApiSendMessageRequest) =>
access,
username,
message,
}): Promise<IResultWithStatus<{ message: IMessage }>> =>
api api
.post(API.USER.MESSAGE_SEND(username), { message }, configWithToken(access)) .post<ApiSendMessageResult>(API.USER.MESSAGE_SEND(username), { message })
.then(resultMiddleware) .then(cleanResult);
.catch(errorMiddleware);
export const apiMessagesDeleteMessage = ({ export const apiDeleteMessage = ({ username, id, is_locked }: ApiDeleteMessageRequest) =>
access,
username,
id,
is_locked,
}: {
access: string;
username: string;
id: number;
is_locked: boolean;
}): Promise<IResultWithStatus<{ message: IMessage }>> =>
api api
.delete( .delete<ApiDeleteMessageResult>(API.USER.MESSAGE_DELETE(username, id), {
API.USER.MESSAGE_DELETE(username, id), params: { is_locked },
configWithToken(access, { params: { is_locked } }) })
) .then(cleanResult);
.then(resultMiddleware)
.catch(errorMiddleware);

View file

@ -5,14 +5,9 @@ import {
selectAuthProfileUsername, selectAuthProfileUsername,
selectAuthUpdates, selectAuthUpdates,
} from '~/redux/auth/selectors'; } from '~/redux/auth/selectors';
import { import { apiDeleteMessage, apiGetUserMessages, apiSendMessage } from '~/redux/messages/api';
apiMessagesDeleteMessage,
apiMessagesGetUserMessages,
apiMessagesSendMessage,
} from '~/redux/messages/api';
import { ERRORS } from '~/constants/errors'; import { ERRORS } from '~/constants/errors';
import { IMessageNotification, Unwrap } from '~/redux/types'; import { IMessageNotification, Unwrap } from '~/redux/types';
import { wrap } from '~/redux/auth/sagas';
import { import {
messagesDeleteMessage, messagesDeleteMessage,
messagesGetMessages, messagesGetMessages,
@ -25,6 +20,7 @@ import { selectMessages } from '~/redux/messages/selectors';
import { sortCreatedAtDesc } from '~/utils/date'; import { sortCreatedAtDesc } from '~/utils/date';
function* getMessages({ username }: ReturnType<typeof messagesGetMessages>) { function* getMessages({ username }: ReturnType<typeof messagesGetMessages>) {
try {
const { messages }: ReturnType<typeof selectMessages> = yield select(selectMessages); const { messages }: ReturnType<typeof selectMessages> = yield select(selectMessages);
yield put( yield put(
@ -39,20 +35,9 @@ function* getMessages({ username }: ReturnType<typeof messagesGetMessages>) {
}) })
); );
const { error, data }: Unwrap<typeof apiMessagesGetUserMessages> = yield call( const data: Unwrap<typeof apiGetUserMessages> = yield call(apiGetUserMessages, {
wrap, username,
apiMessagesGetUserMessages, });
{ username }
);
if (error || !data.messages) {
return yield put(
messagesSet({
is_loading_messages: false,
error: ERRORS.EMPTY_RESPONSE,
})
);
}
yield put(messagesSet({ is_loading_messages: false, messages: data.messages })); yield put(messagesSet({ is_loading_messages: false, messages: data.messages }));
@ -62,50 +47,49 @@ function* getMessages({ username }: ReturnType<typeof messagesGetMessages>) {
const filtered = notifications.filter( const filtered = notifications.filter(
notification => notification =>
notification.type !== 'message' || notification.type !== 'message' ||
(notification as IMessageNotification).content.from.username !== username (notification as IMessageNotification)?.content?.from?.username !== username
); );
if (filtered.length !== notifications.length) { if (filtered.length !== notifications.length) {
yield put(authSetUpdates({ notifications: filtered })); yield put(authSetUpdates({ notifications: filtered }));
} }
} catch (error) {
messagesSet({
error: error || ERRORS.EMPTY_RESPONSE,
});
} finally {
yield put(
messagesSet({
is_loading_messages: false,
})
);
}
} }
function* sendMessage({ message, onSuccess }: ReturnType<typeof messagesSendMessage>) { function* sendMessage({ message, onSuccess }: ReturnType<typeof messagesSendMessage>) {
try {
const username: ReturnType<typeof selectAuthProfileUsername> = yield select( const username: ReturnType<typeof selectAuthProfileUsername> = yield select(
selectAuthProfileUsername selectAuthProfileUsername
); );
if (!username) return; if (!username) return;
yield put(messagesSet({ is_sending_messages: true, error: null })); yield put(messagesSet({ is_sending_messages: true, error: '' }));
const { error, data }: Unwrap<typeof apiMessagesSendMessage> = yield call( const data: Unwrap<typeof apiSendMessage> = yield call(apiSendMessage, {
wrap,
apiMessagesSendMessage,
{
username, username,
message, message,
} });
);
if (error || !data.message) {
return yield put(
messagesSet({
is_sending_messages: false,
error: error || ERRORS.EMPTY_RESPONSE,
})
);
}
const { user }: ReturnType<typeof selectAuthProfile> = yield select(selectAuthProfile); const { user }: ReturnType<typeof selectAuthProfile> = yield select(selectAuthProfile);
if (user.username !== username) { if (user?.username !== username) {
return yield put(messagesSet({ is_sending_messages: false })); return yield put(messagesSet({ is_sending_messages: false }));
} }
const { messages }: ReturnType<typeof selectMessages> = yield select(selectMessages); const { messages }: ReturnType<typeof selectMessages> = yield select(selectMessages);
if (message.id > 0) { if (message.id && message.id > 0) {
// modified // modified
yield put( yield put(
messagesSet({ messagesSet({
@ -124,34 +108,34 @@ function* sendMessage({ message, onSuccess }: ReturnType<typeof messagesSendMess
} }
onSuccess(); onSuccess();
} catch (error) {
messagesSet({
error: error || ERRORS.EMPTY_RESPONSE,
});
} finally {
yield put(
messagesSet({
is_loading_messages: false,
})
);
}
} }
function* deleteMessage({ id, is_locked }: ReturnType<typeof messagesDeleteMessage>) { function* deleteMessage({ id, is_locked }: ReturnType<typeof messagesDeleteMessage>) {
try {
const username: ReturnType<typeof selectAuthProfileUsername> = yield select( const username: ReturnType<typeof selectAuthProfileUsername> = yield select(
selectAuthProfileUsername selectAuthProfileUsername
); );
if (!username) return; if (!username) return;
yield put(messagesSet({ is_sending_messages: true, error: null })); yield put(messagesSet({ is_sending_messages: true, error: '' }));
const { error, data }: Unwrap<typeof apiMessagesDeleteMessage> = yield call( const data: Unwrap<typeof apiDeleteMessage> = yield call(apiDeleteMessage, {
wrap,
apiMessagesDeleteMessage,
{
username, username,
id, id,
is_locked, is_locked,
} });
);
if (error || !data.message) {
return yield put(
messagesSet({
is_sending_messages: false,
})
);
}
const currentUsername: ReturnType<typeof selectAuthProfileUsername> = yield select( const currentUsername: ReturnType<typeof selectAuthProfileUsername> = yield select(
selectAuthProfileUsername selectAuthProfileUsername
@ -169,9 +153,21 @@ function* deleteMessage({ id, is_locked }: ReturnType<typeof messagesDeleteMessa
messages: messages.map(item => (item.id === id ? data.message : item)), messages: messages.map(item => (item.id === id ? data.message : item)),
}) })
); );
} catch (error) {
messagesSet({
error: error || ERRORS.EMPTY_RESPONSE,
});
} finally {
yield put(
messagesSet({
is_loading_messages: false,
})
);
}
} }
function* refreshMessages({}: ReturnType<typeof messagesRefreshMessages>) { function* refreshMessages({}: ReturnType<typeof messagesRefreshMessages>) {
try {
const username: ReturnType<typeof selectAuthProfileUsername> = yield select( const username: ReturnType<typeof selectAuthProfileUsername> = yield select(
selectAuthProfileUsername selectAuthProfileUsername
); );
@ -184,26 +180,28 @@ function* refreshMessages({}: ReturnType<typeof messagesRefreshMessages>) {
const after = messages.length > 0 ? messages[0].created_at : undefined; const after = messages.length > 0 ? messages[0].created_at : undefined;
const { data, error }: Unwrap<typeof apiMessagesGetUserMessages> = yield call( const data: Unwrap<typeof apiGetUserMessages> = yield call(apiGetUserMessages, {
wrap, username,
apiMessagesGetUserMessages, after,
{ username, after } });
);
yield put(messagesSet({ is_loading_messages: false })); yield put(messagesSet({ is_loading_messages: false }));
if (error) {
return yield put(
messagesSet({
error: error || ERRORS.EMPTY_RESPONSE,
})
);
}
if (!data.messages || !data.messages.length) return; if (!data.messages || !data.messages.length) return;
const newMessages = [...data.messages, ...messages].sort(sortCreatedAtDesc); const newMessages = [...data.messages, ...messages].sort(sortCreatedAtDesc);
yield put(messagesSet({ messages: newMessages })); yield put(messagesSet({ messages: newMessages }));
} catch (error) {
messagesSet({
error: error || ERRORS.EMPTY_RESPONSE,
});
} finally {
yield put(
messagesSet({
is_loading_messages: false,
})
);
}
} }
export default function*() { export default function*() {

View file

@ -0,0 +1,26 @@
import { IMessage } from '~/redux/types';
export type ApiGetUserMessagesRequest = {
username: string;
after?: string;
before?: string;
};
export type ApiGetUserMessagesResponse = { messages: IMessage[] };
export type ApiSendMessageRequest = {
username: string;
message: Partial<IMessage>;
};
export type ApiSendMessageResult = {
message: IMessage;
};
export type ApiDeleteMessageRequest = {
username: string;
id: number;
is_locked: boolean;
};
export type ApiDeleteMessageResult = {
message: IMessage;
};