React HOCS is added

This commit is contained in:
Sukchan Lee 2017-05-18 11:08:37 +09:00
parent db473e8ed6
commit 2f2da5ffc4
3 changed files with 37 additions and 2 deletions

17
webui/hocs/defaultPage.js Normal file
View File

@ -0,0 +1,17 @@
import React from 'react';
export default Page => class DefaultPage extends React.Component {
static getInitialProps (ctx) {
const loggedUser = 'admin'
const pageProps = Page.getInitialProps && Page.getInitialProps(ctx);
return {
...pageProps,
loggedUser,
isAuthenticated: !!loggedUser
};
}
render() {
return (<Page { ...this.props } />);
}
}

14
webui/hocs/securePage.js Normal file
View File

@ -0,0 +1,14 @@
import React from 'react';
import defaultPage from './defaultPage';
const securePageHoc = Page => class SecurePage extends React.Component {
static getInitialProps (ctx) {
return Page.getInitialProps && Page.getInitialProps(ctx);
}
render () {
return <Page { ...this.props } />
}
}
export default Page => defaultPage(securePageHoc(Page));

View File

@ -1,14 +1,18 @@
import React from 'react';
import Link from 'next/link';
import defaultPage from '../hocs/defaultPage';
const Index = () => {
const Index = ({ loggedUser, isAuthenticated }) => {
return (
<div>
<h1>Hello Worlds!</h1>
{ loggedUser }
{isAuthenticated && <p>Authenticated!!</p>}
{!isAuthenticated && <p>Not Authriozed</p>}
<p><Link href='/login'><a>Login</a></Link></p>
<p><Link href='/logout'><a>Logout</a></Link></p>
</div>
);
}
export default (Index);
export default defaultPage(Index);