Browse Source

feature(txs): setup getTransactions and hook up to new reducer

renovate/lint-staged-8.x
Jack Mallers 7 years ago
parent
commit
a6111c3dd5
  1. 5
      app/lnd/methods/index.js
  2. 4
      app/reducers/index.js
  3. 5
      app/reducers/ipc.js
  4. 93
      app/reducers/transaction.js
  5. 3
      app/routes/activity/components/Activity.js
  6. 2
      app/routes/activity/containers/ActivityContainer.js

5
app/lnd/methods/index.js

@ -49,6 +49,11 @@ export default function (lnd, event, msg, data) {
)
.catch(error => console.log('channels error: ', error))
break
case 'transactions':
// Data looks like { transactions: [] }
walletController.getTransactions(lnd)
.then(transactionsData => event.sender.send('receiveTransactions', transactionsData))
.catch(error => console.log('transactions error: ', error))
case 'payments':
// Data looks like { payments: [] }
paymentsController.listPayments(lnd)

4
app/reducers/index.js

@ -11,6 +11,7 @@ import form from './form'
import invoice from './invoice'
import modal from './modal'
import address from './address'
import transaction from './transaction'
const rootReducer = combineReducers({
router,
@ -23,7 +24,8 @@ const rootReducer = combineReducers({
form,
invoice,
modal,
address
address,
transaction
})
export default rootReducer

5
app/reducers/ipc.js

@ -22,6 +22,7 @@ import {
import { receivePayments, paymentSuccessful, sendSuccessful, sendCoinsError } from './payment'
import { receiveInvoices, createdInvoice, receiveFormInvoice } from './invoice'
import { receiveBalance } from './balance'
import { receiveTransactions } from './transaction'
// Import all receiving IPC event handlers and pass them into createIpc
const ipc = createIpc({
@ -58,7 +59,9 @@ const ipc = createIpc({
disconnectSuccess,
receiveAddress,
receiveCryptocurrency
receiveCryptocurrency,
receiveTransactions
})
export default ipc

93
app/reducers/transaction.js

@ -0,0 +1,93 @@
import { createSelector } from 'reselect'
import { ipcRenderer } from 'electron'
import { btc, usd } from '../utils'
import { setForm, resetForm } from './form'
import { showModal } from './modal'
// ------------------------------------
// Constants
// ------------------------------------
export const GET_TRANSACTIONS = 'GET_TRANSACTIONS'
export const RECEIVE_TRANSACTIONS = 'RECEIVE_TRANSACTIONS'
export const SEND_TRANSACTION = 'SEND_TRANSACTION'
export const TRANSACTION_SUCCESSFULL = 'TRANSACTION_SUCCESSFULL'
export const TRANSACTION_FAILED = 'TRANSACTION_FAILED'
// ------------------------------------
// Actions
// ------------------------------------
export function getTransactions() {
return {
type: GET_TRANSACTIONS
}
}
export function sendTransaction() {
return {
type: SEND_TRANSACTION
}
}
// Send IPC event for payments
export const fetchTransactions = () => (dispatch) => {
dispatch(getTransactions())
ipcRenderer.send('lnd', { msg: 'transactions' })
}
// Receive IPC event for payments
export const receiveTransactions = (event, { transactions }) => dispatch => dispatch({ type: RECEIVE_TRANSACTIONS, transactions })
export const sendCoins = ({ value, addr, currency, rate }) => (dispatch) => {
const amount = currency === 'usd' ? btc.btcToSatoshis(usd.usdToBtc(value, rate)) : btc.btcToSatoshis(value)
dispatch(sendPayment())
ipcRenderer.send('lnd', { msg: 'sendCoins', data: { amount, addr } })
}
// Receive IPC event for successful payment
// TODO: Add payment to state, not a total re-fetch
export const transactionSuccessful = (event, { amount, addr, txid }) => (dispatch) => {
// Get the new list of transactions (TODO dont do an entire new fetch)
fetchTransactions()
// Close the form modal once the payment was succesful
dispatch(setForm({ modalOpen: false }))
// Show successful payment state
dispatch(showModal('SUCCESSFUL_SEND_COINS', { txid, amount, addr }))
// TODO: Add successful on-chain payment to payments list once payments list supports on-chain and LN
// dispatch({ type: PAYMENT_SUCCESSFULL, payment: { amount, addr, txid, pending: true } })
dispatch({ type: TRANSACTION_SUCCESSFULL })
// Reset the payment form
dispatch(resetForm())
}
export const transactionError = () => (dispatch) => {
dispatch({ type: PAYMENT_FAILED })
}
// ------------------------------------
// Action Handlers
// ------------------------------------
const ACTION_HANDLERS = {
[GET_TRANSACTIONS]: state => ({ ...state, transactionLoading: true }),
[SEND_TRANSACTION]: state => ({ ...state, sendingTransaction: true }),
[RECEIVE_TRANSACTIONS]: (state, { transactions }) => ({ ...state, transactionLoading: false, transactions }),
[TRANSACTION_SUCCESSFULL]: state => ({ ...state, sendingTransaction: false }),
[TRANSACTION_FAILED]: state => ({ ...state, sendingTransaction: false })
}
// ------------------------------------
// Reducer
// ------------------------------------
const initialState = {
sendingtransaction: false,
transactionLoading: false,
transactions: []
}
export default function transactionReducer(state = initialState, action) {
const handler = ACTION_HANDLERS[action.type]
return handler ? handler(state, action) : state
}

3
app/routes/activity/components/Activity.js

@ -14,10 +14,11 @@ class Activity extends Component {
}
componentWillMount() {
const { fetchPayments, fetchInvoices } = this.props
const { fetchPayments, fetchInvoices, fetchTransactions } = this.props
fetchPayments()
fetchInvoices()
fetchTransactions()
}
render() {

2
app/routes/activity/containers/ActivityContainer.js

@ -11,6 +11,7 @@ import {
fetchPayments,
paymentSelectors
} from '../../../reducers/payment'
import { fetchTransactions } from '../../../reducers/transaction'
import Activity from '../components/Activity'
const mapDispatchToProps = {
@ -18,6 +19,7 @@ const mapDispatchToProps = {
setInvoice,
fetchPayments,
fetchInvoices,
fetchTransactions,
searchInvoices
}

Loading…
Cancel
Save