This repository has been archived by the owner on Sep 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Adding summaries API call and related action, reducer and compon…
…ents, refs #4
- Loading branch information
Genar Trias Ortiz
committed
Dec 7, 2017
1 parent
448e6d6
commit 32d2b68
Showing
9 changed files
with
174 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import axios from 'axios' | ||
import { | ||
SUMMARIES_GET, | ||
SUMMARIES_GET_SUCCESS, | ||
SUMMARIES_GET_ERROR, | ||
} from '../constants/actionTypes' | ||
import { uri, parseResponse } from '../utils' | ||
|
||
export const summariesGet = () => ({ | ||
type: SUMMARIES_GET, | ||
}) | ||
|
||
export const summariesGetSuccess = (summaries) => ({ | ||
type: SUMMARIES_GET_SUCCESS, | ||
summaries, | ||
}) | ||
|
||
export const summariesGetError = (error) => ({ | ||
type: SUMMARIES_GET_ERROR, | ||
error, | ||
}) | ||
|
||
export const get = () => (dispatch) => { | ||
dispatch(summariesGet()) | ||
|
||
return axios.get(uri('summaries')) | ||
.then(parseResponse) | ||
.then((summaries) => dispatch(summariesGetSuccess(summaries))) | ||
.catch((error) => dispatch(summariesGetError(error))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import configureMockStore from 'redux-mock-store' | ||
import axiosCore from 'axios' | ||
import MockAdapter from 'axios-mock-adapter' | ||
import thunk from 'redux-thunk' | ||
|
||
import { | ||
SUMMARIES_GET, | ||
SUMMARIES_GET_SUCCESS, | ||
} from '../constants/actionTypes' | ||
import * as actions from './summaries' | ||
|
||
const axios = new MockAdapter(axiosCore) | ||
|
||
const middlewares = [thunk] | ||
const mockStore = configureMockStore(middlewares) | ||
|
||
const defaultState = { | ||
all: {}, | ||
ids: [], | ||
error: false, | ||
} | ||
|
||
const summaries = [ | ||
{ | ||
Currency: 'ADA', | ||
Balance: 0, | ||
Available: 0, | ||
Pending: 0, | ||
CryptoAddress: null, | ||
}, | ||
{ | ||
Currency: 'BAT', | ||
Balance: 200, | ||
Available: 0, | ||
Pending: 0, | ||
CryptoAddress: null, | ||
}, | ||
] | ||
|
||
describe('actions::summaries', () => { | ||
it('get() fires SUMMARIES_GET && SUMMARIES_GET_SUCCESS', () => { | ||
axios.onGet(/summaries$/).reply(200, summaries) | ||
const expectedActions = [ | ||
{ | ||
type: SUMMARIES_GET, | ||
}, | ||
{ | ||
type: SUMMARIES_GET_SUCCESS, | ||
summaries, | ||
}, | ||
] | ||
|
||
const store = mockStore({ summaries: defaultState }) | ||
|
||
return store.dispatch(actions.get()).then(() => | ||
expect(store.getActions()).toEqual(expectedActions) | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React, { Component } from 'react' | ||
import PropTypes from 'prop-types' | ||
|
||
import { get as getSummaries } from '../../actions/summaries' | ||
|
||
export default class Summaries extends Component { | ||
static propTypes = { | ||
dispatch: PropTypes.func.isRequired, | ||
summaries: PropTypes.object.isRequired, | ||
} | ||
|
||
componentWillMount() { | ||
this.props.dispatch(getSummaries()) | ||
} | ||
|
||
render() { | ||
return ( | ||
<div className='summaries-index'> | ||
Summaries goes here | ||
</div> | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { connect } from 'react-redux' | ||
import Summaries from '../components/Summaries' | ||
|
||
export default connect( | ||
(state) => ({ | ||
summaries: state.summaries, | ||
}) | ||
)(Summaries) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { connectionErrorMessage } from '../utils' | ||
import { | ||
SUMMARIES_GET_SUCCESS, | ||
SUMMARIES_GET_ERROR, | ||
SUMMARIES_GET, | ||
} from '../constants/actionTypes' | ||
import { idsMapper } from '.' | ||
|
||
const defaultState = { | ||
all: {}, | ||
ids: [], | ||
error: false, | ||
} | ||
|
||
export default (state = defaultState, action = {}) => { | ||
switch (action.type) { | ||
case SUMMARIES_GET: | ||
return { | ||
...state, | ||
error: false, | ||
} | ||
|
||
case SUMMARIES_GET_SUCCESS: | ||
return { | ||
...state, | ||
...idsMapper(action.summaries, 'Currency'), | ||
} | ||
|
||
case SUMMARIES_GET_ERROR: | ||
return { | ||
...state, | ||
error: connectionErrorMessage(action.error), | ||
} | ||
|
||
default: | ||
return state | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters