|
1 | 1 | import React, { Component } from 'react'; |
2 | | -import { bindActionCreators } from 'redux'; |
3 | | -import { connect } from 'react-redux'; |
4 | 2 | import { Link } from 'react-router-dom'; |
5 | | -import { getResources, getStatus } from 'react-redux-resource'; |
6 | 3 | import './Gists.css'; |
7 | | -import { readManyUsersGists } from '../state/gists/action-creators'; |
| 4 | +import { ReadUsersGists } from '../request-components/Gists'; |
8 | 5 | import login from '../personal-access-token'; |
9 | 6 |
|
10 | 7 | const username = login.username; |
11 | 8 |
|
12 | | -class Gists extends Component { |
13 | | - render() { |
14 | | - const { usersGists, usersGistsStatus } = this.props; |
15 | | - |
16 | | - return ( |
17 | | - <div className="Gists"> |
18 | | - {usersGistsStatus.pending && ('Loading gists...')} |
19 | | - {usersGistsStatus.failed && ( |
20 | | - <span> |
21 | | - There was an error loading gists. <button onClick={this.fetchUsersGists}>Try again.</button> |
22 | | - </span> |
23 | | - )} |
24 | | - {usersGistsStatus.succeeded && ( |
25 | | - <ul className="Gists-list"> |
26 | | - {usersGists.map(gist => ( |
27 | | - <li key={gist.id} className="Gists-listItem"> |
28 | | - {username} / |
29 | | - <Link to={`/${gist.id}`}> |
30 | | - {Object.keys(gist.files)[0]} |
31 | | - </Link> |
32 | | - |
33 | | - {!gist.public && '🔒'} |
34 | | - </li> |
35 | | - ))} |
36 | | - </ul> |
37 | | - )} |
38 | | - </div> |
39 | | - ); |
40 | | - } |
41 | | - |
42 | | - componentDidMount() { |
43 | | - this.fetchUsersGists(); |
44 | | - } |
45 | | - |
46 | | - componentWillUnmount() { |
47 | | - if (this.readManyUsersGistsXhr) { |
48 | | - this.readManyUsersGistsXhr.abort(); |
49 | | - } |
50 | | - } |
51 | | - |
52 | | - fetchUsersGists = () => { |
53 | | - const { readManyUsersGists } = this.props; |
54 | | - |
55 | | - if (this.readManyUsersGistsXhr) { |
56 | | - this.readManyUsersGistsXhr.abort(); |
57 | | - } |
58 | | - |
59 | | - this.readManyUsersGistsXhr = readManyUsersGists(username); |
60 | | - } |
| 9 | +export default function Gists() { |
| 10 | + return ( |
| 11 | + <ReadUsersGists username={username}> |
| 12 | + {({ status, lists, doFetch }) => ( |
| 13 | + <div> |
| 14 | + {status.pending && 'Loading gists...'} |
| 15 | + {status.failed && ( |
| 16 | + <span> |
| 17 | + There was an error loading gists.{' '} |
| 18 | + <button onClick={() => doFetch()}>Try again.</button> |
| 19 | + </span> |
| 20 | + )} |
| 21 | + {status.succeeded && ( |
| 22 | + <ul className="Gists-list"> |
| 23 | + {lists.usersGists.map(gist => ( |
| 24 | + <li key={gist.id} className="Gists-listItem"> |
| 25 | + {username} / |
| 26 | + <Link to={`/${gist.id}`}>{Object.keys(gist.files)[0]}</Link> |
| 27 | + |
| 28 | + {!gist.public && '🔒'} |
| 29 | + </li> |
| 30 | + ))} |
| 31 | + </ul> |
| 32 | + )} |
| 33 | + </div> |
| 34 | + )} |
| 35 | + </ReadUsersGists> |
| 36 | + ); |
61 | 37 | } |
62 | | - |
63 | | -function mapStateToProps(state) { |
64 | | - const usersGists = getResources(state.gists, 'usersGists'); |
65 | | - const usersGistsStatus = getStatus(state, 'gists.requests.getUsersGists.status', true); |
66 | | - |
67 | | - return { |
68 | | - usersGists, |
69 | | - usersGistsStatus |
70 | | - }; |
71 | | -} |
72 | | - |
73 | | -function mapDispatchToProps(dispatch) { |
74 | | - return bindActionCreators({ |
75 | | - readManyUsersGists |
76 | | - }, dispatch); |
77 | | -} |
78 | | - |
79 | | -export default connect(mapStateToProps, mapDispatchToProps)(Gists); |
0 commit comments