Skip to content
This repository was archived by the owner on Jul 3, 2020. It is now read-only.

Commit cee897e

Browse files
committed
Added api client action to retrieve status as a demo
1 parent 8516ef3 commit cee897e

File tree

5 files changed

+65
-10
lines changed

5 files changed

+65
-10
lines changed

frontend/src/actions/app.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,36 @@
1-
import { APP_LOAD } from '../constants/action-types';
1+
import { ACTION_TYPES } from '../constants/action-types';
2+
import ApiClient from '../shared/api-client';
23

34
export function loadApp() {
45
return {
5-
type: APP_LOAD,
6+
type: ACTION_TYPES.APP_LOAD,
67
};
78
}
89

9-
export default { loadApp };
10+
function ApiSuccess(status) {
11+
return {
12+
type: ACTION_TYPES.STATUS_SUCCESS,
13+
status
14+
}
15+
}
16+
17+
function ApiFailure(err) {
18+
return {
19+
type: ACTION_TYPES.STATUS_FAILURE,
20+
err
21+
}
22+
}
23+
24+
const STATUS_URI = "/";
25+
26+
export function checkApiStatus() {
27+
return function(dispatch) {
28+
return ApiClient.get(STATUS_URI)
29+
.then((response) => {
30+
dispatch(ApiSuccess(response.body.status));
31+
},
32+
(error) => {
33+
dispatch(ApiFailure(error));
34+
});
35+
}
36+
}
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
export const APP_LOAD = 'APP_LOAD';
2-
3-
export default { APP_LOAD };
1+
export const ACTION_TYPES = {
2+
APP_LOAD: 'APP_LOAD',
3+
STATUS_REQUEST: 'STATUS_REQUEST',
4+
STATUS_SUCCESS: 'STATUS_SUCCESS',
5+
STATUS_FAILURE: 'STATUS_FAILURE'
6+
};

frontend/src/containers/app.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,16 @@ header {
1616

1717
p {
1818
line-height: 1.5;
19+
}
20+
21+
.StatusPill {
22+
border-radius: 8px;
23+
padding: 0.25rem 0.5rem;
24+
width: 80px;
25+
text-align: center;
26+
margin-left: 15px;
27+
color: white;
28+
font-weight: bold;
29+
background: green;
30+
margin: auto 5px;
1931
}

frontend/src/containers/app.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import { connect } from 'react-redux';
3-
import { loadApp } from '../actions/app';
3+
import { loadApp, checkApiStatus } from '../actions/app';
44

55
import styles from './app.css';
66

@@ -15,6 +15,7 @@ class App extends React.Component {
1515

1616
componentDidMount() {
1717
this.props.dispatch(loadApp());
18+
this.props.dispatch(checkApiStatus());
1819
}
1920

2021
render() {
@@ -34,6 +35,12 @@ class App extends React.Component {
3435
<p>
3536
With redux, thunk actions, redux action logging
3637
</p>
38+
39+
<p>API Status:
40+
<span className="StatusPill">
41+
{this.props.apiStatus}
42+
</span>
43+
</p>
3744
</header>
3845

3946
<main>
@@ -46,6 +53,7 @@ class App extends React.Component {
4653

4754
export default connect((store, props) => {
4855
return {
49-
loaded: store.app.loaded
56+
loaded: store.app.loaded,
57+
apiStatus: store.app.apiStatus
5058
};
5159
})(App);

frontend/src/reducers/app.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
import { APP_LOAD } from '../constants/action-types';
1+
import { ACTION_TYPES } from '../constants/action-types';
22

33
const initialState = {
44
loaded: false,
5+
apiStatus: undefined
56
};
67

78
export default function app(state = initialState, action) {
89
switch (action.type) {
9-
case APP_LOAD:
10+
case ACTION_TYPES.APP_LOAD:
1011
return { ...state, loaded: true };
12+
case ACTION_TYPES.STATUS_SUCCESS:
13+
return { ...state, apiStatus: action.status }
14+
case ACTION_TYPES.STATUS_SUCCESS:
15+
return { ...state, apiStatus: "BAD" }
1116
default:
1217
return state;
1318
}

0 commit comments

Comments
 (0)