mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 12:56:41 +07:00
moved history listen to sagas
This commit is contained in:
parent
e5be4c383e
commit
d7ed0cbe54
9 changed files with 107 additions and 61 deletions
|
@ -44,6 +44,7 @@ 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>
|
||||||
|
|
|
@ -16,12 +16,12 @@
|
||||||
|
|
||||||
.image {
|
.image {
|
||||||
flex: 1 1;
|
flex: 1 1;
|
||||||
background: red;
|
// background: red;
|
||||||
display: flex;
|
display: flex;
|
||||||
border-radius: 0 $radius $radius 0;
|
border-radius: 0 $radius $radius 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.sign {
|
.sign {
|
||||||
font: $font_24_semibold;
|
// font: $font_24_semibold;
|
||||||
font-size: 72px;
|
// font-size: 72px;
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,3 +76,13 @@ export const authPatchUser = (user: Partial<IUser>) => ({
|
||||||
type: AUTH_USER_ACTIONS.PATCH_USER,
|
type: AUTH_USER_ACTIONS.PATCH_USER,
|
||||||
user,
|
user,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const authSetRestore = (restore: Partial<IAuthState['restore']>) => ({
|
||||||
|
type: AUTH_USER_ACTIONS.SET_RESTORE,
|
||||||
|
restore,
|
||||||
|
});
|
||||||
|
|
||||||
|
export const authRestorePassword = (code: string) => ({
|
||||||
|
type: AUTH_USER_ACTIONS.RESTORE_PASSWORD,
|
||||||
|
code,
|
||||||
|
});
|
||||||
|
|
|
@ -18,6 +18,9 @@ export const AUTH_USER_ACTIONS = {
|
||||||
SET_UPDATES: 'SET_UPDATES',
|
SET_UPDATES: 'SET_UPDATES',
|
||||||
SET_LAST_SEEN_MESSAGES: 'SET_LAST_SEEN_MESSAGES',
|
SET_LAST_SEEN_MESSAGES: 'SET_LAST_SEEN_MESSAGES',
|
||||||
PATCH_USER: 'PATCH_USER',
|
PATCH_USER: 'PATCH_USER',
|
||||||
|
|
||||||
|
SET_RESTORE: 'SET_RESTORE',
|
||||||
|
RESTORE_PASSWORD: 'RESTORE_PASSWORD',
|
||||||
};
|
};
|
||||||
|
|
||||||
export const USER_ERRORS = {
|
export const USER_ERRORS = {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { AUTH_USER_ACTIONS } from '~/redux/auth/constants';
|
import { AUTH_USER_ACTIONS } from '~/redux/auth/constants';
|
||||||
import * as ActionCreators from '~/redux/auth/actions';
|
import * as ActionCreators from '~/redux/auth/actions';
|
||||||
import { IAuthState } from '~/redux/auth/types';
|
import { IAuthState } from '~/redux/auth/types';
|
||||||
|
import { Action } from 'history';
|
||||||
|
|
||||||
interface ActionHandler<T> {
|
interface ActionHandler<T> {
|
||||||
(state: IAuthState, payload: T extends (...args: any[]) => infer R ? R : any): IAuthState;
|
(state: IAuthState, payload: T extends (...args: any[]) => infer R ? R : any): IAuthState;
|
||||||
|
@ -57,6 +58,14 @@ const setLastSeenMessages: ActionHandler<typeof ActionCreators.authSetLastSeenMe
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const setRestore: ActionHandler<typeof ActionCreators.authSetRestore> = (state, { restore }) => ({
|
||||||
|
...state,
|
||||||
|
restore: {
|
||||||
|
...state.restore,
|
||||||
|
...restore,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
export const AUTH_USER_HANDLERS = {
|
export const AUTH_USER_HANDLERS = {
|
||||||
[AUTH_USER_ACTIONS.SET_LOGIN_ERROR]: setLoginError,
|
[AUTH_USER_ACTIONS.SET_LOGIN_ERROR]: setLoginError,
|
||||||
[AUTH_USER_ACTIONS.SET_USER]: setUser,
|
[AUTH_USER_ACTIONS.SET_USER]: setUser,
|
||||||
|
@ -64,4 +73,5 @@ export const AUTH_USER_HANDLERS = {
|
||||||
[AUTH_USER_ACTIONS.SET_PROFILE]: setProfile,
|
[AUTH_USER_ACTIONS.SET_PROFILE]: setProfile,
|
||||||
[AUTH_USER_ACTIONS.SET_UPDATES]: setUpdates,
|
[AUTH_USER_ACTIONS.SET_UPDATES]: setUpdates,
|
||||||
[AUTH_USER_ACTIONS.SET_LAST_SEEN_MESSAGES]: setLastSeenMessages,
|
[AUTH_USER_ACTIONS.SET_LAST_SEEN_MESSAGES]: setLastSeenMessages,
|
||||||
|
[AUTH_USER_ACTIONS.SET_RESTORE]: setRestore,
|
||||||
};
|
};
|
||||||
|
|
|
@ -31,6 +31,13 @@ const INITIAL_STATE: IAuthState = {
|
||||||
messages_error: null,
|
messages_error: null,
|
||||||
patch_errors: {},
|
patch_errors: {},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
restore: {
|
||||||
|
code: '',
|
||||||
|
is_loading: false,
|
||||||
|
is_succesfull: false,
|
||||||
|
errors: {},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export default createReducer(INITIAL_STATE, HANDLERS);
|
export default createReducer(INITIAL_STATE, HANDLERS);
|
||||||
|
|
|
@ -49,4 +49,11 @@ export type IAuthState = Readonly<{
|
||||||
|
|
||||||
patch_errors: Record<string, string>;
|
patch_errors: Record<string, string>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
restore: {
|
||||||
|
code: string;
|
||||||
|
is_loading: boolean;
|
||||||
|
is_succesfull: boolean;
|
||||||
|
errors: Record<string, string>;
|
||||||
|
};
|
||||||
}>;
|
}>;
|
||||||
|
|
23
src/redux/modal/sagas.ts
Normal file
23
src/redux/modal/sagas.ts
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
import { takeEvery, put } from 'redux-saga/effects';
|
||||||
|
import { LocationChangeAction, LOCATION_CHANGE } from 'connected-react-router';
|
||||||
|
import { authOpenProfile, authRestorePassword } from '../auth/actions';
|
||||||
|
|
||||||
|
function* onPathChange({
|
||||||
|
payload: {
|
||||||
|
location: { pathname },
|
||||||
|
},
|
||||||
|
}: LocationChangeAction) {
|
||||||
|
if (pathname.match(/^\/~([\wа-яА-Я]+)/)) {
|
||||||
|
const [, username] = pathname.match(/^\/~([\wа-яА-Я]+)/);
|
||||||
|
return yield put(authOpenProfile(username));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pathname.match(/^\/restore\/([\w\-]+)/)) {
|
||||||
|
const [, code] = pathname.match(/^\/restore\/([\w\-]+)/);
|
||||||
|
return yield put(authRestorePassword(code));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function* modalSaga() {
|
||||||
|
yield takeEvery(LOCATION_CHANGE, onPathChange);
|
||||||
|
}
|
|
@ -1,53 +1,44 @@
|
||||||
import {
|
import { createStore, applyMiddleware, combineReducers, compose, Store } from 'redux';
|
||||||
createStore,
|
|
||||||
applyMiddleware,
|
|
||||||
combineReducers,
|
|
||||||
compose,
|
|
||||||
Store
|
|
||||||
} from "redux";
|
|
||||||
|
|
||||||
import { persistStore, persistReducer } from "redux-persist";
|
import { persistStore, persistReducer } from 'redux-persist';
|
||||||
import storage from "redux-persist/lib/storage";
|
import storage from 'redux-persist/lib/storage';
|
||||||
import createSagaMiddleware from "redux-saga";
|
import createSagaMiddleware from 'redux-saga';
|
||||||
import {
|
import { connectRouter, RouterState, routerMiddleware } from 'connected-react-router';
|
||||||
connectRouter,
|
import { createBrowserHistory } from 'history';
|
||||||
RouterState,
|
import { PersistConfig, Persistor } from 'redux-persist/es/types';
|
||||||
routerMiddleware
|
|
||||||
} from "connected-react-router";
|
|
||||||
import { createBrowserHistory } from "history";
|
|
||||||
import { PersistConfig, Persistor } from "redux-persist/es/types";
|
|
||||||
|
|
||||||
import authReducer from "~/redux/auth/reducer";
|
import authReducer from '~/redux/auth/reducer';
|
||||||
import authSaga from "~/redux/auth/sagas";
|
import authSaga from '~/redux/auth/sagas';
|
||||||
|
|
||||||
import nodeReducer, { INodeState } from "~/redux/node/reducer";
|
import nodeReducer, { INodeState } from '~/redux/node/reducer';
|
||||||
import nodeSaga from "~/redux/node/sagas";
|
import nodeSaga from '~/redux/node/sagas';
|
||||||
|
|
||||||
import flowReducer, { IFlowState } from "~/redux/flow/reducer";
|
import flowReducer, { IFlowState } from '~/redux/flow/reducer';
|
||||||
import flowSaga from "~/redux/flow/sagas";
|
import flowSaga from '~/redux/flow/sagas';
|
||||||
|
|
||||||
import uploadReducer, { IUploadState } from "~/redux/uploads/reducer";
|
import uploadReducer, { IUploadState } from '~/redux/uploads/reducer';
|
||||||
import uploadSaga from "~/redux/uploads/sagas";
|
import uploadSaga from '~/redux/uploads/sagas';
|
||||||
|
|
||||||
import playerReducer, { IPlayerState } from "~/redux/player/reducer";
|
import playerReducer, { IPlayerState } from '~/redux/player/reducer';
|
||||||
import playerSaga from "~/redux/player/sagas";
|
import playerSaga from '~/redux/player/sagas';
|
||||||
|
|
||||||
import { IAuthState } from "~/redux/auth/types";
|
import { IAuthState } from '~/redux/auth/types';
|
||||||
|
|
||||||
import modalReducer, { IModalState } from "~/redux/modal/reducer";
|
import modalReducer, { IModalState } from '~/redux/modal/reducer';
|
||||||
import { gotAuthPostMessage, authOpenProfile } from "./auth/actions";
|
import { gotAuthPostMessage, authOpenProfile } from './auth/actions';
|
||||||
|
import { modalSaga } from './modal/sagas';
|
||||||
|
|
||||||
const authPersistConfig: PersistConfig = {
|
const authPersistConfig: PersistConfig = {
|
||||||
key: "auth",
|
key: 'auth',
|
||||||
whitelist: ["token", "user", "updates"],
|
whitelist: ['token', 'user', 'updates'],
|
||||||
storage
|
storage,
|
||||||
};
|
};
|
||||||
|
|
||||||
const flowPersistConfig: PersistConfig = {
|
const flowPersistConfig: PersistConfig = {
|
||||||
key: "flow",
|
key: 'flow',
|
||||||
whitelist: ["nodes", "recent", "updated"],
|
whitelist: ['nodes', 'recent', 'updated'],
|
||||||
// whitelist: [],
|
// whitelist: [],
|
||||||
storage
|
storage,
|
||||||
};
|
};
|
||||||
|
|
||||||
export interface IState {
|
export interface IState {
|
||||||
|
@ -63,16 +54,19 @@ export interface IState {
|
||||||
export const sagaMiddleware = createSagaMiddleware();
|
export const sagaMiddleware = createSagaMiddleware();
|
||||||
export const history = createBrowserHistory();
|
export const history = createBrowserHistory();
|
||||||
|
|
||||||
history.listen(({ pathname }) => {
|
// history.
|
||||||
if (pathname.match(/~([\wа-яА-Я]+)/)) {
|
// history.listen(({ pathname }) => {
|
||||||
const [, username] = pathname.match(/~([\wа-яА-Я]+)/);
|
// if (pathname.match(/~([\wа-яА-Я]+)/)) {
|
||||||
window.postMessage({ type: "username", username }, "*");
|
// const [, username] = pathname.match(/~([\wа-яА-Я]+)/);
|
||||||
}
|
// window.postMessage({ type: 'username', username }, '*');
|
||||||
});
|
// }
|
||||||
|
|
||||||
|
// console.log({ pathname });
|
||||||
|
// // if (pathname.match)
|
||||||
|
// });
|
||||||
|
|
||||||
const composeEnhancers =
|
const composeEnhancers =
|
||||||
typeof window === "object" &&
|
typeof window === 'object' && (<any>window).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
|
||||||
(<any>window).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
|
|
||||||
? (<any>window).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({})
|
? (<any>window).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({})
|
||||||
: compose;
|
: compose;
|
||||||
|
|
||||||
|
@ -84,7 +78,7 @@ export const store = createStore(
|
||||||
node: nodeReducer,
|
node: nodeReducer,
|
||||||
uploads: uploadReducer,
|
uploads: uploadReducer,
|
||||||
flow: persistReducer(flowPersistConfig, flowReducer),
|
flow: persistReducer(flowPersistConfig, flowReducer),
|
||||||
player: playerReducer
|
player: playerReducer,
|
||||||
}),
|
}),
|
||||||
composeEnhancers(applyMiddleware(routerMiddleware(history), sagaMiddleware))
|
composeEnhancers(applyMiddleware(routerMiddleware(history), sagaMiddleware))
|
||||||
);
|
);
|
||||||
|
@ -98,22 +92,13 @@ export function configureStore(): {
|
||||||
sagaMiddleware.run(uploadSaga);
|
sagaMiddleware.run(uploadSaga);
|
||||||
sagaMiddleware.run(flowSaga);
|
sagaMiddleware.run(flowSaga);
|
||||||
sagaMiddleware.run(playerSaga);
|
sagaMiddleware.run(playerSaga);
|
||||||
|
sagaMiddleware.run(modalSaga);
|
||||||
|
|
||||||
window.addEventListener("message", message => {
|
window.addEventListener('message', message => {
|
||||||
if (
|
if (message && message.data && message.data.type === 'oauth_login' && message.data.token)
|
||||||
message &&
|
|
||||||
message.data &&
|
|
||||||
message.data.type === "oauth_login" &&
|
|
||||||
message.data.token
|
|
||||||
)
|
|
||||||
return store.dispatch(gotAuthPostMessage({ token: message.data.token }));
|
return store.dispatch(gotAuthPostMessage({ token: message.data.token }));
|
||||||
|
|
||||||
if (
|
if (message && message.data && message.data.type === 'username' && message.data.username)
|
||||||
message &&
|
|
||||||
message.data &&
|
|
||||||
message.data.type === "username" &&
|
|
||||||
message.data.username
|
|
||||||
)
|
|
||||||
return store.dispatch(authOpenProfile(message.data.username));
|
return store.dispatch(authOpenProfile(message.data.username));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue