Loëck Vézien
7 years ago
committed by
GitHub
28 changed files with 263 additions and 485 deletions
@ -1,24 +1,14 @@ |
|||||
// @flow
|
// @flow
|
||||
|
|
||||
import axios from 'axios' |
import { fetchCurrentCounterValues } from '@ledgerhq/wallet-common/lib/api/countervalue' |
||||
|
|
||||
type SendFunction = (type: string, data: *) => void |
type SendFunction = (type: string, data: *) => void |
||||
|
|
||||
export default async (send: SendFunction, { counterValue, currencies }: Object) => { |
export default async (send: SendFunction, { counterValue, currencies }: Object) => { |
||||
const data = await axios |
try { |
||||
.get( |
const data = await fetchCurrentCounterValues(currencies, counterValue) |
||||
`https://min-api.cryptocompare.com/data/pricemulti?extraParams=ledger-test&fsyms=${currencies.join( |
send('counterValues.update', data) |
||||
',', |
} catch (err) { |
||||
)}&tsyms=${counterValue}`,
|
console.error(err) // eslint-disable-line no-console
|
||||
) |
} |
||||
.then(({ data }) => |
|
||||
currencies.reduce((result, code) => { |
|
||||
result.push({ |
|
||||
symbol: `${code}-${counterValue}`, |
|
||||
value: data[code][counterValue], |
|
||||
}) |
|
||||
return result |
|
||||
}, []), |
|
||||
) |
|
||||
send('counterValues.update', data) |
|
||||
} |
} |
||||
|
@ -1,73 +1,41 @@ |
|||||
// @flow
|
// @flow
|
||||
|
|
||||
import { handleActions } from 'redux-actions' |
import { handleActions } from 'redux-actions' |
||||
|
import merge from 'lodash/merge' |
||||
export type CounterValuesState = { |
import get from 'lodash/get' |
||||
[string]: { |
import { |
||||
byDate: Object, |
makeCalculateCounterValue, |
||||
list: Array<[string, number]>, |
makeReverseCounterValue, |
||||
}, |
formatCounterValueDay, |
||||
} |
} from '@ledgerhq/wallet-common/lib/helpers/countervalue' |
||||
|
import type { CalculateCounterValue } from '@ledgerhq/wallet-common/lib/types' |
||||
|
|
||||
|
import type { State } from 'reducers' |
||||
|
|
||||
|
export type CounterValuesState = {} |
||||
const state: CounterValuesState = {} |
const state: CounterValuesState = {} |
||||
|
|
||||
const handlers = { |
const handlers = { |
||||
UPDATE_COUNTER_VALUES: ( |
UPDATE_COUNTER_VALUES: (state, { payload: counterValues }) => merge(state, counterValues), |
||||
state: CounterValuesState, |
|
||||
{ payload: counterValues }: { payload: CounterValuesState }, |
|
||||
): CounterValuesState => ({ |
|
||||
...state, |
|
||||
...counterValues, |
|
||||
}), |
|
||||
UPDATE_LAST_COUNTER_VALUE: ( |
|
||||
state: CounterValuesState, |
|
||||
{ payload: { symbol, value } }: { payload: { symbol: string, value: number } }, |
|
||||
): CounterValuesState => { |
|
||||
// We update only last value (newer)
|
|
||||
if (state[symbol]) { |
|
||||
const [date] = state[symbol].list[0] |
|
||||
// [0] date, [1] value, only update value
|
|
||||
state[symbol].list[0][1] = value |
|
||||
// Keep the same value for byDate object
|
|
||||
state[symbol].byDate[date] = value |
|
||||
|
|
||||
// Update reference for a proper update
|
|
||||
return { ...state } |
|
||||
} |
|
||||
|
|
||||
return state |
|
||||
}, |
|
||||
} |
} |
||||
|
|
||||
export function getLastCounterValueBySymbol( |
const getPairHistory = state => (coinTicker, fiat) => { |
||||
symbol: string, |
const byDate = get(state, `counterValues.${coinTicker}.${fiat}`) |
||||
state: { counterValues: CounterValuesState }, |
return date => { |
||||
): number { |
if (!byDate) { |
||||
return state.counterValues[symbol].list[0][1] |
return 0 |
||||
} |
|
||||
|
|
||||
export function serializeCounterValues(counterValues: Object) { |
|
||||
return Object.keys(counterValues).reduce((result, key) => { |
|
||||
const counterValue = counterValues[key].sort(([dateA], [dateB]) => (dateA < dateB ? 1 : -1)) |
|
||||
|
|
||||
result[key] = { |
|
||||
byDate: counterValue.reduce((r, [date, value]) => { |
|
||||
r[date] = value |
|
||||
return r |
|
||||
}, {}), |
|
||||
list: counterValue, |
|
||||
} |
} |
||||
|
if (!date) { |
||||
return result |
return byDate.latest || 0 |
||||
}, {}) |
} |
||||
|
return byDate[formatCounterValueDay(date)] || 0 |
||||
|
} |
||||
} |
} |
||||
|
|
||||
export function deserializeCounterValues(counterValues: Object) { |
export const calculateCounterValueSelector = (state: State): CalculateCounterValue => |
||||
return Object.keys(counterValues).reduce((result, key) => { |
makeCalculateCounterValue(getPairHistory(state)) |
||||
const counterValue = counterValues[key] |
|
||||
result[key] = counterValue.list |
export const reverseCounterValueSelector = (state: State): CalculateCounterValue => |
||||
return result |
makeReverseCounterValue(getPairHistory(state)) |
||||
}, {}) |
|
||||
} |
|
||||
|
|
||||
export default handleActions(handlers, state) |
export default handleActions(handlers, state) |
||||
|
Loading…
Reference in new issue