Skip to content

Commit 7b86b7d

Browse files
committed
list repositories at Select
1 parent 7633e73 commit 7b86b7d

File tree

4 files changed

+88
-11
lines changed

4 files changed

+88
-11
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"react-dom": "^16.2.0",
3939
"react-router": "^4.2.0",
4040
"react-router-dom": "^4.2.2",
41+
"react-select": "^1.2.1",
4142
"reactstrap": "^5.0.0-beta",
4243
"style-loader": "0.19.0",
4344
"svg-react-loader": "^0.4.5",
@@ -49,7 +50,7 @@
4950
"whatwg-fetch": "2.0.3"
5051
},
5152
"scripts": {
52-
"start": "yarn install && HOST=cwps.lucassabreu.test node scripts/start.js",
53+
"start": "yarn install && node scripts/start.js",
5354
"build": "node scripts/build.js && cp _redirects build/_redirects",
5455
"test": "node scripts/test.js --env=jsdom"
5556
},

src/GitHub/GitHub.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
import React from 'react'
22
import Loading from '../Components/Loading';
3+
import { withGithubRepositories } from './withGitHubRepositories';
4+
import Select from 'react-select';
5+
import 'react-select/dist/react-select.css';
36

47
class GitHub extends React.Component {
5-
constructor(props) {
6-
super(props);
78

8-
const params = new URLSearchParams(props.location.search);
9+
constructor(props) {
10+
super(props)
11+
this.state = { selectedOption: null }
12+
}
913

10-
this.state = {
11-
loading: true,
12-
code: params.get('code')
13-
};
14+
handleSelect(selectedOption) {
15+
this.setState({ selectedOption })
1416
}
1517

1618
render() {
17-
const { loading } = this.state;
19+
const { loading, repositories } = this.props;
20+
const selectedOption = this.state.selectedOption && this.state.selectedOption.value
21+
22+
repositories.sort((a, b) => a.full_name.toLocaleLowerCase() > b.full_name.toLocaleLowerCase() ? 1 : -1)
1823
return (
1924
<div className="GitHub">
2025
{loading ?
@@ -24,11 +29,19 @@ class GitHub extends React.Component {
2429
</section>
2530
:
2631
<section>
32+
<Select
33+
value={selectedOption}
34+
onChange={(e) => this.handleSelect(e)}
35+
options={repositories.map(r => Object.assign(r, {
36+
value: r.full_name,
37+
label: r.full_name,
38+
}))}
39+
/>
2740
</section>
2841
}
2942
</div>
3043
)
3144
}
3245
}
3346

34-
export default GitHub
47+
export default withGithubRepositories(GitHub)

src/GitHub/withGitHubRepositories.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React from 'react';
2+
3+
export function withGithubRepositories(Component) {
4+
return class extends React.Component {
5+
constructor(props) {
6+
super(props);
7+
this.state = {
8+
loading: true,
9+
repositories: [],
10+
}
11+
}
12+
13+
async componentDidMount() {
14+
const repositories = await this.fetchNextPage();
15+
this.setState({
16+
loading: false,
17+
repositories: repositories,
18+
})
19+
}
20+
21+
async fetchNextPage(page) {
22+
const paging = page ? `?page=${page}` : '';
23+
24+
const resp = await fetch(new Request(
25+
`https://api.github.com/user/repos${paging}`,
26+
{
27+
headers: new Headers({
28+
Authorization: `token ${sessionStorage.getItem('github-token')}`,
29+
Accept: 'application/json',
30+
})
31+
}
32+
));
33+
34+
let repos = await resp.json();
35+
if (repos.length === 0) {
36+
return repos;
37+
}
38+
39+
return repos.concat(await this.fetchNextPage(page ? page + 1 : 2));
40+
}
41+
42+
render() {
43+
return <Component {...this.props}
44+
loading={this.state.loading}
45+
repositories={this.state.repositories}
46+
/>
47+
}
48+
}
49+
}

yarn.lock

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,7 @@ clap@^1.0.9:
13311331
dependencies:
13321332
chalk "^1.1.3"
13331333

1334-
classnames@^2.2.3, classnames@^2.2.5:
1334+
classnames@^2.2.3, classnames@^2.2.4, classnames@^2.2.5:
13351335
version "2.2.5"
13361336
resolved "https://registry.yarnpkg.com/classnames/-/classnames-2.2.5.tgz#fb3801d453467649ef3603c7d61a02bd129bde6d"
13371337

@@ -5210,6 +5210,12 @@ react-error-overlay@^4.0.0:
52105210
version "4.0.0"
52115211
resolved "https://registry.yarnpkg.com/react-error-overlay/-/react-error-overlay-4.0.0.tgz#d198408a85b4070937a98667f500c832f86bd5d4"
52125212

5213+
react-input-autosize@^2.1.2:
5214+
version "2.2.1"
5215+
resolved "https://registry.yarnpkg.com/react-input-autosize/-/react-input-autosize-2.2.1.tgz#ec428fa15b1592994fb5f9aa15bb1eb6baf420f8"
5216+
dependencies:
5217+
prop-types "^15.5.8"
5218+
52135219
react-popper@^0.7.2:
52145220
version "0.7.5"
52155221
resolved "https://registry.yarnpkg.com/react-popper/-/react-popper-0.7.5.tgz#71c25946f291db381231281f6b95729e8b801596"
@@ -5240,6 +5246,14 @@ react-router@^4.2.0:
52405246
prop-types "^15.5.4"
52415247
warning "^3.0.0"
52425248

5249+
react-select@^1.2.1:
5250+
version "1.2.1"
5251+
resolved "https://registry.yarnpkg.com/react-select/-/react-select-1.2.1.tgz#a2fe58a569eb14dcaa6543816260b97e538120d1"
5252+
dependencies:
5253+
classnames "^2.2.4"
5254+
prop-types "^15.5.8"
5255+
react-input-autosize "^2.1.2"
5256+
52435257
react-transition-group@^2.2.0:
52445258
version "2.2.1"
52455259
resolved "https://registry.yarnpkg.com/react-transition-group/-/react-transition-group-2.2.1.tgz#e9fb677b79e6455fd391b03823afe84849df4a10"

0 commit comments

Comments
 (0)