1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-08-09 07:25:24 +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 e28c06b85a
commit a858419f3c
19 changed files with 157 additions and 47 deletions

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