-
Notifications
You must be signed in to change notification settings - Fork 504
/
reducers.js
131 lines (127 loc) · 4.63 KB
/
reducers.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
import { assoc, assocPath, merge, lensProp, over, append, compose, dropLast } from 'ramda'
import * as AT from './actionTypes'
import Remote from '../../../remote'
const INITIAL_STATE = {
addresses: Remote.NotAsked,
fee: Remote.NotAsked,
info: Remote.NotAsked,
latest_block: Remote.NotAsked,
rates: Remote.NotAsked,
transactions: [],
transactions_fiat: {},
spendable_balance: Remote.NotAsked,
unspendable_balance: Remote.NotAsked,
transaction_history: Remote.NotAsked
}
const bitcoinReducer = (state = INITIAL_STATE, action) => {
const { type, payload } = action
switch (type) {
case AT.SET_BITCOIN_LATEST_BLOCK: {
return assocPath(['latest_block'], Remote.Success(payload), state)
}
case AT.FETCH_BITCOIN_DATA_LOADING: {
const data = {
addresses: Remote.Loading,
info: Remote.Loading,
latest_block: Remote.Loading
}
return merge(state, data)
}
case AT.FETCH_BITCOIN_DATA_SUCCESS: {
const { addresses, info, latest_block } = payload
const data = {
addresses: Remote.Success(addresses),
info: Remote.Success(info),
latest_block: Remote.Success(latest_block)
}
return merge(state, data)
}
case AT.FETCH_BITCOIN_DATA_FAILURE: {
const data = {
addresses: Remote.Failure(payload),
info: Remote.Failure(payload),
latest_block: Remote.Failure(payload)
}
return merge(state, data)
}
case AT.FETCH_BITCOIN_FEE_LOADING: {
return assoc('fee', Remote.Loading, state)
}
case AT.FETCH_BITCOIN_FEE_SUCCESS: {
return assoc('fee', Remote.Success(payload), state)
}
case AT.FETCH_BITCOIN_FEE_FAILURE: {
return assoc('fee', Remote.Failure(payload), state)
}
case AT.FETCH_BITCOIN_RATES_LOADING: {
return assoc('rates', Remote.Loading, state)
}
case AT.FETCH_BITCOIN_RATES_SUCCESS: {
return assoc('rates', Remote.Success(payload), state)
}
case AT.FETCH_BITCOIN_RATES_FAILURE: {
return assoc('rates', Remote.Failure(payload), state)
}
case AT.FETCH_BITCOIN_TRANSACTIONS_LOADING: {
const { reset } = payload
return reset
? assoc('transactions', [Remote.Loading], state)
: over(lensProp('transactions'), append(Remote.Loading), state)
}
case AT.FETCH_BITCOIN_TRANSACTIONS_SUCCESS: {
const { reset, transactions } = payload
return reset
? assoc('transactions', [Remote.Success(transactions)], state)
: over(lensProp('transactions'), compose(append(Remote.Success(transactions)), dropLast(1)), state)
}
case AT.FETCH_BITCOIN_TRANSACTIONS_FAILURE: {
return over(lensProp('transactions'), dropLast(1), state)
}
case AT.FETCH_BITCOIN_FIAT_AT_TIME_LOADING: {
const { hash, currency } = payload
return assocPath(['transactions_fiat', hash, currency], Remote.Loading, state)
}
case AT.FETCH_BITCOIN_FIAT_AT_TIME_SUCCESS: {
const { hash, currency, data } = payload
return assocPath(['transactions_fiat', hash, currency], Remote.Success(data), state)
}
case AT.FETCH_BITCOIN_FIAT_AT_TIME_FAILURE: {
const { hash, currency, error } = payload
return assocPath(['transactions_fiat', hash, currency], Remote.Success(error), state)
}
case AT.FETCH_BITCOIN_TRANSACTION_HISTORY_LOADING: {
return assoc('transaction_history', Remote.Loading, state)
}
case AT.FETCH_BITCOIN_TRANSACTION_HISTORY_SUCCESS: {
return assoc('transaction_history', Remote.Success(payload), state)
}
case AT.FETCH_BITCOIN_TRANSACTION_HISTORY_FAILURE: {
return assoc('transaction_history', Remote.Failure(payload), state)
}
case AT.FETCH_BITCOIN_SPENDABLE_BALANCE_LOADING: {
return assoc('spendable_balance', Remote.Loading, state)
}
case AT.FETCH_BITCOIN_SPENDABLE_BALANCE_SUCCESS: {
const { wallet } = payload
const balance = wallet.final_balance
return assoc('spendable_balance', Remote.Success(balance), state)
}
case AT.FETCH_BITCOIN_SPENDABLE_BALANCE_FAILURE: {
return assoc('spendable_balance', Remote.Failure(payload), state)
}
case AT.FETCH_BITCOIN_UNSPENDABLE_BALANCE_LOADING: {
return assoc('unspendable_balance', Remote.Loading, state)
}
case AT.FETCH_BITCOIN_UNSPENDABLE_BALANCE_SUCCESS: {
const { wallet } = payload
const balance = wallet.final_balance
return assoc('unspendable_balance', Remote.Success(balance), state)
}
case AT.FETCH_BITCOIN_UNSPENDABLE_BALANCE_FAILURE: {
return assoc('unspendable_balance', Remote.Failure(payload), state)
}
default:
return state
}
}
export default bitcoinReducer