Skip to content

Commit 6ed6473

Browse files
committed
Add client.js to handle fetch requests.
1 parent 9169625 commit 6ed6473

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

ui/src/App.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import React, {Component} from 'react';
22
import { HashRouter, Route, Link } from 'react-router-dom';
33

4+
import Client from "./Client";
5+
46
import reactLogo from './images/react.svg';
57
import playLogo from './images/play.svg';
68
import scalaLogo from './images/scala.png';
@@ -19,11 +21,10 @@ class App extends Component {
1921
}
2022

2123
async componentDidMount() {
22-
const response = await fetch('/summary');
23-
const resContent = await response.json();
24-
25-
this.setState({
26-
title: resContent.content
24+
Client.getSummary(summary => {
25+
this.setState({
26+
title: summary.content
27+
});
2728
});
2829
}
2930

ui/src/Client.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* eslint-disable no-undef */
2+
function getSummary(cb) {
3+
return fetch(`/summary`, {
4+
accept: "application/json"
5+
})
6+
.then(checkStatus)
7+
.then(parseJSON)
8+
.then(cb);
9+
}
10+
11+
function checkStatus(response) {
12+
if (response.status >= 200 && response.status < 300) {
13+
return response;
14+
}
15+
const error = new Error(`HTTP Error ${response.statusText}`);
16+
error.status = response.statusText;
17+
error.response = response;
18+
console.log(error); // eslint-disable-line no-console
19+
throw error;
20+
}
21+
22+
function parseJSON(response) {
23+
return response.json();
24+
}
25+
26+
const Client = { getSummary };
27+
export default Client;

0 commit comments

Comments
 (0)