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

code restore process finished

This commit is contained in:
Fedor Katurov 2019-11-25 17:05:42 +07:00
parent 078c531e93
commit e227c9660e
6 changed files with 61 additions and 18 deletions

View file

@ -44,7 +44,6 @@ const Component: FC<IProps> = ({ modal: { is_shown } }) => {
<Route path={URLS.NODE_URL(':id')} component={NodeLayout} /> <Route path={URLS.NODE_URL(':id')} component={NodeLayout} />
<Route path={URLS.BORIS} component={BorisLayout} /> <Route path={URLS.BORIS} component={BorisLayout} />
<Route path={URLS.ERRORS.NOT_FOUND} component={ErrorNotFound} /> <Route path={URLS.ERRORS.NOT_FOUND} component={ErrorNotFound} />
<Route path="/restore/:code" component={ErrorNotFound} />
<Redirect to="/" /> <Redirect to="/" />
</Switch> </Switch>

View file

@ -18,7 +18,7 @@ const mapStateToProps = state => ({
restore: selectAuthRestore(state), restore: selectAuthRestore(state),
}); });
const mapDispatchToProps = pick(['authRequestRestoreCode', 'authSetRestore'], AUTH_ACTIONS); const mapDispatchToProps = pick(['authRestorePassword', 'authSetRestore'], AUTH_ACTIONS);
type IProps = IDialogProps & ReturnType<typeof mapStateToProps> & typeof mapDispatchToProps & {}; type IProps = IDialogProps & ReturnType<typeof mapStateToProps> & typeof mapDispatchToProps & {};
@ -26,7 +26,7 @@ const RestorePasswordDialogUnconnected: FC<IProps> = ({
restore: { error, is_loading, is_succesfull, user }, restore: { error, is_loading, is_succesfull, user },
authSetRestore, authSetRestore,
onRequestClose, onRequestClose,
authRequestRestoreCode, authRestorePassword,
}) => { }) => {
const [password, setPassword] = useState(''); const [password, setPassword] = useState('');
const [password_again, setPasswordAgain] = useState(''); const [password_again, setPasswordAgain] = useState('');
@ -41,11 +41,10 @@ const RestorePasswordDialogUnconnected: FC<IProps> = ({
event.preventDefault(); event.preventDefault();
if (doesnt_match) return; if (doesnt_match) return;
// if (!field) return;
// authRestorePassword(password);
// authRequestRestoreCode(field);
}, },
[doesnt_match] [doesnt_match, authRestorePassword]
); );
useEffect(() => { useEffect(() => {
@ -69,7 +68,8 @@ const RestorePasswordDialogUnconnected: FC<IProps> = ({
<Group className={styles.shade}> <Group className={styles.shade}>
<Icon icon="check" size={64} /> <Icon icon="check" size={64} />
<div>Отлично, добро пожаловать домой!</div> <div>Пароль обновлен</div>
<div>Добро пожаловать домой, ~{user.username}!</div>
<div /> <div />

View file

@ -91,3 +91,8 @@ export const authShowRestoreModal = (code: string) => ({
type: AUTH_USER_ACTIONS.SHOW_RESTORE_MODAL, type: AUTH_USER_ACTIONS.SHOW_RESTORE_MODAL,
code, code,
}); });
export const authRestorePassword = (password: string) => ({
type: AUTH_USER_ACTIONS.RESTORE_PASSWORD,
password,
});

View file

@ -78,3 +78,9 @@ export const apiCheckRestoreCode = ({ code }): Promise<IResultWithStatus<{}>> =>
.get(API.USER.REQUEST_CODE(code)) .get(API.USER.REQUEST_CODE(code))
.then(resultMiddleware) .then(resultMiddleware)
.catch(errorMiddleware); .catch(errorMiddleware);
export const apiRestoreCode = ({ code, password }): Promise<IResultWithStatus<{}>> =>
api
.post(API.USER.REQUEST_CODE(code), { password })
.then(resultMiddleware)
.catch(errorMiddleware);

View file

@ -22,6 +22,7 @@ export const AUTH_USER_ACTIONS = {
SET_RESTORE: 'SET_RESTORE', SET_RESTORE: 'SET_RESTORE',
REQUEST_RESTORE_CODE: 'REQUEST_RESTORE_CODE', REQUEST_RESTORE_CODE: 'REQUEST_RESTORE_CODE',
SHOW_RESTORE_MODAL: 'SHOW_RESTORE_MODAL', SHOW_RESTORE_MODAL: 'SHOW_RESTORE_MODAL',
RESTORE_PASSWORD: 'RESTORE_PASSWORD',
}; };
export const USER_ERRORS = { export const USER_ERRORS = {

View file

@ -17,6 +17,7 @@ import {
authShowRestoreModal, authShowRestoreModal,
authSetRestore, authSetRestore,
authRequestRestoreCode, authRequestRestoreCode,
authRestorePassword,
} from '~/redux/auth/actions'; } from '~/redux/auth/actions';
import { import {
apiUserLogin, apiUserLogin,
@ -28,9 +29,16 @@ import {
apiUpdateUser, apiUpdateUser,
apiRequestRestoreCode, apiRequestRestoreCode,
apiCheckRestoreCode, apiCheckRestoreCode,
apiRestoreCode,
} from '~/redux/auth/api'; } from '~/redux/auth/api';
import { modalSetShown, modalShowDialog } from '~/redux/modal/actions'; import { modalSetShown, modalShowDialog } from '~/redux/modal/actions';
import { selectToken, selectAuthProfile, selectAuthUser, selectAuthUpdates } from './selectors'; import {
selectToken,
selectAuthProfile,
selectAuthUser,
selectAuthUpdates,
selectAuthRestore,
} from './selectors';
import { IResultWithStatus, INotification, IMessageNotification } from '../types'; import { IResultWithStatus, INotification, IMessageNotification } from '../types';
import { IUser, IAuthState } from './types'; import { IUser, IAuthState } from './types';
import { REHYDRATE, RehydrateAction } from 'redux-persist'; import { REHYDRATE, RehydrateAction } from 'redux-persist';
@ -293,12 +301,7 @@ function* requestRestoreCode({ field }: ReturnType<typeof authRequestRestoreCode
function* showRestoreModal({ code }: ReturnType<typeof authShowRestoreModal>) { function* showRestoreModal({ code }: ReturnType<typeof authShowRestoreModal>) {
if (!code && !code.length) { if (!code && !code.length) {
return yield put( return yield put(authSetRestore({ error: ERRORS.CODE_IS_INVALID, is_loading: false }));
authSetRestore({
error: ERRORS.CODE_IS_INVALID,
is_loading: false,
})
);
} }
yield put(authSetRestore({ user: null, is_loading: true })); yield put(authSetRestore({ user: null, is_loading: true }));
@ -306,13 +309,41 @@ function* showRestoreModal({ code }: ReturnType<typeof authShowRestoreModal>) {
const { error, data } = yield call(apiCheckRestoreCode, { code }); const { error, data } = yield call(apiCheckRestoreCode, { code });
if (data.error || error || !data.user) { if (data.error || error || !data.user) {
yield put(
authSetRestore({ is_loading: false, error: data.error || error || ERRORS.CODE_IS_INVALID })
);
return yield put(modalShowDialog(DIALOGS.RESTORE_PASSWORD));
}
yield put(authSetRestore({ user: data.user, code, is_loading: false }));
yield put(modalShowDialog(DIALOGS.RESTORE_PASSWORD));
}
function* restorePassword({ password }: ReturnType<typeof authRestorePassword>) {
if (!password) return;
yield put(authSetRestore({ is_loading: true }));
const { code } = yield select(selectAuthRestore);
if (!code) {
return yield put(authSetRestore({ error: ERRORS.CODE_IS_INVALID, is_loading: false }));
}
const { error, data } = yield call(apiRestoreCode, { code, password });
if (data.error || error || !data.user || !data.token) {
return yield put( return yield put(
authSetRestore({ is_loading: false, error: data.error || error || ERRORS.CODE_IS_INVALID }) authSetRestore({ is_loading: false, error: data.error || error || ERRORS.CODE_IS_INVALID })
); );
} }
yield put(modalShowDialog(DIALOGS.RESTORE_PASSWORD)); yield put(authSetToken(data.token));
yield put(authSetRestore({ user: data.user, is_loading: false })); yield put(authSetUser(data.user));
yield put(authSetRestore({ is_loading: false, is_succesfull: true, error: null }));
yield call(refreshUser);
} }
function* authSaga() { function* authSaga() {
@ -327,8 +358,9 @@ function* authSaga() {
yield takeLatest(AUTH_USER_ACTIONS.SEND_MESSAGE, sendMessage); yield takeLatest(AUTH_USER_ACTIONS.SEND_MESSAGE, sendMessage);
yield takeLatest(AUTH_USER_ACTIONS.SET_LAST_SEEN_MESSAGES, setLastSeenMessages); yield takeLatest(AUTH_USER_ACTIONS.SET_LAST_SEEN_MESSAGES, setLastSeenMessages);
yield takeLatest(AUTH_USER_ACTIONS.PATCH_USER, patchUser); yield takeLatest(AUTH_USER_ACTIONS.PATCH_USER, patchUser);
yield takeLatest(AUTH_USER_ACTIONS.SHOW_RESTORE_MODAL, showRestoreModal);
yield takeLatest(AUTH_USER_ACTIONS.REQUEST_RESTORE_CODE, requestRestoreCode); yield takeLatest(AUTH_USER_ACTIONS.REQUEST_RESTORE_CODE, requestRestoreCode);
yield takeLatest(AUTH_USER_ACTIONS.SHOW_RESTORE_MODAL, showRestoreModal);
yield takeLatest(AUTH_USER_ACTIONS.RESTORE_PASSWORD, restorePassword);
} }
export default authSaga; export default authSaga;