1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-25 21:06:42 +07:00

moved messages to separate reducer

This commit is contained in:
Fedor Katurov 2020-09-08 10:58:55 +07:00
parent bc09810802
commit 737a4396de
14 changed files with 212 additions and 153 deletions

View file

@ -1,6 +1,6 @@
import { AUTH_USER_ACTIONS } from '~/redux/auth/constants';
import { IAuthState, ISocialProvider, IUser } from '~/redux/auth/types';
import { IMessage, IOAuthEvent } from '../types';
import { IOAuthEvent } from '../types';
export const userSendLoginRequest = ({
username,
@ -54,22 +54,6 @@ export const authSetProfile = (profile: Partial<IAuthState['profile']>) => ({
profile,
});
export const authGetMessages = (username: string) => ({
type: AUTH_USER_ACTIONS.GET_MESSAGES,
username,
});
export const authSendMessage = (message: Partial<IMessage>, onSuccess) => ({
type: AUTH_USER_ACTIONS.SEND_MESSAGE,
message,
onSuccess,
});
export const authDeleteMessage = (id: IMessage['id']) => ({
type: AUTH_USER_ACTIONS.DELETE_MESSAGE,
id,
});
export const authSetUpdates = (updates: Partial<IAuthState['updates']>) => ({
type: AUTH_USER_ACTIONS.SET_UPDATES,
updates,

View file

@ -13,10 +13,6 @@ export const AUTH_USER_ACTIONS = {
OPEN_PROFILE: 'OPEN_PROFILE',
LOAD_PROFILE: 'LOAD_PROFILE',
SET_PROFILE: 'SET_PROFILE',
GET_MESSAGES: 'GET_MESSAGES',
SEND_MESSAGE: 'SEND_MESSAGE',
DELETE_MESSAGE: 'DELETE_MESSAGE',
SET_UPDATES: 'SET_UPDATES',
SET_LAST_SEEN_MESSAGES: 'SET_LAST_SEEN_MESSAGES',

View file

@ -26,11 +26,8 @@ const INITIAL_STATE: IAuthState = {
profile: {
tab: 'profile',
is_loading: true,
is_loading_messages: true,
is_sending_messages: false,
user: null,
messages: [],
messages_error: null,
patch_errors: {},
socials: {

View file

@ -3,7 +3,6 @@ import { AUTH_USER_ACTIONS, EMPTY_USER, USER_ERRORS, USER_ROLES } from '~/redux/
import {
authAttachSocial,
authDropSocial,
authGetMessages,
authGotOauthLoginEvent,
authLoadProfile,
authLoggedIn,
@ -12,7 +11,6 @@ import {
authPatchUser,
authRequestRestoreCode,
authRestorePassword,
authSendMessage,
authSendRegisterSocial,
authSetLastSeenMessages,
authSetProfile,
@ -32,9 +30,7 @@ import {
apiAttachSocial,
apiAuthGetUpdates,
apiAuthGetUser,
apiAuthGetUserMessages,
apiAuthGetUserProfile,
apiAuthSendMessage,
apiCheckRestoreCode,
apiDropSocial,
apiGetSocials,
@ -54,13 +50,14 @@ import {
selectAuthUser,
selectToken,
} from './selectors';
import { IMessageNotification, IResultWithStatus, OAUTH_EVENT_TYPES, Unwrap } from '../types';
import { IResultWithStatus, OAUTH_EVENT_TYPES, Unwrap } from '../types';
import { IAuthState, IUser } from './types';
import { REHYDRATE, RehydrateAction } from 'redux-persist';
import { selectModal } from '~/redux/modal/selectors';
import { IModalState } from '~/redux/modal';
import { DIALOGS } from '~/redux/modal/constants';
import { ERRORS } from '~/constants/errors';
import { messagesSetMessages } from '~/redux/messages/actions';
export function* reqWrapper(requestAction, props = {}): ReturnType<typeof requestAction> {
const access = yield select(selectToken);
@ -157,7 +154,8 @@ function* loadProfile({ username }: ReturnType<typeof authLoadProfile>) {
return false;
}
yield put(authSetProfile({ is_loading: false, user, messages: [] }));
yield put(authSetProfile({ is_loading: false, user }));
yield put(messagesSetMessages({ messages: [] }));
return true;
}
@ -172,94 +170,6 @@ function* openProfile({ username, tab = 'profile' }: ReturnType<typeof authOpenP
}
}
function* getMessages({ username }: ReturnType<typeof authGetMessages>) {
// yield put(modalShowDialog(DIALOGS.PROFILE));
const { messages } = yield select(selectAuthProfile);
yield put(
authSetProfile({
is_loading_messages: true,
messages:
messages &&
messages.length > 0 &&
(messages[0].to.username === username || messages[0].from.username === username)
? messages
: [],
})
);
const {
error,
data,
// data: { messages },
} = yield call(reqWrapper, apiAuthGetUserMessages, { username });
if (error || !data.messages) {
return yield put(
authSetProfile({
is_loading_messages: false,
messages_error: ERRORS.EMPTY_RESPONSE,
})
);
}
yield put(authSetProfile({ is_loading_messages: false, messages: data.messages }));
const { notifications } = yield select(selectAuthUpdates);
// clear viewed message from notifcation list
const filtered = notifications.filter(
notification =>
notification.type !== 'message' ||
(notification as IMessageNotification).content.from.username !== username
);
if (filtered.length !== notifications.length) {
yield put(authSetUpdates({ notifications: filtered }));
}
}
function* sendMessage({ message, onSuccess }: ReturnType<typeof authSendMessage>) {
const {
user: { username },
} = yield select(selectAuthProfile);
if (!username) return;
yield put(authSetProfile({ is_sending_messages: true, messages_error: null }));
const { error, data } = yield call(reqWrapper, apiAuthSendMessage, {
username,
message,
});
console.log({ error, data });
if (error || !data.message) {
return yield put(
authSetProfile({
is_sending_messages: false,
messages_error: error || ERRORS.EMPTY_RESPONSE,
})
);
}
const { user, messages } = yield select(selectAuthProfile);
if (user.username !== username) {
return yield put(authSetProfile({ is_sending_messages: false }));
}
yield put(
authSetProfile({
is_sending_messages: false,
messages: [data.message, ...messages],
})
);
onSuccess();
}
function* getUpdates() {
const user: ReturnType<typeof selectAuthUser> = yield select(selectAuthUser);
@ -543,8 +453,6 @@ function* authSaga() {
yield takeLatest(AUTH_USER_ACTIONS.GOT_AUTH_POST_MESSAGE, gotPostMessageSaga);
yield takeLatest(AUTH_USER_ACTIONS.OPEN_PROFILE, openProfile);
yield takeLatest(AUTH_USER_ACTIONS.LOAD_PROFILE, loadProfile);
yield takeLatest(AUTH_USER_ACTIONS.GET_MESSAGES, getMessages);
yield takeLatest(AUTH_USER_ACTIONS.SEND_MESSAGE, sendMessage);
yield takeLatest(AUTH_USER_ACTIONS.SET_LAST_SEEN_MESSAGES, setLastSeenMessages);
yield takeLatest(AUTH_USER_ACTIONS.PATCH_USER, patchUser);
yield takeLatest(AUTH_USER_ACTIONS.REQUEST_RESTORE_CODE, requestRestoreCode);

View file

@ -1,4 +1,4 @@
import { IFile, IMessage, INotification } from '../types';
import { IFile, INotification } from '../types';
export interface IToken {
access: string;
@ -52,12 +52,8 @@ export type IAuthState = Readonly<{
profile: {
tab: 'profile' | 'messages' | 'settings';
is_loading: boolean;
is_loading_messages: boolean;
is_sending_messages: boolean;
user: IUser;
messages: IMessage[];
messages_error: string;
patch_errors: Record<string, string>;
socials: {