docbrown/app/javascript/githubRepos/githubRepos.jsx
rhymes 81fff248dd
Refactoring GitHub Repos functionality - step 1 (#7764)
* Rename find_or_create to upsert and improve validation and testing

* Add User.authenticated_through?

* Refactor settings/integrations

* Refactor profile github repositories rendering

* Refactor repos fetching

* Only fetch a single repo and improve error messages

* Remove unused code

* Cleanups

* Fix specs

* Remove trailing whitespace

* Fix spec

* Trigger Travis
2020-05-12 13:48:19 +02:00

58 lines
1.4 KiB
JavaScript

import { h, Component } from 'preact';
import { SingleRepo } from './singleRepo';
export class GithubRepos extends Component {
state = {
repos: [],
erroredOut: false,
};
componentDidMount() {
this.getGithubRepos();
}
getGithubRepos = () => {
fetch(`/github_repos`, {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
credentials: 'same-origin',
})
.then((response) => response.json())
.then((json) => {
this.setState({ repos: json });
})
.catch(() => {
this.setState({ erroredOut: true });
});
};
render() {
const { repos, erroredOut } = this.state;
const allRepos = repos.map((repo) => (
<SingleRepo
githubIdCode={repo.github_id_code}
name={repo.name}
fork={repo.fork}
featured={repo.featured}
/>
));
if (erroredOut) {
return (
<div className="github-repos github-repos-errored">
An error occurred. Please check your browser console and email
<a href="mailto:yo@dev.to"> yo@dev.to </a>
for more help.
</div>
);
}
if (repos.length > 0) {
return <div className="github-repos">{allRepos}</div>;
}
return <div className="github-repos loading-repos" />;
}
}
GithubRepos.displayName = 'GitHub Repositories Wrapper';