Skip to content

Instantly share code, notes, and snippets.

@mauricedb
Created November 11, 2016 17:43
Show Gist options
  • Save mauricedb/87b6ed6ad3f9934807daf1983e296bae to your computer and use it in GitHub Desktop.
Save mauricedb/87b6ed6ad3f9934807daf1983e296bae to your computer and use it in GitHub Desktop.
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 } = this.state;
return (
<AppPresentation
user={user}
loginAsUser={this.loginAsUser}
/>
);
}
}
export default AppContainer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment