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

messages form

This commit is contained in:
Fedor Katurov 2019-11-12 14:38:25 +07:00
parent e45dee3c9f
commit 98c66dec8c
24 changed files with 456 additions and 42 deletions

View file

@ -9,15 +9,17 @@ import {
authOpenProfile,
authSetProfile,
authGetMessages,
authSendMessage,
} from '~/redux/auth/actions';
import {
apiUserLogin,
apiAuthGetUser,
apiAuthGetUserProfile,
apiAuthGetUserMessages,
apiAuthSendMessage,
} from '~/redux/auth/api';
import { modalSetShown, modalShowDialog } from '~/redux/modal/actions';
import { selectToken } from './selectors';
import { selectToken, selectAuthProfile } from './selectors';
import { IResultWithStatus } from '../types';
import { IUser } from './types';
import { REHYDRATE, RehydrateAction } from 'redux-persist';
@ -128,7 +130,7 @@ function* getMessages({ username }: ReturnType<typeof authGetMessages>) {
return yield put(
authSetProfile({
is_loading_messages: false,
messages_errors: { network: ERRORS.EMPTY_RESPONSE },
messages_error: ERRORS.EMPTY_RESPONSE,
})
);
}
@ -136,6 +138,44 @@ function* getMessages({ username }: ReturnType<typeof authGetMessages>) {
yield put(authSetProfile({ is_loading_messages: false, messages }));
}
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: [message, ...messages],
})
);
onSuccess();
}
function* authSaga() {
yield takeLatest(REHYDRATE, checkUserSaga);
yield takeLatest(AUTH_USER_ACTIONS.LOGOUT, logoutSaga);
@ -143,6 +183,7 @@ 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.GET_MESSAGES, getMessages);
yield takeLatest(AUTH_USER_ACTIONS.SEND_MESSAGE, sendMessage);
}
export default authSaga;