mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 21:06:42 +07:00
attaching accounts and displaying errors
This commit is contained in:
parent
3ae22fb63d
commit
b1c71faf3a
9 changed files with 140 additions and 18 deletions
|
@ -1,6 +1,6 @@
|
|||
import { api, errorMiddleware, resultMiddleware, configWithToken } from '~/utils/api';
|
||||
import { api, configWithToken, errorMiddleware, resultMiddleware } from '~/utils/api';
|
||||
import { API } from '~/constants/api';
|
||||
import { IResultWithStatus, IMessage, INotification } from '~/redux/types';
|
||||
import { IMessage, INotification, IResultWithStatus } from '~/redux/types';
|
||||
import { userLoginTransform } from '~/redux/auth/transforms';
|
||||
import { ISocialAccount, IUser } from './types';
|
||||
|
||||
|
@ -115,3 +115,17 @@ export const apiDropSocial = ({
|
|||
.delete(API.USER.DROP_SOCIAL(provider, id), configWithToken(access))
|
||||
.then(resultMiddleware)
|
||||
.catch(errorMiddleware);
|
||||
|
||||
export const apiAttachSocial = ({
|
||||
access,
|
||||
token,
|
||||
}: {
|
||||
access: string;
|
||||
token: string;
|
||||
}): Promise<IResultWithStatus<{
|
||||
account: ISocialAccount;
|
||||
}>> =>
|
||||
api
|
||||
.post(API.USER.ATTACH_SOCIAL, { token }, configWithToken(access))
|
||||
.then(resultMiddleware)
|
||||
.catch(errorMiddleware);
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import { call, delay, put, select, takeEvery, takeLatest } from 'redux-saga/effects';
|
||||
import { AUTH_USER_ACTIONS, EMPTY_USER, USER_ERRORS, USER_ROLES } from '~/redux/auth/constants';
|
||||
import {
|
||||
authAttachSocial,
|
||||
authDropSocial,
|
||||
authGetMessages,
|
||||
authGetSocials,
|
||||
authLoadProfile,
|
||||
authLoggedIn,
|
||||
authOpenProfile,
|
||||
|
@ -24,6 +24,7 @@ import {
|
|||
userSetLoginError,
|
||||
} from '~/redux/auth/actions';
|
||||
import {
|
||||
apiAttachSocial,
|
||||
apiAuthGetUpdates,
|
||||
apiAuthGetUser,
|
||||
apiAuthGetUserMessages,
|
||||
|
@ -412,7 +413,37 @@ function* dropSocial({ provider, id }: ReturnType<typeof authDropSocial>) {
|
|||
|
||||
yield call(getSocials);
|
||||
} catch (e) {
|
||||
yield put(authSetSocials({ error: e.toString() }));
|
||||
yield put(authSetSocials({ error: e.message }));
|
||||
}
|
||||
}
|
||||
|
||||
function* attachSocial({ token }: ReturnType<typeof authAttachSocial>) {
|
||||
if (!token) return;
|
||||
|
||||
try {
|
||||
yield put(authSetSocials({ error: '', is_loading: true }));
|
||||
|
||||
const { data, error }: Unwrap<ReturnType<typeof apiAttachSocial>> = yield call(
|
||||
reqWrapper,
|
||||
apiAttachSocial,
|
||||
{ token }
|
||||
);
|
||||
|
||||
if (error) {
|
||||
throw new Error(error);
|
||||
}
|
||||
|
||||
const {
|
||||
socials: { accounts },
|
||||
}: ReturnType<typeof selectAuthProfile> = yield select(selectAuthProfile);
|
||||
|
||||
if (accounts.some(it => it.id === data.account.id && it.provider === data.account.provider)) {
|
||||
yield put(authSetSocials({ is_loading: false }));
|
||||
} else {
|
||||
yield put(authSetSocials({ is_loading: false, accounts: [...accounts, data.account] }));
|
||||
}
|
||||
} catch (e) {
|
||||
yield put(authSetSocials({ is_loading: false, error: e.message }));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -434,6 +465,7 @@ function* authSaga() {
|
|||
yield takeLatest(AUTH_USER_ACTIONS.RESTORE_PASSWORD, restorePassword);
|
||||
yield takeLatest(AUTH_USER_ACTIONS.GET_SOCIALS, getSocials);
|
||||
yield takeLatest(AUTH_USER_ACTIONS.DROP_SOCIAL, dropSocial);
|
||||
yield takeLatest(AUTH_USER_ACTIONS.ATTACH_SOCIAL, attachSocial);
|
||||
}
|
||||
|
||||
export default authSaga;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue