Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Commit

Permalink
WIP: Adding summaries API call and related action, reducer and compon…
Browse files Browse the repository at this point in the history
…ents, refs #4
  • Loading branch information
Genar Trias Ortiz committed Dec 7, 2017
1 parent 448e6d6 commit 32d2b68
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/actions/summaries.js
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)))
}
59 changes: 59 additions & 0 deletions src/actions/summaries.spec.js
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)
)
})
})
3 changes: 3 additions & 0 deletions src/components/Navbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export default class Navbar extends Component {
<Link to="/" className="navbar-brand">
Goby
</Link>
<Link to="/summaries" className="btn btn-link navbar-btn">
Summaries
</Link>
<Link to="/balances" className="btn btn-link navbar-btn">
Your balances
</Link>
Expand Down
5 changes: 5 additions & 0 deletions src/components/Root.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import _ from 'lodash'

import Error from '../containers/Error'
import Balances from '../containers/Balances'
import Summaries from '../containers/Summaries'
import Dashboard from '../containers/Dashboard'
import Selector from '../containers/Selector'
import Container from './Container'
Expand All @@ -18,6 +19,10 @@ const routes = [
component: Dashboard,
exact: true,
},
{
path: '/summaries',
component: Summaries,
},
{
path: '/ticker-add',
component: Selector,
Expand Down
23 changes: 23 additions & 0 deletions src/components/Summaries/index.js
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>
)
}
}
7 changes: 7 additions & 0 deletions src/constants/actionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ export const MARKETS_FILTER = 'MARKETS_FILTER'
export const BALANCES_GET = 'BALANCES_GET'
export const BALANCES_GET_ERROR = 'BALANCES_GET_ERROR'
export const BALANCES_GET_SUCCESS = 'BALANCES_GET_SUCCESS'

/*
* Summaries
*/
export const SUMMARIES_GET = 'SUMMARIES_GET'
export const SUMMARIES_GET_ERROR = 'SUMMARIES_GET_ERROR'
export const SUMMARIES_GET_SUCCESS = 'SUMMARIES_GET_SUCCESS'
8 changes: 8 additions & 0 deletions src/containers/Summaries.js
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)
38 changes: 38 additions & 0 deletions src/reducers/summaries.js
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
}
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ babel-core@^6.0.0, babel-core@^6.26.0, babel-core@~6.26.0:
slash "^1.0.0"
source-map "^0.5.6"

babel-eslint@^8.0.2:
babel-eslint@^8.0.3:
version "8.0.3"
resolved "https://registry.yarnpkg.com/babel-eslint/-/babel-eslint-8.0.3.tgz#f29ecf02336be438195325cd47c468da81ee4e98"
dependencies:
Expand Down

0 comments on commit 32d2b68

Please sign in to comment.