Swap: Redux actions; Redux Reducers

This commit is contained in:
Daniel Ternyak 2017-06-11 20:00:28 -05:00
parent 9144d2c666
commit 0304bc8a67
3 changed files with 105 additions and 0 deletions

42
common/actions/swap.js Normal file
View File

@ -0,0 +1,42 @@
// @flow
export const SWAP_ORIGIN_KIND = 'SWAP_ORIGIN_KIND';
export const SWAP_DESTINATION_KIND = 'SWAP_DESTINATION_KIND';
export const SWAP_ORIGIN_AMOUNT = 'SWAP_ORIGIN_AMOUNT';
export const SWAP_DESTINATION_AMOUNT = 'SWAP_DESTINATION_AMOUNT';
export const SWAP_UPDATE_BITY_RATES = 'SWAP_UPDATE_BITY_RATES';
export const SWAP_ORIGIN_KIND_TO = (value: any) => {
return {
type: SWAP_ORIGIN_KIND,
value
};
};
export const SWAP_DESTINATION_KIND_TO = (value: any) => {
return {
type: SWAP_DESTINATION_KIND,
value
}
}
export const SWAP_ORIGIN_AMOUNT_TO = (value: any) => {
return {
type: SWAP_ORIGIN_AMOUNT,
value
};
};
export const SWAP_DESTINATION_AMOUNT_TO = (value: any) => {
return {
type: SWAP_DESTINATION_AMOUNT,
value
}
}
export const SWAP_UPDATE_BITY_RATES_TO = (value: any) => {
return {
type: SWAP_UPDATE_BITY_RATES,
value
}
}

View File

@ -1,5 +1,7 @@
import * as generateWallet from './generateWallet'
import * as config from './config'
import * as swap from './swap'
import { reducer as formReducer } from 'redux-form'
import {combineReducers} from 'redux';
import {routerReducer} from 'react-router-redux'
@ -7,6 +9,7 @@ import {routerReducer} from 'react-router-redux'
export default combineReducers({
...generateWallet,
...config,
...swap,
form: formReducer,
routing: routerReducer
})

60
common/reducers/swap.js Normal file
View File

@ -0,0 +1,60 @@
import {
SWAP_DESTINATION_AMOUNT,
SWAP_DESTINATION_KIND,
SWAP_ORIGIN_AMOUNT,
SWAP_ORIGIN_KIND,
SWAP_UPDATE_BITY_RATES
} from 'actions/swap';
import {without} from 'lodash';
const ALL_CRYPTO_KIND_OPTIONS = ['BTC', 'ETH', 'REP'];
const initialState = {
originAmount: 0,
destinationAmount: 0,
originKind: 'BTC',
destinationKind: 'ETH',
destinationKindOptions: without(ALL_CRYPTO_KIND_OPTIONS, 'BTC'),
originKindOptions: without(ALL_CRYPTO_KIND_OPTIONS, 'REP'),
bityRates: {}
};
export function swap(state = initialState, action) {
switch (action.type) {
case SWAP_ORIGIN_KIND: {
return {
...state,
originKind: action.value,
destinationKindOptions: without(ALL_CRYPTO_KIND_OPTIONS, action.value),
destinationKind: without(ALL_CRYPTO_KIND_OPTIONS, action.value)[0]
};
}
case SWAP_DESTINATION_KIND: {
return {
...state,
destinationKind: action.value
};
}
case SWAP_ORIGIN_AMOUNT:
return {
...state,
originAmount: action.value
};
case SWAP_DESTINATION_AMOUNT:
return {
...state,
destinationAmount: action.value
};
case SWAP_UPDATE_BITY_RATES:
return {
...state,
bityRates: {
...state.bityRates,
...action.value
}
};
default:
return state
}
}