1
0
Fork 0
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:
Maksim Eltyshev 2022-12-15 01:34:48 +01:00
parent 1fda1bc687
commit 381146b991
19 changed files with 157 additions and 47 deletions

View file

@ -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",

View file

@ -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",

View file

@ -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>
);
}

View file

@ -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
View file

@ -0,0 +1,3 @@
import { createBrowserHistory } from 'history';
export default createBrowserHistory();

View file

@ -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';

View 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;

View 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');

View 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;

View 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;

View 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';

View file

@ -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,

View file

@ -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);

View file

@ -1,6 +0,0 @@
import { createBrowserHistory } from 'history';
import { createReduxHistoryContext } from 'redux-first-history';
export const { createReduxHistory, routerMiddleware, routerReducer } = createReduxHistoryContext({
history: createBrowserHistory(),
});

View file

@ -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';

View file

@ -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());
}

View file

@ -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';

View file

@ -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());
}

View file

@ -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);