This repository has been archived by the owner on Jul 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 174
/
app.js
137 lines (123 loc) · 3.33 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// @flow
import {
getCryptoCurrencyById,
getFiatCurrencyByTicker,
listCryptoCurrencies
} from "@ledgerhq/live-common/lib/currencies";
import type { Currency } from "@ledgerhq/live-common/lib/types";
import { createSelector } from "reselect";
export type State = {
countervalueCurrency: Currency,
countervalueExchange: ?string,
intermediaryCurrency: Currency,
rows: Array<{
currency: ?Currency,
exchange: ?string,
value: number
}>
};
const pick = array => array[Math.floor(Math.random() * array.length)];
const initialState: State = {
countervalueCurrency: getFiatCurrencyByTicker("USD"),
countervalueExchange: "Kraken",
intermediaryCurrency: getCryptoCurrencyById("bitcoin"),
rows: [
{
currency: getCryptoCurrencyById("bitcoin"),
exchange: null,
value: 0.5
},
{
currency: getCryptoCurrencyById("litecoin"),
exchange: "BINANCE",
value: 2
},
{
currency: getCryptoCurrencyById("ethereum"),
exchange: "BINANCE",
value: 1
},
{
currency: getCryptoCurrencyById("zencash"),
exchange: "BINANCE",
value: 10
}
]
};
const reducers: { [_: string]: (State, *) => State } = {
SET_COUNTERVALUE_CURRENCY: (state, { currency }) => ({
...state,
countervalueCurrency: currency
}),
SET_COUNTERVALUE_EXCHANGE: (state, { exchange }) => ({
...state,
countervalueExchange: exchange
}),
ADD_ROW: state => ({
...state,
rows: state.rows.concat({
currency: pick(listCryptoCurrencies()),
exchange: null,
value: 1
})
}),
SET_ROW: (state, action) => ({
...state,
rows: state.rows.map((row, i) => {
if (i !== action.index) return row;
return { ...row, ...action.patch };
})
}),
SET_EXCHANGE_PAIRS: (state, action) => {
state = { ...state };
// re-set the countervalueExchange if it is in the pairs
const globalPair = action.pairs.find(
p =>
p.from.ticker === state.intermediaryCurrency.ticker &&
p.to.ticker === state.countervalueCurrency.ticker
);
if (globalPair) {
state.countervalueExchange = globalPair.exchange;
}
// For all row, also synchronize with potential updates
state.rows = state.rows.map(row => {
if (!row.currency) return row;
const el = action.pairs.find(
p =>
row.currency &&
p.from.ticker === row.currency.ticker &&
p.to.ticker === state.intermediaryCurrency.ticker
);
if (el) {
return { ...row, exchange: el.exchange };
}
return row;
});
return state;
}
};
export default (state: State = initialState, action: *) => {
const reducer = reducers[action.type];
return (reducer && reducer(state, action)) || state;
};
export const appSelector = (state: *): State => state.app;
export const pairsSelector = createSelector(appSelector, (state: State) => {
const array = [];
const {
countervalueCurrency,
countervalueExchange,
intermediaryCurrency,
rows
} = state;
array.push({
from: intermediaryCurrency,
to: countervalueCurrency,
exchange: countervalueExchange
});
for (const { currency, exchange } of rows) {
if (currency && currency.ticker !== intermediaryCurrency.ticker) {
array.push({ from: currency, to: intermediaryCurrency, exchange });
}
}
return array;
});