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

fixed profile patching

This commit is contained in:
Fedor Katurov 2019-11-19 15:56:45 +07:00
parent f25d627b4e
commit 60dffc2353
6 changed files with 209 additions and 60 deletions

View file

@ -1,12 +1,17 @@
import { api, errorMiddleware, resultMiddleware, configWithToken } from '~/utils/api';
import { API } from '~/constants/api';
import { IResultWithStatus, IMessage } from '~/redux/types';
import { userLoginTransform } from '~/redux/auth/transforms';
import { IUser } from './types';
import {
api,
errorMiddleware,
resultMiddleware,
configWithToken
} from "~/utils/api";
import { API } from "~/constants/api";
import { IResultWithStatus, IMessage } from "~/redux/types";
import { userLoginTransform } from "~/redux/auth/transforms";
import { IUser } from "./types";
export const apiUserLogin = ({
username,
password,
password
}: {
username: string;
password: string;
@ -17,7 +22,9 @@ export const apiUserLogin = ({
.catch(errorMiddleware)
.then(userLoginTransform);
export const apiAuthGetUser = ({ access }): Promise<IResultWithStatus<{ user: IUser }>> =>
export const apiAuthGetUser = ({
access
}): Promise<IResultWithStatus<{ user: IUser }>> =>
api
.get(API.USER.ME, configWithToken(access))
.then(resultMiddleware)
@ -25,7 +32,7 @@ export const apiAuthGetUser = ({ access }): Promise<IResultWithStatus<{ user: IU
export const apiAuthGetUserProfile = ({
access,
username,
username
}): Promise<IResultWithStatus<{ user: IUser }>> =>
api
.get(API.USER.PROFILE(username), configWithToken(access))
@ -34,7 +41,7 @@ export const apiAuthGetUserProfile = ({
export const apiAuthGetUserMessages = ({
access,
username,
username
}): Promise<IResultWithStatus<{ messages: IMessage[] }>> =>
api
.get(API.USER.MESSAGES(username), configWithToken(access))
@ -44,7 +51,7 @@ export const apiAuthGetUserMessages = ({
export const apiAuthSendMessage = ({
access,
username,
message,
message
}): Promise<IResultWithStatus<{ message: IMessage }>> =>
api
.post(API.USER.MESSAGE_SEND(username), { message }, configWithToken(access))
@ -54,14 +61,20 @@ export const apiAuthSendMessage = ({
export const apiAuthGetUpdates = ({
access,
exclude_dialogs,
last,
last
}): Promise<IResultWithStatus<{ message: IMessage }>> =>
api
.get(API.USER.GET_UPDATES, configWithToken(access, { params: { exclude_dialogs, last } }))
.get(
API.USER.GET_UPDATES,
configWithToken(access, { params: { exclude_dialogs, last } })
)
.then(resultMiddleware)
.catch(errorMiddleware);
export const apiUpdateUser = ({ access, user }): Promise<IResultWithStatus<{ user: IUser }>> =>
export const apiUpdateUser = ({
access,
user
}): Promise<IResultWithStatus<{ user: IUser }>> =>
api
.patch(API.USER.ME, { user }, configWithToken(access))
.then(resultMiddleware)

View file

@ -1,4 +1,11 @@
import { call, put, takeLatest, select, delay } from "redux-saga/effects";
import {
call,
put,
takeEvery,
takeLatest,
select,
delay
} from "redux-saga/effects";
import {
AUTH_USER_ACTIONS,
EMPTY_USER,
@ -92,26 +99,32 @@ function* sendLoginRequestSaga({
}
function* refreshUser() {
const {
error,
data: { user }
}: IResultWithStatus<{ user: IUser }> = yield call(
reqWrapper,
apiAuthGetUser
);
if (error) {
yield put(
authSetUser({
...EMPTY_USER,
is_user: false
})
try {
const {
error,
data: { user }
}: IResultWithStatus<{ user: IUser }> = yield call(
reqWrapper,
apiAuthGetUser
);
return;
}
if (error) {
yield put(
authSetUser({
...EMPTY_USER,
is_user: false
})
);
yield put(authSetUser({ ...user, is_user: true }));
return;
}
yield put(authSetUser({ ...user, is_user: true }));
} catch (e) {
console.log("erro", e);
} finally {
console.log("ended");
}
}
function* checkUserSaga({ key }: RehydrateAction) {
@ -315,12 +328,14 @@ function* patchUser({ user }: ReturnType<typeof authPatchUser>) {
return yield put(authSetProfile({ patch_errors: data.errors }));
}
yield put(authSetUser(data.user));
console.log({ me, data });
yield put(authSetUser({ ...me, ...data.user }));
yield put(authSetProfile({ user: { ...me, ...data.user }, tab: "profile" }));
}
function* authSaga() {
yield takeLatest(REHYDRATE, checkUserSaga);
yield takeEvery(REHYDRATE, checkUserSaga);
yield takeLatest([REHYDRATE, AUTH_USER_ACTIONS.LOGGED_IN], startPollingSaga);
yield takeLatest(AUTH_USER_ACTIONS.LOGOUT, logoutSaga);