|
| 1 | +# 12 Table Http |
| 2 | + |
| 3 | +Let's move forward with the table sample, this time we are going to replace the |
| 4 | +mock data by real one. |
| 5 | + |
| 6 | +We will take a startup point sample _11 TableMock_: |
| 7 | + |
| 8 | +Summary steps: |
| 9 | + |
| 10 | +- Install promises dependencies and typescript definitions. |
| 11 | +- Update API in order to work with promises and fetch data from Github API. |
| 12 | +- Update the _tableComponent_ in order to show this data. |
| 13 | + |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +Install [Node.js and npm](https://nodejs.org/en/) (v6.6.0 or newer) if they are not already installed on your computer. |
| 18 | + |
| 19 | +> Verify that you are running at least node v6.x.x and npm 3.x.x by running `node -v` and `npm -v` in a terminal/console window. Older versions may produce errors. |
| 20 | +
|
| 21 | +## Steps to build it |
| 22 | + |
| 23 | +- Copy the content from _11 TableMock_ and execute: |
| 24 | + |
| 25 | + ``` |
| 26 | + npm install |
| 27 | + ``` |
| 28 | + |
| 29 | +- Let's add the dependencies to manage promises and typescript definitions |
| 30 | + |
| 31 | + - **[core-js](https://github.com/zloirock/core-js)**: includes polyfills for ECMAScript 5, ECMAScript 6: **promises**, symbols, collections, iterators, typed arrays, ECMAScript 7+ proposals, setImmediate, etc. |
| 32 | + |
| 33 | + ``` |
| 34 | + npm install core-js --save |
| 35 | + ``` |
| 36 | +
|
| 37 | + - **[whatwg-fetch](https://github.com/github/fetch)**: AJAX calls. |
| 38 | +
|
| 39 | + ``` |
| 40 | + npm install whatwg-fetch --save |
| 41 | + ``` |
| 42 | +
|
| 43 | +- Let's remove the file _mermberMockData.ts_ in _src/api_ directory. |
| 44 | +
|
| 45 | +- Let's define a model entity in _src/model/member.ts_: |
| 46 | +
|
| 47 | + ```javascript |
| 48 | + class MemberEntity { |
| 49 | + constructor() { |
| 50 | + this.id = -1; |
| 51 | + this.login = ''; |
| 52 | + this.avatar_url = ''; |
| 53 | + } |
| 54 | + } |
| 55 | +
|
| 56 | + export default MemberEntity; |
| 57 | +
|
| 58 | + ``` |
| 59 | + |
| 60 | +- Let's replace _memberAPI_ load members with the fetch / promise one: |
| 61 | + |
| 62 | + ```javascript |
| 63 | + import {} from 'core-js'; |
| 64 | + import 'whatwg-fetch'; |
| 65 | + import MemberEntity from '../model/member'; |
| 66 | + |
| 67 | + // Sync mock data API, inspired from: |
| 68 | + // https://gist.github.com/coryhouse/fd6232f95f9d601158e4 |
| 69 | + class MemberAPI { |
| 70 | + |
| 71 | + // Just return a copy of the mock data |
| 72 | + getAllMembers() { |
| 73 | + const gitHubMembersUrl = 'https://api.github.com/orgs/lemoncode/members'; |
| 74 | + |
| 75 | + return fetch(gitHubMembersUrl) |
| 76 | + .then(response => this.checkStatus(response)) |
| 77 | + .then(response => this.parseJSON(response)) |
| 78 | + .then(data => this.resolveMembers(data)); |
| 79 | + } |
| 80 | + |
| 81 | + checkStatus(response) { |
| 82 | + if (!(response.status >= 200 && response.status < 300)) { |
| 83 | + const error = new Error(response.statusText); |
| 84 | + throw error; |
| 85 | + } |
| 86 | + return Promise.resolve(response); |
| 87 | + } |
| 88 | + |
| 89 | + parseJSON(response) { |
| 90 | + return response.json(); |
| 91 | + } |
| 92 | + |
| 93 | + resolveMembers(data) { |
| 94 | + const members = data.map((gitHubMember) => { |
| 95 | + const member = new MemberEntity(); |
| 96 | + |
| 97 | + member.id = gitHubMember.id; |
| 98 | + member.login = gitHubMember.login; |
| 99 | + member.avatar_url = gitHubMember.avatar_url; |
| 100 | + |
| 101 | + return member; |
| 102 | + }); |
| 103 | + |
| 104 | + return Promise.resolve(members); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + const memberAPI = new MemberAPI(); |
| 109 | + |
| 110 | + export default memberAPI; |
| 111 | + |
| 112 | + ``` |
| 113 | + |
| 114 | +- Now it's time to update our _membersTable_ component. <br /> |
| 115 | + Let's consume the new promise base method to retrieve the users: |
| 116 | + |
| 117 | + ```jsx |
| 118 | + // Standard react lifecycle function: |
| 119 | + // https://facebook.github.io/react/docs/component-specs.html |
| 120 | + componentWillMount() { |
| 121 | + memberAPI.getAllMembers().then(members => |
| 122 | + this.setState({members: members}) |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + ``` |
| 127 | + |
| 128 | +- Let's give a try and check the results |
| 129 | + |
| 130 | + ``` |
| 131 | + npm start |
| 132 | + ``` |
0 commit comments