Skip to content

Commit d1c28b6

Browse files
committed
migration
1 parent d22a6df commit d1c28b6

File tree

5 files changed

+49
-94
lines changed

5 files changed

+49
-94
lines changed

front/src/components/privateRoute/PrivateRoute.js

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import React from 'react';
2+
import { Route, Redirect } from 'react-router-dom';
3+
import { RouteComponentProps } from 'react-router';
4+
5+
// #region types
6+
type Props = {
7+
component: any;
8+
checkUserIsConnected: () => { isAuthenticated: boolean };
9+
} & RouteComponentProps;
10+
// #endregion
11+
12+
function PrivateRoute(props: Props) {
13+
const { component: InnerComponent, ...rest } = props;
14+
const { location, checkUserIsConnected } = props;
15+
16+
const { isAuthenticated = false } = !!window && checkUserIsConnected();
17+
18+
return (
19+
<Route
20+
{...rest}
21+
render={props =>
22+
isAuthenticated ? (
23+
<InnerComponent {...props} />
24+
) : (
25+
<Redirect to={{ pathname: '/login', state: { from: location } }} />
26+
)
27+
}
28+
/>
29+
);
30+
}
31+
32+
PrivateRoute.displayName = 'PrivateRoute';
33+
34+
export default PrivateRoute;

front/src/components/privateRoute/__tests__/PrivateRoute.test.js renamed to front/src/components/privateRoute/__tests__/PrivateRoute.test.tsx

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
// @flow
2-
3-
// #region imports
41
import React from 'react';
5-
import renderer from 'react-test-renderer'; // needed both for snpashot testing but also to prevent errors from enzyme
6-
import { mount } from 'enzyme';
2+
import { mount, shallow } from 'enzyme';
73
import { Router, Switch } from 'react-router';
84
import { Route } from 'react-router';
95
import createHistory from 'history/createHashHistory';
106
import PrivateRoute from '../PrivateRoute';
11-
// #endregion
127

138
// #region constants
149
const history = createHistory();
15-
const Home = p => {
10+
const Home = (p: any) => {
1611
p.history.push('/protected');
1712
return <p>home</p>;
1813
};
@@ -24,21 +19,19 @@ describe('PrivateRoute component', () => {
2419
checkIsAuthenticated: () => true,
2520
};
2621

27-
const component = renderer
28-
.create(
29-
<Router history={history}>
30-
<Switch>
31-
<Route exact path="/" component={Home} />
32-
<PrivateRoute
33-
{...props}
34-
path="/protected"
35-
component={() => <p>private</p>}
36-
/>
37-
<Route exact path="/login" component={() => <p>login</p>} />
38-
</Switch>
39-
</Router>,
40-
)
41-
.toJSON();
22+
const component = shallow(
23+
<Router history={history}>
24+
<Switch>
25+
<Route exact path="/" component={Home} />
26+
<PrivateRoute
27+
{...props}
28+
path="/protected"
29+
component={() => <p>private</p>}
30+
/>
31+
<Route exact path="/login" component={() => <p>login</p>} />
32+
</Switch>
33+
</Router>,
34+
);
4235
expect(component).toMatchSnapshot();
4336
});
4437

front/src/components/privateRoute/__tests__/__snapshots__/PrivateRoute.test.js.snap

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
// @flow
2-
3-
// #region imports
41
import { compose } from 'redux';
52
import PrivateRoute from './PrivateRoute';
63
import withAuth from '../../contexts/auth/consumerHOC';
7-
// #endregion
84

95
export default compose(withAuth())(PrivateRoute);

0 commit comments

Comments
 (0)