mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 21:06:42 +07:00
fetching messages for user
This commit is contained in:
parent
54687b131c
commit
e45dee3c9f
12 changed files with 103 additions and 17 deletions
|
@ -42,3 +42,8 @@ export const authSetProfile = (profile: Partial<IAuthState['profile']>) => ({
|
|||
type: AUTH_USER_ACTIONS.SET_PROFILE,
|
||||
profile,
|
||||
});
|
||||
|
||||
export const authGetMessages = (username: string) => ({
|
||||
type: AUTH_USER_ACTIONS.GET_MESSAGES,
|
||||
username,
|
||||
});
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { api, errorMiddleware, resultMiddleware, configWithToken } from '~/utils/api';
|
||||
import { API } from '~/constants/api';
|
||||
import { IResultWithStatus } from '~/redux/types';
|
||||
import { IResultWithStatus, IMessage } from '~/redux/types';
|
||||
import { userLoginTransform } from '~/redux/auth/transforms';
|
||||
import { IUser } from './types';
|
||||
|
||||
|
@ -31,3 +31,12 @@ export const apiAuthGetUserProfile = ({
|
|||
.get(API.USER.PROFILE(username), configWithToken(access))
|
||||
.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);
|
||||
|
|
|
@ -11,6 +11,7 @@ export const AUTH_USER_ACTIONS = {
|
|||
GOT_AUTH_POST_MESSAGE: 'GOT_POST_MESSAGE',
|
||||
OPEN_PROFILE: 'OPEN_PROFILE',
|
||||
SET_PROFILE: 'SET_PROFILE',
|
||||
GET_MESSAGES: 'GET_MESSAGES',
|
||||
};
|
||||
|
||||
export const USER_ERRORS = {
|
||||
|
|
|
@ -18,7 +18,10 @@ const INITIAL_STATE: IAuthState = {
|
|||
|
||||
profile: {
|
||||
is_loading: true,
|
||||
is_loading_messages: true,
|
||||
user: null,
|
||||
messages: [],
|
||||
messages_errors: {},
|
||||
},
|
||||
};
|
||||
|
||||
|
|
|
@ -8,8 +8,14 @@ import {
|
|||
gotAuthPostMessage,
|
||||
authOpenProfile,
|
||||
authSetProfile,
|
||||
authGetMessages,
|
||||
} from '~/redux/auth/actions';
|
||||
import { apiUserLogin, apiAuthGetUser, apiAuthGetUserProfile } from '~/redux/auth/api';
|
||||
import {
|
||||
apiUserLogin,
|
||||
apiAuthGetUser,
|
||||
apiAuthGetUserProfile,
|
||||
apiAuthGetUserMessages,
|
||||
} from '~/redux/auth/api';
|
||||
import { modalSetShown, modalShowDialog } from '~/redux/modal/actions';
|
||||
import { selectToken } from './selectors';
|
||||
import { IResultWithStatus } from '../types';
|
||||
|
@ -18,6 +24,7 @@ import { REHYDRATE, RehydrateAction } from 'redux-persist';
|
|||
import { selectModal } from '../modal/selectors';
|
||||
import { IModalState } from '../modal/reducer';
|
||||
import { DIALOGS } from '../modal/constants';
|
||||
import { ERRORS } from '~/constants/errors';
|
||||
|
||||
export function* reqWrapper(requestAction, props = {}): ReturnType<typeof requestAction> {
|
||||
const access = yield select(selectToken);
|
||||
|
@ -75,6 +82,7 @@ function* refreshUser() {
|
|||
function* checkUserSaga({ key }: RehydrateAction) {
|
||||
if (key !== 'auth') return;
|
||||
yield call(refreshUser);
|
||||
yield put(authOpenProfile('gvorcek'));
|
||||
}
|
||||
|
||||
function* gotPostMessageSaga({ token }: ReturnType<typeof gotAuthPostMessage>) {
|
||||
|
@ -107,12 +115,34 @@ function* openProfile({ username }: ReturnType<typeof authOpenProfile>) {
|
|||
yield put(authSetProfile({ is_loading: false, user }));
|
||||
}
|
||||
|
||||
function* getMessages({ username }: ReturnType<typeof authGetMessages>) {
|
||||
yield put(modalShowDialog(DIALOGS.PROFILE));
|
||||
yield put(authSetProfile({ is_loading_messages: true }));
|
||||
|
||||
const {
|
||||
error,
|
||||
data: { messages },
|
||||
} = yield call(reqWrapper, apiAuthGetUserMessages, { username });
|
||||
|
||||
if (error || !messages) {
|
||||
return yield put(
|
||||
authSetProfile({
|
||||
is_loading_messages: false,
|
||||
messages_errors: { network: ERRORS.EMPTY_RESPONSE },
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
yield put(authSetProfile({ is_loading_messages: false, messages }));
|
||||
}
|
||||
|
||||
function* authSaga() {
|
||||
yield takeLatest(REHYDRATE, checkUserSaga);
|
||||
yield takeLatest(AUTH_USER_ACTIONS.LOGOUT, logoutSaga);
|
||||
yield takeLatest(AUTH_USER_ACTIONS.SEND_LOGIN_REQUEST, sendLoginRequestSaga);
|
||||
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);
|
||||
}
|
||||
|
||||
export default authSaga;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { IFile } from '../types';
|
||||
import { IFile, IMessage } from '../types';
|
||||
|
||||
export interface IToken {
|
||||
access: string;
|
||||
|
@ -31,6 +31,9 @@ export type IAuthState = Readonly<{
|
|||
|
||||
profile: {
|
||||
is_loading: boolean;
|
||||
is_loading_messages: boolean;
|
||||
user: IUser;
|
||||
messages: IMessage[];
|
||||
messages_errors: Record<string, string>;
|
||||
};
|
||||
}>;
|
||||
|
|
|
@ -146,6 +146,11 @@ export interface IComment {
|
|||
update_at?: string;
|
||||
}
|
||||
|
||||
export type IMessage = Omit<IComment, 'user' | 'node'> & {
|
||||
from: IUser;
|
||||
to: IUser;
|
||||
};
|
||||
|
||||
export interface ICommentGroup {
|
||||
user: IUser;
|
||||
comments: IComment[];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue