Skip to content

Commit

Permalink
refactor router implementation + implement navbar
Browse files Browse the repository at this point in the history
  • Loading branch information
FreakDev committed Oct 25, 2017
1 parent 153a920 commit c44a1a8
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 51 deletions.
21 changes: 21 additions & 0 deletions src/NavigationBar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react'

import { Link } from 'react-router-dom';

export default ({ pages }) => {
return (
<nav>
<ul>{
pages.map((page, k) => {
return (
<li key={k}>
<Link to={ page.path }>
{ page.name }
</Link>
</li>
)
})
}</ul>
</nav>
)
}
20 changes: 16 additions & 4 deletions src/Page.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import React, { Component } from 'react';
import { Route } from 'react-router-dom';

class Page extends Component {
export class Page extends Component {
render() {
const props = this.props
return (
<div className="page">{props.name}</div>
);
<div className="page">
<h1>{props.name}</h1>
{ props.children }
</div>
)
}
}

export default Page;
const RoutePage = (props) => {
return (
<Route exact={props.exact} path={props.path} render={() => (
<Page { ...props } />
)} />
)
}

export default RoutePage;
85 changes: 85 additions & 0 deletions src/PageManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import React, { Component } from 'react';

import { withRouter, Switch } from 'react-router-dom'
import { BrowserRouter as Router } from 'react-router-dom';

import { TransitionGroup, CSSTransition } from 'react-transition-group'

import Page from './Page'
import NavigationBar from './NavigationBar'

import './App.css';

export class PageManager extends Component {
state = {
direction: 0
}

componentWillReceiveProps(nextProps) {
const findPage = (ref, elem) => {
return elem.indexOf(ref) === 0;
};
const pageOrder = React.Children.map(this.props.children, (child) => child.props.path)

console.log(pageOrder)

let indexCurrent = pageOrder.findIndex(
findPage.bind(this, this.props.location.pathname)
);
let indexNext = pageOrder.findIndex(
findPage.bind(this, nextProps.location.pathname)
);

// indexCurrent = indexCurrent !== -1 ? indexCurrent : index404;
// indexNext = indexNext !== -1 ? indexNext : index404;

this.setState({
direction: indexNext - indexCurrent
});
}

render() {
const { children, location } = this.props
const currentKey = location.pathname.split("/")[1] || "/";
const timeout = { enter: 500, exit: 500 };
let pages = []
const routes = React.Children.map(children, child => {
if (child.type === Page) {
pages.push({ name: child.props.name, path: child.props.path })
return child
} else {
return null
}
})
const navbars = children.filter(child => {
return child.type === NavigationBar
})
return (
<TransitionGroup component="div" className={"page-container " + (this.state.direction >= 0 ? "right" : "left")}>
<CSSTransition
key={currentKey}
timeout={timeout}
classNames="page"
appear
>
<div className={"page-inner"}>
<Switch location={location}>
{ routes }
</Switch>
</div>
</CSSTransition>
{ React.Children.map(navbars, (navbar) => {
return React.cloneElement(navbar, { pages })
}) }
</TransitionGroup>
);
}
}

const PageManagerWithRouter = withRouter(PageManager)

export default props => (
<Router>
<PageManagerWithRouter { ...props } />
</Router>
);
42 changes: 0 additions & 42 deletions src/Pages.js

This file was deleted.

18 changes: 13 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import React from 'react';
import ReactDOM from 'react-dom';

import './index.css';
import Pages from './Pages';

import PageManager from './PageManager';
import Page from './Page';
import NavigationBar from './NavigationBar';

import registerServiceWorker from './registerServiceWorker';

ReactDOM.render(
<Pages>
<Page name="home" exact path="/" />
<Page name="page 2" path="/two" />
</Pages>, document.getElementById('root'));
<div>
<PageManager>
<Page name="home" exact path="/" />
<Page name="page 2" exact path="/two" />
<NavigationBar position="bottom" />
</PageManager>
</div>
, document.getElementById('root'));
registerServiceWorker();

0 comments on commit c44a1a8

Please sign in to comment.