|
1 | 1 | import React from 'react'; |
2 | 2 | import gql from 'graphql-tag'; |
3 | | -import { Query, Mutation } from 'react-apollo'; |
| 3 | +import { Query } from 'react-apollo'; |
4 | 4 |
|
5 | 5 | import './App.css'; |
6 | 6 |
|
7 | | -const GET_REPOSITORIES_OF_ORGANIZATION = gql` |
| 7 | +const GET_ORGANIZATION = gql` |
8 | 8 | { |
9 | 9 | organization(login: "the-road-to-learn-react") { |
10 | | - repositories(first: 20) { |
11 | | - edges { |
12 | | - node { |
13 | | - id |
14 | | - name |
15 | | - url |
16 | | - viewerHasStarred |
17 | | - } |
18 | | - } |
19 | | - } |
20 | | - } |
21 | | - } |
22 | | -`; |
23 | | - |
24 | | -const STAR_REPOSITORY = gql` |
25 | | - mutation($id: ID!) { |
26 | | - addStar(input: { starrableId: $id }) { |
27 | | - starrable { |
28 | | - id |
29 | | - viewerHasStarred |
30 | | - } |
| 10 | + name |
| 11 | + url |
31 | 12 | } |
32 | 13 | } |
33 | 14 | `; |
34 | 15 |
|
35 | 16 | const App = () => ( |
36 | | - <Query query={GET_REPOSITORIES_OF_ORGANIZATION}> |
| 17 | + <Query query={GET_ORGANIZATION}> |
37 | 18 | {({ data: { organization }, loading }) => { |
38 | 19 | if (loading || !organization) { |
39 | 20 | return <div>Loading ...</div>; |
40 | 21 | } |
41 | 22 |
|
42 | | - return ( |
43 | | - <Repositories repositories={organization.repositories} /> |
44 | | - ); |
| 23 | + return <a href={organization.url}>{organization.name}</a>; |
45 | 24 | }} |
46 | 25 | </Query> |
47 | 26 | ); |
48 | 27 |
|
49 | | -class Repositories extends React.Component { |
50 | | - state = { |
51 | | - selectedRepositoryIds: [], |
52 | | - }; |
53 | | - |
54 | | - toggleSelectRepository = (id, isSelected) => { |
55 | | - let { selectedRepositoryIds } = this.state; |
56 | | - |
57 | | - selectedRepositoryIds = isSelected |
58 | | - ? selectedRepositoryIds.filter(itemId => itemId !== id) |
59 | | - : selectedRepositoryIds.concat(id); |
60 | | - |
61 | | - this.setState({ selectedRepositoryIds }); |
62 | | - }; |
63 | | - |
64 | | - render() { |
65 | | - return ( |
66 | | - <RepositoryList |
67 | | - repositories={this.props.repositories} |
68 | | - selectedRepositoryIds={this.state.selectedRepositoryIds} |
69 | | - toggleSelectRepository={this.toggleSelectRepository} |
70 | | - /> |
71 | | - ); |
72 | | - } |
73 | | -} |
74 | | - |
75 | | -const RepositoryList = ({ |
76 | | - repositories, |
77 | | - selectedRepositoryIds, |
78 | | - toggleSelectRepository, |
79 | | -}) => ( |
80 | | - <ul> |
81 | | - {repositories.edges.map(({ node }) => { |
82 | | - const isSelected = selectedRepositoryIds.includes(node.id); |
83 | | - |
84 | | - const rowClassName = ['row']; |
85 | | - |
86 | | - if (isSelected) { |
87 | | - rowClassName.push('row_selected'); |
88 | | - } |
89 | | - |
90 | | - return ( |
91 | | - <li className={rowClassName.join(' ')} key={node.id}> |
92 | | - <Select |
93 | | - id={node.id} |
94 | | - isSelected={isSelected} |
95 | | - toggleSelectRepository={toggleSelectRepository} |
96 | | - />{' '} |
97 | | - <a href={node.url}>{node.name}</a>{' '} |
98 | | - {!node.viewerHasStarred && <Star id={node.id} />} |
99 | | - </li> |
100 | | - ); |
101 | | - })} |
102 | | - </ul> |
103 | | -); |
104 | | - |
105 | | -const Star = ({ id }) => ( |
106 | | - <Mutation mutation={STAR_REPOSITORY} variables={{ id }}> |
107 | | - {starRepository => ( |
108 | | - <button type="button" onClick={starRepository}> |
109 | | - Star |
110 | | - </button> |
111 | | - )} |
112 | | - </Mutation> |
113 | | -); |
114 | | - |
115 | | -const Select = ({ id, isSelected, toggleSelectRepository }) => ( |
116 | | - <button |
117 | | - type="button" |
118 | | - onClick={() => toggleSelectRepository(id, isSelected)} |
119 | | - > |
120 | | - {isSelected ? 'Unselect' : 'Select'} |
121 | | - </button> |
122 | | -); |
123 | | - |
124 | 28 | export default App; |
0 commit comments