Skip to content

Commit 6004fe2

Browse files
committed
Convert gists list to use resource request
1 parent 4ff5ccc commit 6004fe2

File tree

6 files changed

+136
-21
lines changed

6 files changed

+136
-21
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"dependencies": {
66
"lodash": "^4.17.4",
77
"react": "^16.0.0",
8+
"react-composer": "^2.1.0",
89
"react-dom": "^16.0.0",
910
"react-redux": "^5.0.5",
1011
"react-redux-resource": "0.0.1",

src/components/App.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import React, { Component } from 'react';
22
import { Link } from 'react-router-dom';
33
import './App.css';
44
import login from '../personal-access-token';
5-
import { ReadGist } from '../request-components/Gist';
65

76
const isLoggedIn = Boolean(login.username && login.token);
87

@@ -25,22 +24,7 @@ class App extends Component {
2524
{!isLoggedIn &&
2625
`Please provide a GitHub Personal Access Token to use this application.
2726
For more, refer to this project's documentation on GitHub.`}
28-
{isLoggedIn && (
29-
<ReadGist gistId="a6396779d28ed91eb1a55d54a5d228c9">
30-
{stuff => {
31-
console.log('the stuff', stuff);
32-
const { status } = stuff;
33-
34-
return (
35-
<div>
36-
{status.pending && <div>Loading...</div>}
37-
{status.error && <div>Here is an error</div>}
38-
{status.succeeded && <div>Got it!</div>}
39-
</div>
40-
);
41-
}}
42-
</ReadGist>
43-
)}
27+
{isLoggedIn && children}
4428
</div>
4529
);
4630
}

src/components/Gist2.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import React, { Component } from 'react';
2+
import _ from 'lodash';
3+
import { ReadGist } from '../request-components/Gists';
4+
5+
export default class Gist extends Component {
6+
render() {
7+
const {gistId} = this.props.match.params;
8+
9+
return (<ReadGist gistId={gistId}>
10+
{stuff => {
11+
const { status, doFetch } = stuff;
12+
const { description, files } = this.state;
13+
14+
return (
15+
<div>
16+
{status.pending && ('Loading gist...')}
17+
{status.failed && (
18+
<span>
19+
There was an error while retrieving this gist. <button onClick={() => doFetch()}>Try again.</button>
20+
</span>
21+
)}
22+
{status.succeeded && (
23+
<form>
24+
<div>
25+
<div className="Gist-description">
26+
<div className="Gist-descriptionLabel">
27+
Description:
28+
</div>
29+
<input
30+
id="gist-description"
31+
type="text"
32+
className="gist_descriptionInput"
33+
value={description}
34+
placeholder="Gist description..."
35+
onChange={this.onDescriptionChange}/>
36+
</div>
37+
<div>
38+
{_.map(files, (file, originalFilename) => {
39+
return (
40+
<div className="Gist-file" key={originalFilename}>
41+
<input
42+
type="text"
43+
className="gist_fileNameInput"
44+
value={file.filename}
45+
onChange={(event) => this.onFileNameChange(originalFilename, event)}/>
46+
<textarea
47+
className="Gist-textarea"
48+
value={file.content}
49+
onChange={(event) => this.onFileContentsChange(originalFilename, event)}/>
50+
</div>
51+
);
52+
})}
53+
</div>
54+
</div>
55+
</form>
56+
)}
57+
</div>
58+
);
59+
}}
60+
</ReadGist>);
61+
}
62+
63+
constructor(props) {
64+
super(props);
65+
66+
const gist = this.props.gist || {};
67+
const {
68+
description = '',
69+
files = []
70+
} = gist;
71+
72+
this.state = {
73+
description,
74+
files
75+
};
76+
}
77+
}

src/components/Gists2.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React, { Component } from 'react';
2+
import { Link } from 'react-router-dom';
3+
import './Gists.css';
4+
import { ReadUsersGists } from '../request-components/Gists';
5+
import login from '../personal-access-token';
6+
7+
const username = login.username;
8+
9+
export default function Gists() {
10+
return (
11+
<ReadUsersGists username={username}>
12+
{({ status, resources, doFetch }) => (
13+
<div>
14+
{status.pending && ('Loading gists...')}
15+
{status.failed && (
16+
<span>
17+
There was an error loading gists. <button onClick={() => doFetch()}>Try again.</button>
18+
</span>
19+
)}
20+
{status.succeeded && (
21+
<ul className="Gists-list">
22+
{resources.map(gist => (
23+
<li key={gist.id} className="Gists-listItem">
24+
{username}&nbsp;/&nbsp;
25+
<Link to={`/${gist.id}`}>
26+
{Object.keys(gist.files)[0]}
27+
</Link>
28+
&nbsp;
29+
{!gist.public && '🔒'}
30+
</li>
31+
))}
32+
</ul>
33+
)}
34+
</div>
35+
)}
36+
</ReadUsersGists>
37+
);
38+
}

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { Provider } from 'react-redux';
44
import { BrowserRouter, Route, Switch } from 'react-router-dom';
55
import './index.css';
66
import App from './components/App';
7-
import Gists from './components/Gists';
8-
import Gist from './components/Gist';
7+
import Gists from './components/Gists2';
8+
import Gist from './components/Gist2';
99
import CreateGist from './components/CreateGist';
1010
import store from './state/store';
1111

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,23 @@ import React from 'react';
22
import { ResourceRequest, Fetch } from 'react-redux-resource';
33
import headers from '../utils/headers';
44

5+
export function ReadUsersGists({ username, children }) {
6+
const request = (
7+
<Fetch url={`https://api.github.com/users/${username}/gists`} headers={headers} />
8+
);
9+
10+
return (
11+
<ResourceRequest
12+
treatNullAsPending
13+
resourceName="gists"
14+
request={request}
15+
children={children}
16+
/>
17+
);
18+
}
19+
520
export function ReadGist({ gistId, children }) {
6-
const ReadGistRequest = (
21+
const request = (
722
<Fetch url={`https://api.github.com/gists/${gistId}`} headers={headers} />
823
);
924

@@ -12,7 +27,7 @@ export function ReadGist({ gistId, children }) {
1227
treatNullAsPending
1328
transformData={gist => [gist]}
1429
resourceName="gists"
15-
request={ReadGistRequest}
30+
request={request}
1631
children={children}
1732
/>
1833
);

0 commit comments

Comments
 (0)