mirror of
https://github.com/plankanban/planka.git
synced 2025-07-18 20:59:44 +02:00
fix: Fix router synchronization with redux store
This commit is contained in:
parent
1fda1bc687
commit
381146b991
19 changed files with 157 additions and 47 deletions
17
client/package-lock.json
generated
17
client/package-lock.json
generated
|
@ -32,13 +32,11 @@
|
|||
"react-markdown": "^8.0.3",
|
||||
"react-photoswipe-gallery": "^2.2.2",
|
||||
"react-redux": "^8.0.5",
|
||||
"react-router": "^6.4.3",
|
||||
"react-router-dom": "^6.4.3",
|
||||
"react-scripts": "5.0.1",
|
||||
"react-simplemde-editor": "^5.2.0",
|
||||
"react-textarea-autosize": "^8.4.0",
|
||||
"redux": "^4.2.0",
|
||||
"redux-first-history": "^5.1.1",
|
||||
"redux-logger": "^3.0.6",
|
||||
"redux-orm": "^0.16.2",
|
||||
"redux-saga": "^1.2.1",
|
||||
|
@ -19949,15 +19947,6 @@
|
|||
"@babel/runtime": "^7.9.2"
|
||||
}
|
||||
},
|
||||
"node_modules/redux-first-history": {
|
||||
"version": "5.1.1",
|
||||
"resolved": "https://registry.npmjs.org/redux-first-history/-/redux-first-history-5.1.1.tgz",
|
||||
"integrity": "sha512-ujVHv+y9wC2rqavS5tLiSu6zkw+VYrEea+/ggwVTRuutadEtwxSRlaK19ry/PTLSQtFuUF1Xu+plL5erD4roVw==",
|
||||
"peerDependencies": {
|
||||
"history": "^4.7.2 || ^5.0",
|
||||
"redux": "^3.6.0 || ^4.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/redux-logger": {
|
||||
"version": "3.0.6",
|
||||
"resolved": "https://registry.npmjs.org/redux-logger/-/redux-logger-3.0.6.tgz",
|
||||
|
@ -37850,12 +37839,6 @@
|
|||
"@babel/runtime": "^7.9.2"
|
||||
}
|
||||
},
|
||||
"redux-first-history": {
|
||||
"version": "5.1.1",
|
||||
"resolved": "https://registry.npmjs.org/redux-first-history/-/redux-first-history-5.1.1.tgz",
|
||||
"integrity": "sha512-ujVHv+y9wC2rqavS5tLiSu6zkw+VYrEea+/ggwVTRuutadEtwxSRlaK19ry/PTLSQtFuUF1Xu+plL5erD4roVw==",
|
||||
"requires": {}
|
||||
},
|
||||
"redux-logger": {
|
||||
"version": "3.0.6",
|
||||
"resolved": "https://registry.npmjs.org/redux-logger/-/redux-logger-3.0.6.tgz",
|
||||
|
|
|
@ -79,13 +79,11 @@
|
|||
"react-markdown": "^8.0.3",
|
||||
"react-photoswipe-gallery": "^2.2.2",
|
||||
"react-redux": "^8.0.5",
|
||||
"react-router": "^6.4.3",
|
||||
"react-router-dom": "^6.4.3",
|
||||
"react-scripts": "5.0.1",
|
||||
"react-simplemde-editor": "^5.2.0",
|
||||
"react-textarea-autosize": "^8.4.0",
|
||||
"redux": "^4.2.0",
|
||||
"redux-first-history": "^5.1.1",
|
||||
"redux-logger": "^3.0.6",
|
||||
"redux-orm": "^0.16.2",
|
||||
"redux-saga": "^1.2.1",
|
||||
|
|
|
@ -2,7 +2,7 @@ import React from 'react';
|
|||
import PropTypes from 'prop-types';
|
||||
import { Provider } from 'react-redux';
|
||||
import { Route, Routes } from 'react-router-dom';
|
||||
import { HistoryRouter as Router } from 'redux-first-history/rr6';
|
||||
import { ReduxRouter } from '../lib/redux-router';
|
||||
|
||||
import Paths from '../constants/Paths';
|
||||
import LoginContainer from '../containers/LoginContainer';
|
||||
|
@ -19,7 +19,7 @@ import '../styles.module.scss';
|
|||
function Root({ store, history }) {
|
||||
return (
|
||||
<Provider store={store}>
|
||||
<Router history={history}>
|
||||
<ReduxRouter history={history}>
|
||||
<Routes>
|
||||
<Route path={Paths.LOGIN} element={<LoginContainer />} />
|
||||
<Route path={Paths.ROOT} element={<CoreWrapperContainer />} />
|
||||
|
@ -28,7 +28,7 @@ function Root({ store, history }) {
|
|||
<Route path={Paths.CARDS} element={<CoreWrapperContainer />} />
|
||||
<Route path="*" element={<NotFound />} />
|
||||
</Routes>
|
||||
</Router>
|
||||
</ReduxRouter>
|
||||
</Provider>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { bindActionCreators } from 'redux';
|
||||
import { connect } from 'react-redux';
|
||||
import { push } from 'redux-first-history';
|
||||
import omit from 'lodash/omit';
|
||||
import { push } from '../lib/redux-router';
|
||||
|
||||
import selectors from '../selectors';
|
||||
import entryActions from '../entry-actions';
|
||||
|
|
3
client/src/history.js
Executable file
3
client/src/history.js
Executable file
|
@ -0,0 +1,3 @@
|
|||
import { createBrowserHistory } from 'history';
|
||||
|
||||
export default createBrowserHistory();
|
|
@ -1,7 +1,8 @@
|
|||
import React from 'react';
|
||||
import ReactDOM from 'react-dom/client';
|
||||
|
||||
import store, { history } from './store';
|
||||
import store from './store';
|
||||
import history from './history';
|
||||
import Root from './components/Root';
|
||||
|
||||
import './i18n';
|
||||
|
|
46
client/src/lib/redux-router/ReduxRouter.jsx
Normal file
46
client/src/lib/redux-router/ReduxRouter.jsx
Normal file
|
@ -0,0 +1,46 @@
|
|||
import React, { useLayoutEffect } from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import { useDispatch, useSelector } from 'react-redux';
|
||||
import { Router } from 'react-router-dom';
|
||||
|
||||
import { handleLocationChange } from './actions';
|
||||
|
||||
function ReduxRouter({ children, history, selector, basename }) {
|
||||
const state = useSelector(selector);
|
||||
const dispatch = useDispatch();
|
||||
|
||||
useLayoutEffect(() => {
|
||||
const unlisten = history.listen((nextState) => {
|
||||
dispatch(handleLocationChange(nextState.location, nextState.action));
|
||||
});
|
||||
|
||||
dispatch(handleLocationChange(history.location, history.action, true));
|
||||
|
||||
return unlisten;
|
||||
}, [history, dispatch]);
|
||||
|
||||
return (
|
||||
<Router
|
||||
basename={basename}
|
||||
location={state.location}
|
||||
navigationType={state.action}
|
||||
navigator={history}
|
||||
>
|
||||
{children}
|
||||
</Router>
|
||||
);
|
||||
}
|
||||
|
||||
ReduxRouter.propTypes = {
|
||||
children: PropTypes.element.isRequired,
|
||||
history: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types
|
||||
selector: PropTypes.func,
|
||||
basename: PropTypes.string,
|
||||
};
|
||||
|
||||
ReduxRouter.defaultProps = {
|
||||
selector: ({ router }) => router,
|
||||
basename: undefined,
|
||||
};
|
||||
|
||||
export default ReduxRouter;
|
28
client/src/lib/redux-router/actions.js
Normal file
28
client/src/lib/redux-router/actions.js
Normal file
|
@ -0,0 +1,28 @@
|
|||
export const LOCATION_CHANGE_HANDLE = '@@router/LOCATION_CHANGE_HANDLE';
|
||||
|
||||
export const handleLocationChange = (location, action, isFirstRendering = false) => ({
|
||||
type: LOCATION_CHANGE_HANDLE,
|
||||
payload: {
|
||||
location,
|
||||
action,
|
||||
isFirstRendering,
|
||||
},
|
||||
});
|
||||
|
||||
export const HISTORY_METHOD_CALL = '@@router/HISTORY_METHOD_CALL';
|
||||
|
||||
const createHistoryMethodCaller = (method) => {
|
||||
return (...args) => ({
|
||||
type: HISTORY_METHOD_CALL,
|
||||
payload: {
|
||||
method,
|
||||
args,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export const push = createHistoryMethodCaller('push');
|
||||
export const replace = createHistoryMethodCaller('replace');
|
||||
export const go = createHistoryMethodCaller('go');
|
||||
export const back = createHistoryMethodCaller('back');
|
||||
export const forward = createHistoryMethodCaller('forward');
|
18
client/src/lib/redux-router/create-router-middleware.js
Normal file
18
client/src/lib/redux-router/create-router-middleware.js
Normal file
|
@ -0,0 +1,18 @@
|
|||
import { HISTORY_METHOD_CALL } from './actions';
|
||||
|
||||
const createRouterMiddleware = (history) => {
|
||||
// eslint-disable-next-line consistent-return
|
||||
return () => (next) => (action) => {
|
||||
if (action.type !== HISTORY_METHOD_CALL) {
|
||||
return next(action);
|
||||
}
|
||||
|
||||
const {
|
||||
payload: { method, args },
|
||||
} = action;
|
||||
|
||||
history[method](...args);
|
||||
};
|
||||
};
|
||||
|
||||
export default createRouterMiddleware;
|
28
client/src/lib/redux-router/create-router-reducer.js
Normal file
28
client/src/lib/redux-router/create-router-reducer.js
Normal file
|
@ -0,0 +1,28 @@
|
|||
import { LOCATION_CHANGE_HANDLE } from './actions';
|
||||
|
||||
const createRouterReducer = (history) => {
|
||||
const initialState = {
|
||||
location: history.location,
|
||||
action: history.action,
|
||||
};
|
||||
|
||||
return (state = initialState, { type, payload } = {}) => {
|
||||
if (type === LOCATION_CHANGE_HANDLE) {
|
||||
const { location, action, isFirstRendering } = payload;
|
||||
|
||||
if (isFirstRendering) {
|
||||
return state;
|
||||
}
|
||||
|
||||
return {
|
||||
...state,
|
||||
location,
|
||||
action,
|
||||
};
|
||||
}
|
||||
|
||||
return state;
|
||||
};
|
||||
};
|
||||
|
||||
export default createRouterReducer;
|
12
client/src/lib/redux-router/index.js
Normal file
12
client/src/lib/redux-router/index.js
Normal file
|
@ -0,0 +1,12 @@
|
|||
export {
|
||||
LOCATION_CHANGE_HANDLE,
|
||||
HISTORY_METHOD_CALL,
|
||||
push,
|
||||
replace,
|
||||
go,
|
||||
back,
|
||||
forward,
|
||||
} from './actions';
|
||||
export { default as createRouterReducer } from './create-router-reducer';
|
||||
export { default as createRouterMiddleware } from './create-router-middleware';
|
||||
export { default as ReduxRouter } from './ReduxRouter';
|
|
@ -1,4 +1,4 @@
|
|||
import { LOCATION_CHANGE } from 'redux-first-history';
|
||||
import { LOCATION_CHANGE_HANDLE } from '../lib/redux-router';
|
||||
|
||||
import ActionTypes from '../constants/ActionTypes';
|
||||
import ModalTypes from '../constants/ModalTypes';
|
||||
|
@ -12,7 +12,7 @@ const initialState = {
|
|||
// eslint-disable-next-line default-param-last
|
||||
export default (state = initialState, { type, payload }) => {
|
||||
switch (type) {
|
||||
case LOCATION_CHANGE:
|
||||
case LOCATION_CHANGE_HANDLE:
|
||||
case ActionTypes.MODAL_CLOSE:
|
||||
return {
|
||||
...state,
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import { routerReducer } from '../redux-history-context';
|
||||
import { createRouterReducer } from '../lib/redux-router';
|
||||
|
||||
export default routerReducer;
|
||||
import history from '../history';
|
||||
|
||||
export default createRouterReducer(history);
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
import { createBrowserHistory } from 'history';
|
||||
import { createReduxHistoryContext } from 'redux-first-history';
|
||||
|
||||
export const { createReduxHistory, routerMiddleware, routerReducer } = createReduxHistoryContext({
|
||||
history: createBrowserHistory(),
|
||||
});
|
|
@ -1,5 +1,5 @@
|
|||
import { call, put, select, take } from 'redux-saga/effects';
|
||||
import { push } from 'redux-first-history';
|
||||
import { push } from '../../../lib/redux-router';
|
||||
|
||||
import request from '../request';
|
||||
import selectors from '../../../selectors';
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import { takeEvery } from 'redux-saga/effects';
|
||||
import { LOCATION_CHANGE } from 'redux-first-history';
|
||||
import { LOCATION_CHANGE_HANDLE } from '../../../lib/redux-router';
|
||||
|
||||
import services from '../services';
|
||||
|
||||
export default function* routerWatchers() {
|
||||
yield takeEvery(LOCATION_CHANGE, () => services.handleLocationChange());
|
||||
yield takeEvery(LOCATION_CHANGE_HANDLE, () => services.handleLocationChange());
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { call, put, select } from 'redux-saga/effects';
|
||||
import { push } from 'redux-first-history';
|
||||
import { push } from '../../../lib/redux-router';
|
||||
|
||||
import selectors from '../../../selectors';
|
||||
import Paths from '../../../constants/Paths';
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import { takeEvery } from 'redux-saga/effects';
|
||||
import { LOCATION_CHANGE } from 'redux-first-history';
|
||||
import { LOCATION_CHANGE_HANDLE } from '../../../lib/redux-router';
|
||||
|
||||
import services from '../services';
|
||||
|
||||
export default function* routerWatchers() {
|
||||
yield takeEvery(LOCATION_CHANGE, () => services.handleLocationChange());
|
||||
yield takeEvery(LOCATION_CHANGE_HANDLE, () => services.handleLocationChange());
|
||||
}
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
import { applyMiddleware, legacy_createStore as createStore, compose as reduxCompose } from 'redux';
|
||||
import createSagaMiddleware from 'redux-saga';
|
||||
import { createRouterMiddleware } from './lib/redux-router';
|
||||
|
||||
import rootReducer from './reducers';
|
||||
import rootSaga from './sagas';
|
||||
import { createReduxHistory, routerMiddleware } from './redux-history-context';
|
||||
import history from './history';
|
||||
|
||||
const sagaMiddleware = createSagaMiddleware();
|
||||
|
||||
const middlewares = [sagaMiddleware, routerMiddleware];
|
||||
const middlewares = [sagaMiddleware, createRouterMiddleware(history)];
|
||||
|
||||
let compose = reduxCompose;
|
||||
|
||||
|
@ -22,10 +23,6 @@ if (process.env.NODE_ENV !== 'production') {
|
|||
}
|
||||
}
|
||||
|
||||
const store = createStore(rootReducer, compose(applyMiddleware(...middlewares)));
|
||||
export default createStore(rootReducer, compose(applyMiddleware(...middlewares)));
|
||||
|
||||
sagaMiddleware.run(rootSaga);
|
||||
|
||||
export default store;
|
||||
|
||||
export const history = createReduxHistory(store);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue