Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- "8.7"
- "10"

script:
- yarn test:ci
Expand All @@ -9,4 +9,4 @@ git:
depth: 10

notifications:
slack: angularbuddies:b6leQM9gkBCW0S7MNDsELpqU
slack: angularbuddies:b6leQM9gkBCW0S7MNDsELpqU
8 changes: 4 additions & 4 deletions core/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { createReduxPlugin } from './reduxPlugin';
export { routerReducer } from './reducer';
export { default as createRouterMiddleware } from './middleware';
export { triggerTransition } from './actions';
export { createReduxPlugin, ReduxPluginApplyFn } from "./reduxPlugin";
export { routerReducer } from "./reducer";
export { default as createRouterMiddleware } from "./middleware";
export { triggerTransition } from "./actions";
21 changes: 13 additions & 8 deletions core/reduxPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { UIRouter } from '@uirouter/core';
import { Store } from 'redux';
import { UIRouter } from "@uirouter/core";
import { Store } from "redux";

import { applyHooks } from './applyHooks';
import { ReduxPlugin } from './interface';
import { applyHooks } from "./applyHooks";
import { ReduxPlugin } from "./interface";

export function reduxPluginFactory(name: string, store: Store<any>) {
return function(router: UIRouter): ReduxPlugin {
export type ReduxPluginApplyFn = (router: UIRouter) => ReduxPlugin;

export function reduxPluginFactory(
name: string,
store: Store<any>
): ReduxPluginApplyFn {
return function(router) {
// sync should return function to deregister hooks
const removeHooks = applyHooks(router, store);

Expand All @@ -19,6 +24,6 @@ export function reduxPluginFactory(name: string, store: Store<any>) {

export const createReduxPlugin: (
store: Store<any>
) => (router: UIRouter) => ReduxPlugin = store => {
return reduxPluginFactory('redux', store);
) => ReduxPluginApplyFn = store => {
return reduxPluginFactory("redux", store);
};
17 changes: 11 additions & 6 deletions examples/react/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { pushStateLocationPlugin, UIRouterReact, UISref, UIView } from '@uirouter/react';
import { ConnectedUIRouter } from '@uirouter/redux/react';
import * as React from 'react';
import { Provider } from 'react-redux';
import {
pushStateLocationPlugin,
UIRouterReact,
UISref,
UIView,
} from "@uirouter/react";
import { ConnectedUIRouter } from "@uirouter/redux/react";
import * as React from "react";
import { Provider } from "react-redux";

import createRoutedStore from './redux/store';
import states from './router/states';
import createRoutedStore from "./redux/store";
import states from "./router/states";

const router = new UIRouterReact();
const store = createRoutedStore(router);
Expand Down
23 changes: 14 additions & 9 deletions examples/react/src/components/Counter.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { triggerTransition } from '@uirouter/redux';
import * as React from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { triggerTransition } from "@uirouter/redux";
import * as React from "react";
import { connect } from "react-redux";
import { bindActionCreators } from "redux";

import { decrease, increase } from '../redux/actions';
import { decrease, increase } from "../redux/actions";

type CounterProps = {
counter: number;
Expand All @@ -25,7 +25,7 @@ class Counter extends React.Component<CounterProps, any> {

onClickNavigate = () => {
const { triggerTransition } = this.props.actions;
triggerTransition('home');
triggerTransition("home");
};

render() {
Expand All @@ -39,18 +39,23 @@ class Counter extends React.Component<CounterProps, any> {
<button onClick={this.onClickDecrement}>decrease</button>
</div>
<p>
Or click{' '}
Or click{" "}
<b>
<a onClick={this.onClickNavigate}>here</a>
</b>{' '}
</b>{" "}
to trigger a transition with a redux action
</p>
</div>
);
}
}

export default connect(
export default connect<
{ counter: number },
{},
{},
{ counter: { value: number } }
>(
state => ({
counter: state.counter.value,
}),
Expand Down
2 changes: 2 additions & 0 deletions examples/react/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"paths": {
"react": ["../../node_modules/react"],
"react-dom": ["../../node_modules/react-dom"],
"redux": ["../../node_modules/redux"],
"react-redux": ["../../node_modules/react-redux"],
"@uirouter/redux": ["../../core/index.ts"],
"@uirouter/redux/react": ["../../react/index.ts"]
}
Expand Down
19 changes: 8 additions & 11 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ module.exports = {
snapshotSerializers: ["enzyme-to-json/serializer"],
rootDir: __dirname,
transform: {
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
"^.+\\.tsx?$": "ts-jest",
},
moduleFileExtensions: [
"ts",
"tsx",
"js",
"json"
],
moduleFileExtensions: ["ts", "tsx", "js", "json"],
testURL: "http://localhost:4000",
testRegex: "/__tests__/.*\\.(ts|tsx|js)$",
globals: {
"ts-jest": {
"tsConfigFile": "tsconfig.jest.json"
}
}
};
tsConfig: "./tsconfig.jest.json",
},
},
preset: "ts-jest",
testMatch: null,
};
3 changes: 1 addition & 2 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var Enzyme = require("enzyme");
var Adapter = require("enzyme-adapter-react-15");
var serializer = require('enzyme-to-json/serializer');
var Adapter = require("enzyme-adapter-react-16");

Enzyme.configure({ adapter: new Adapter() });
48 changes: 27 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,37 @@
"release": "release"
},
"devDependencies": {
"@types/jest": "^24.9.0",
"@types/react": "^16.0.5",
"@uirouter/core": "^5.0.6",
"@uirouter/publish-scripts": "^2.0.6",
"@uirouter/react": "^0.5.2",
"awesome-typescript-loader": "^3.2.3",
"cross-env": "^5.0.5",
"enzyme": "^3.1.1",
"enzyme-adapter-react-15": "^1.0.5",
"enzyme-to-json": "^3.2.1",
"jest": "^21.1.0",
"prettier": "^1.7.0",
"@types/react-dom": "^16.9.5",
"@types/react-redux": "^7.0.8",
"@uirouter/core": "=6.0.1",
"@uirouter/publish-scripts": "^2.3.44",
"@uirouter/react": "^1.0.0",
"awesome-typescript-loader": "^5.2.1",
"cross-env": "^6.0.3",
"enzyme": "^3.11.0",
"enzyme-to-json": "^3.4.3",
"jest": "^25.1.0",
"prettier": "^1.19.1",
"prop-types": "^15.5.10",
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-test-renderer": "^16.0.0",
"redux": "^3.7.2",
"shelljs": "^0.7.8",
"shx": "^0.2.2",
"ts-jest": "^21.0.1",
"typescript": "^2.5.2",
"webpack": "^3.6.0",
"webpack-dev-server": "^2.8.2"
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-redux": "^7.1.3",
"react-test-renderer": "^16.12.0",
"redux": "^4.0.5",
"shelljs": "^0.8.3",
"shx": "^0.3.2",
"ts-jest": "^24.3.0",
"typescript": "^3.7.5",
"webpack": "^4.41.5",
"webpack-cli": "^3.3.10",
"webpack-dev-server": "^3.10.1"
},
"peerDependencies": {
"@uirouter/core": ">=5.0.0"
},
"dependencies": {}
"dependencies": {
"enzyme-adapter-react-16": "^1.15.2"
}
}
67 changes: 39 additions & 28 deletions react/ConnectedUIRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
import { servicesPlugin, UIRouter, UIRouterProps } from '@uirouter/react';
import * as PropTypes from 'prop-types';
import * as React from 'react';

import { createReduxPlugin } from '../core';

export class ConnectedUIRouter extends React.Component<UIRouterProps, any> {
reduxPlugin;
router;

static contextTypes = {
store: PropTypes.object,
};

constructor(props, context) {
super(props, context);
this.reduxPlugin = createReduxPlugin(context.store);
this.router = props.router;
this.router.plugin(servicesPlugin);
props.plugins.forEach(plugin => this.router.plugin(plugin));
this.router.plugin(this.reduxPlugin);
if (props.config) props.config(this.router);
(props.states || []).forEach(state =>
this.router.stateRegistry.register(state)
import { servicesPlugin, UIRouter, UIRouterProps } from "@uirouter/react";
import { ReactReduxContext } from "react-redux";
import * as React from "react";
import { useContext, useRef } from "react";

import { createReduxPlugin } from "../core";

interface ConnectedUIRouterProps extends UIRouterProps {
children: React.ReactElement;
}

export function ConnectedUIRouter({
children,
router: routerFromProps,
plugins,
config,
states,
}: ConnectedUIRouterProps) {
const init = useRef(false);

const { store } = useContext(ReactReduxContext);
const router = useRef(routerFromProps);
const reduxPlugin = useRef(createReduxPlugin(store));

// let's initialise the plugins and set up the router
if (init.current !== true) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you are moving to Hooks anyway, maybe you should convert this to useEffect to be compliant with the React docs.

Docs of useEffect suggest using it for side effects etc. So it would be a good use-case for this code.
It would look like below, with empty array as second parameter to ensure that it fires only once.

useEffect(setupRouter, [])

https://reactjs.org/docs/hooks-reference.html#useeffect

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think that is possible as the router would be initialized only after the initial render. As for me, the router should be set up synchronously, so the current implementation looks reasonable

// services plugin is necessary for UIRouter to function
router.current.plugin(servicesPlugin);
// apply all the plugins that are passed via props
plugins.forEach(plugin => router.current.plugin(plugin));
// apply the newly created redux plugin
router.current.plugin(reduxPlugin.current);

if (config) config(router.current);
(states || []).forEach(state =>
router.current.stateRegistry.register(state)
);
}

render() {
const { children } = this.props;
return <UIRouter router={this.router}>{children}</UIRouter>;
init.current = true;
}

return <UIRouter router={router.current}>{children}</UIRouter>;
}
Loading