-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
299 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
@import '../node_modules/normalize.css/normalize.css'; | ||
@import 'global'; | ||
@import 'awesome-component'; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import ActionTypes from '../constants'; | ||
|
||
export function loadPosts() { | ||
return { | ||
type: ActionTypes.LOAD_POSTS, | ||
payload: { | ||
posts: [ | ||
{title: 'Title 1', content: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Deserunt dolores perferendis cum nobis, doloribus, ex nisi quibusdam omnis numquam minus illum sint veritatis repellat distinctio accusantium quo fuga quam. Itaque.'}, | ||
{title: 'Title 2', content: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Deserunt dolores perferendis cum nobis, doloribus, ex nisi quibusdam omnis numquam minus illum sint veritatis repellat distinctio accusantium quo fuga quam. Itaque.'}, | ||
{title: 'Title 3', content: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Deserunt dolores perferendis cum nobis, doloribus, ex nisi quibusdam omnis numquam minus illum sint veritatis repellat distinctio accusantium quo fuga quam. Itaque.'}, | ||
{title: 'Title 4', content: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Deserunt dolores perferendis cum nobis, doloribus, ex nisi quibusdam omnis numquam minus illum sint veritatis repellat distinctio accusantium quo fuga quam. Itaque.'}, | ||
{title: 'Title 5', content: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Deserunt dolores perferendis cum nobis, doloribus, ex nisi quibusdam omnis numquam minus illum sint veritatis repellat distinctio accusantium quo fuga quam. Itaque.'}, | ||
] | ||
} | ||
}; | ||
} | ||
|
||
export function loadUserProfile(user, password) { | ||
return { | ||
type: ActionTypes.LOGIN, | ||
payload: { | ||
user: { | ||
id: 42, | ||
name: user, | ||
}, | ||
} | ||
}; | ||
} | ||
|
||
export function badLoadUserProfile() { | ||
return { | ||
type: ActionTypes.UNAUTH_LOGIN, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { browserHistory } from 'react-router'; | ||
|
||
export default function checkAuthHelper(store, checker, baseUrl, loginUrl) { | ||
return async ({location: {pathname, search}}, replace, done) => { | ||
const { payload } = await store.dispatch(checker()); | ||
|
||
if (!payload && pathname !== loginUrl) { | ||
replace({ | ||
pathname: loginUrl, | ||
state: { nextPagePathname: `${pathname}${search}` } | ||
}); | ||
} | ||
|
||
if (payload && pathname === loginUrl) { | ||
replace(baseUrl); | ||
} | ||
|
||
done(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import keymirror from 'keymirror'; | ||
|
||
export default keymirror({ | ||
LOGIN: null, | ||
LOAD_POSTS: null, | ||
UNAUTH_LOGIN: null, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,34 @@ | ||
import '../less/_main.less'; | ||
import React from 'react'; | ||
import { render } from 'react-dom'; | ||
import AwesomeComponent from './components/AwesomeComponent.jsx'; | ||
import { Router, Route, IndexRoute, browserHistory } from 'react-router'; | ||
import { Provider } from 'react-redux'; | ||
import checkAuthHelper from './common/utils/checkAuthHelper'; | ||
import { badLoadUserProfile } from './actions'; | ||
import store from './store'; | ||
import Wrapper from './pages/Wrapper'; | ||
import Admin from './pages/Admin'; | ||
import Login from './pages/Login'; | ||
import Posts from './pages/Posts'; | ||
import NotFound from './pages/NotFound'; | ||
|
||
render(React.createElement(AwesomeComponent), document.getElementById('react-root')); | ||
const baseUrl = '/'; | ||
const loginUrl = `${baseUrl}login`; | ||
const checkAuth = checkAuthHelper(store, badLoadUserProfile, baseUrl, loginUrl); | ||
|
||
function MyReactApp () { | ||
return ( | ||
<Provider store={store}> | ||
<Router history={browserHistory}> | ||
<Route path={baseUrl} component={Wrapper} onEnter={checkAuth}> | ||
<IndexRoute component={Admin} /> | ||
<Route path="login" component={Login} /> | ||
<Route path="posts" component={Posts} /> | ||
<Route path="*" component={NotFound} /> | ||
</Route> | ||
</Router> | ||
</Provider> | ||
); | ||
} | ||
|
||
render(React.createElement(MyReactApp), document.getElementById('react-root')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import React, { Component } from 'react'; | ||
import { Link } from 'react-router'; | ||
|
||
export default class Admin extends Component { | ||
render() { | ||
return ( | ||
<div className="admin-page"> | ||
<h1>Admin Page</h1> | ||
|
||
<Link to="/posts">Posts</Link> | ||
</div> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import React, { Component } from 'react'; | ||
import { connect } from 'react-redux'; | ||
import { loadUserProfile } from '../actions'; | ||
|
||
class Login extends Component { | ||
state = { | ||
user: '', | ||
password: '', | ||
} | ||
|
||
onChange = ({target: {value, name}}) => this.setState({[name]: value}) | ||
|
||
onSubmit = async event => { | ||
event.preventDefault(); | ||
|
||
await Promise.resolve() | ||
.then(() => this.props.loadUserProfile(this.state.user, this.state.password)) | ||
.then(() => { | ||
console.log(this.props) | ||
if (this.props.user && this.props.user.name) { | ||
this.props.router.replace(this.props.location.state.nextPagePathname); | ||
} | ||
}); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<form onSubmit={this.onSubmit}> | ||
<div> | ||
<input | ||
type="text" | ||
name="user" | ||
placeholder="username" | ||
onChange={this.onChange} | ||
/> | ||
</div> | ||
<div> | ||
<input | ||
type="password" | ||
name="password" | ||
placeholder="password" | ||
onChange={this.onChange} | ||
/> | ||
</div> | ||
<button type="submit">Login</button> | ||
</form> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default connect(({user}) => ({user}), { loadUserProfile })(Login); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import React from 'react'; | ||
|
||
export default function NotFound() { | ||
return ( | ||
<h1>There is no handler for that page</h1> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React, { Component } from 'react'; | ||
import { connect } from 'react-redux'; | ||
import { loadPosts } from '../actions'; | ||
|
||
class Posts extends Component { | ||
componentDidMount() { | ||
this.props.loadPosts(); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<h1>Posts Page</h1> | ||
|
||
<div className="posts"> | ||
{this.props.posts.map((post, i) => { | ||
return ( | ||
<div className="posts__item" key={`post_${i}`}> | ||
<div className="posts__title">{post.title}</div> | ||
<div className="posts__content">{post.content}</div> | ||
</div> | ||
); | ||
})} | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default connect(({posts}) => ({posts}), {loadPosts})(Posts); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React from 'react'; | ||
import { Link } from 'react-router'; | ||
import style from './Wrapper.less'; | ||
|
||
export default function Wrapper({children}) { | ||
return ( | ||
<div className={style.wrapper}> | ||
<div className={style.header}> | ||
<h1> | ||
<Link to="/" className={style.headerLink}>Header</Link> | ||
</h1> | ||
</div> | ||
<div className={style.content}> | ||
{children} | ||
</div> | ||
<div className={style.footer}> | ||
<h1>Footer</h1> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
:local { | ||
.wrapper { | ||
max-width: 1080px; | ||
} | ||
|
||
.header { | ||
margin: 0 auto; | ||
width: 100%; | ||
} | ||
|
||
.headerLink { | ||
color: red; | ||
font-size: 20px; | ||
} | ||
|
||
.content { | ||
margin: 0 auto; | ||
} | ||
|
||
.footer { | ||
margin: 0 auto; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default from './Wrapper'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import ActionTypes from '../constants'; | ||
|
||
export function user(state = {}, {type, payload}) { | ||
if (type === ActionTypes.LOGIN) { | ||
return payload.user; | ||
} | ||
|
||
return state; | ||
} | ||
|
||
export function posts(state = [], {type, payload}) { | ||
if (type === ActionTypes.LOAD_POSTS) { | ||
return payload.posts; | ||
} | ||
|
||
return state; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { createStore, applyMiddleware, combineReducers } from 'redux'; | ||
import logger from 'redux-logger'; | ||
import { user, posts } from '../reducers'; | ||
|
||
const { NODE_ENV } = process.env; | ||
const common = []; | ||
const middleware = NODE_ENV === 'production' ? [...common] : [...common, logger()]; | ||
const createStoreWithMiddleware = applyMiddleware(...middleware)(createStore); | ||
|
||
export default createStoreWithMiddleware(combineReducers({ | ||
user, | ||
posts, | ||
})); |
Oops, something went wrong.