diff --git a/common/actions/swap.js b/common/actions/swap.js new file mode 100644 index 00000000..b51e8db1 --- /dev/null +++ b/common/actions/swap.js @@ -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 + } +} diff --git a/common/reducers/index.js b/common/reducers/index.js index 28b1f155..03af9db1 100644 --- a/common/reducers/index.js +++ b/common/reducers/index.js @@ -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 }) diff --git a/common/reducers/swap.js b/common/reducers/swap.js new file mode 100644 index 00000000..8ef090f9 --- /dev/null +++ b/common/reducers/swap.js @@ -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 + } +}