-
-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathRoutes.tsx
48 lines (45 loc) · 1.78 KB
/
Routes.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { Route, Redirect, Switch } from 'react-router-dom';
import { SignIn, Register } from './Auth';
import AuthService from '../services/Auth';
import { ErrorPage } from './Error';
import { Contacts } from './Contacts';
import { ContactForm } from './ContactForm';
import { Header } from './Header';
export class RoutePaths {
public static Contacts: string = "/contacts";
public static ContactEdit: string = "/contacts/edit/:id";
public static ContactNew: string = "/contacts/new";
public static SignIn: string = "/";
public static Register: string = "/register/";
}
export default class Routes extends React.Component<any, any> {
render() {
return <Switch>
<Route exact path={RoutePaths.SignIn} component={SignIn} />
<Route path={RoutePaths.Register} component={Register} />
<DefaultLayout exact path={RoutePaths.Contacts} component={Contacts} />
<DefaultLayout path={RoutePaths.ContactNew} component={ContactForm} />
<DefaultLayout path={RoutePaths.ContactEdit} component={ContactForm} />
<Route path='/error/:code?' component={ErrorPage} />
</Switch>
}
}
const DefaultLayout = ({ component: Component, ...rest }: { component: any, path: string, exact?: boolean }) => (
<Route {...rest} render={props => (
AuthService.isSignedIn() ? (
<div>
<Header {...props} />
<div className="container">
<Component {...props} />
</div>
</div>
) : (
<Redirect to={{
pathname: RoutePaths.SignIn,
state: { from: props.location }
}} />
)
)} />
);