Created
November 11, 2016 17:56
-
-
Save mauricedb/08a6201898d6d6d552128a5746198ceb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import React, { Component } from 'react'; | |
import AppPresentation from './app-presentation'; | |
class AppContainer extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
user: null, | |
movies: null, | |
}; | |
this.loginAsUser = this.loginAsUser.bind(this); | |
} | |
componentWillMount() { | |
if (localStorage.user) { | |
try { | |
const user = JSON.parse(localStorage.user); | |
this.setState({ user }); | |
this.fetchMovies(); | |
} catch (e) { | |
// eslint-disable-next-line no-console | |
console.error(e); | |
} | |
} | |
} | |
fetchMovies() { | |
fetch('/movies.json') | |
.then(rsp => rsp.json()) | |
.then(movies => this.setState({ movies })); | |
} | |
loginAsUser(user) { | |
if (user.rememberMe) { | |
localStorage.user = JSON.stringify(user); | |
} | |
this.setState({ user }); | |
this.fetchMovies(); | |
} | |
render() { | |
const { user, movies } = this.state; | |
return ( | |
<AppPresentation | |
user={user} | |
loginAsUser={this.loginAsUser} | |
movies={movies} | |
/> | |
); | |
} | |
} | |
export default AppContainer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment