Skip to content

Commit

Permalink
dashboard.jsx: Load repos automatically after login
Browse files Browse the repository at this point in the history
Closes #131
  • Loading branch information
li-boxuan committed Jul 29, 2018
1 parent 3233746 commit 93c842b
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions src/components/dashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -332,35 +332,41 @@ class ExamplesPanel extends Component {
}


let allMyReposHack = null;

class DashboardShell extends Component {
state = {repos: null};

componentDidMount() {
Client.on('changeToken', this.onChangeToken);
this.onChangeToken();
}

componentWillUnmount() {
Client.off('changeToken', this.onChangeToken);
}

onChangeToken = () => {
CurrentUserStore.fetchUser()
.then((currentUser) => {
if (currentUser) {
return Client.dbPromise().then(() => Client.getOcto().user.repos.fetchAll());
} else {
return []; // Anonymous has no repos
}
})
.then((allRepos) => this.setState({repos: allRepos}))
.catch(() => this.setState({repos: []}));
};

render() {
let {repos} = this.state;

// HACK to not keep re-fetching the user's list of repos
if (repos) { allMyReposHack = repos; }
else { repos = allMyReposHack; }

let myRepos;

if (repos) {
myRepos = (
<Dashboard repos={repos}/>
);
} else {
CurrentUserStore.fetchUser()
.then((currentUser) => {
if (currentUser) {
return Client.dbPromise().then(() => Client.getOcto().user.repos.fetchAll());
} else {
return []; // Anonymous has no repos
}
})
.then((allRepos) => this.setState({repos: allRepos}))
.catch(() => this.setState({repos: []}));
// TODO: Use Loadable component
myRepos = (
<span className='custom-loading is-loading'>
Expand Down

0 comments on commit 93c842b

Please sign in to comment.