lbry-desktop/src/renderer/redux/reducers/wallet.js

149 lines
3.7 KiB
JavaScript
Raw Normal View History

import * as ACTIONS from 'constants/action_types';
2017-04-22 15:17:01 +02:00
2017-06-06 06:21:55 +02:00
const reducers = {};
const receiveAddress = localStorage.getItem('receiveAddress');
2017-04-23 07:55:47 +02:00
const buildDraftTransaction = () => ({
amount: undefined,
2017-06-06 23:19:12 +02:00
address: undefined,
2017-06-06 06:21:55 +02:00
});
2017-04-23 07:55:47 +02:00
2017-04-22 15:17:01 +02:00
const defaultState = {
balance: undefined,
blocks: {},
transactions: {},
2017-04-22 15:17:01 +02:00
fetchingTransactions: false,
receiveAddress,
2017-04-22 15:17:01 +02:00
gettingNewAddress: false,
2017-06-06 23:19:12 +02:00
draftTransaction: buildDraftTransaction(),
2017-09-17 22:33:52 +02:00
sendingSupport: false,
2017-06-06 06:21:55 +02:00
};
2017-04-22 15:17:01 +02:00
reducers[ACTIONS.FETCH_TRANSACTIONS_STARTED] = state =>
Object.assign({}, state, {
2017-06-06 23:19:12 +02:00
fetchingTransactions: true,
});
2017-04-22 15:17:01 +02:00
reducers[ACTIONS.FETCH_TRANSACTIONS_COMPLETED] = (state, action) => {
2017-12-13 22:36:30 +01:00
const byId = Object.assign({}, state.transactions);
2017-06-06 23:19:12 +02:00
const { transactions } = action.data;
2017-06-06 23:19:12 +02:00
transactions.forEach(transaction => {
byId[transaction.txid] = transaction;
});
2017-04-22 15:17:01 +02:00
return Object.assign({}, state, {
transactions: byId,
2017-06-06 23:19:12 +02:00
fetchingTransactions: false,
});
2017-06-06 06:21:55 +02:00
};
2017-04-22 15:17:01 +02:00
reducers[ACTIONS.GET_NEW_ADDRESS_STARTED] = state =>
Object.assign({}, state, {
2017-06-06 23:19:12 +02:00
gettingNewAddress: true,
});
2017-04-22 15:17:01 +02:00
reducers[ACTIONS.GET_NEW_ADDRESS_COMPLETED] = (state, action) => {
2017-06-06 23:19:12 +02:00
const { address } = action.data;
2017-04-22 15:17:01 +02:00
localStorage.setItem('receiveAddress', address);
2017-04-22 15:17:01 +02:00
return Object.assign({}, state, {
gettingNewAddress: false,
2017-06-06 23:19:12 +02:00
receiveAddress: address,
});
2017-06-06 06:21:55 +02:00
};
2017-04-22 15:17:01 +02:00
reducers[ACTIONS.UPDATE_BALANCE] = (state, action) =>
Object.assign({}, state, {
2017-06-06 23:19:12 +02:00
balance: action.data.balance,
});
2017-04-22 15:17:01 +02:00
reducers[ACTIONS.CHECK_ADDRESS_IS_MINE_STARTED] = state =>
Object.assign({}, state, {
2017-06-06 23:19:12 +02:00
checkingAddressOwnership: true,
});
2017-04-22 16:01:57 +02:00
reducers[ACTIONS.CHECK_ADDRESS_IS_MINE_COMPLETED] = state =>
Object.assign({}, state, {
2017-06-06 23:19:12 +02:00
checkingAddressOwnership: false,
});
2017-04-22 16:01:57 +02:00
reducers[ACTIONS.SET_DRAFT_TRANSACTION_AMOUNT] = (state, action) => {
2017-06-06 23:19:12 +02:00
const oldDraft = state.draftTransaction;
2017-04-23 07:55:47 +02:00
const newDraft = Object.assign({}, oldDraft, {
2017-06-06 23:19:12 +02:00
amount: parseFloat(action.data.amount),
});
2017-04-23 07:55:47 +02:00
return Object.assign({}, state, {
2017-06-06 23:19:12 +02:00
draftTransaction: newDraft,
});
2017-06-06 06:21:55 +02:00
};
2017-04-23 07:55:47 +02:00
reducers[ACTIONS.SET_DRAFT_TRANSACTION_ADDRESS] = (state, action) => {
2017-06-06 23:19:12 +02:00
const oldDraft = state.draftTransaction;
2017-04-23 07:55:47 +02:00
const newDraft = Object.assign({}, oldDraft, {
2017-06-06 23:19:12 +02:00
address: action.data.address,
});
2017-04-23 07:55:47 +02:00
return Object.assign({}, state, {
2017-06-06 23:19:12 +02:00
draftTransaction: newDraft,
});
2017-06-06 06:21:55 +02:00
};
2017-04-23 07:55:47 +02:00
reducers[ACTIONS.SEND_TRANSACTION_STARTED] = state => {
2017-04-23 07:55:47 +02:00
const newDraftTransaction = Object.assign({}, state.draftTransaction, {
2017-06-06 23:19:12 +02:00
sending: true,
});
2017-04-23 07:55:47 +02:00
return Object.assign({}, state, {
2017-06-06 23:19:12 +02:00
draftTransaction: newDraftTransaction,
});
2017-06-06 06:21:55 +02:00
};
2017-04-23 07:55:47 +02:00
reducers[ACTIONS.SEND_TRANSACTION_COMPLETED] = state =>
Object.assign({}, state, {
2017-06-06 23:19:12 +02:00
draftTransaction: buildDraftTransaction(),
});
2017-04-23 07:55:47 +02:00
reducers[ACTIONS.SEND_TRANSACTION_FAILED] = (state, action) => {
2017-04-23 07:55:47 +02:00
const newDraftTransaction = Object.assign({}, state.draftTransaction, {
sending: false,
2017-06-06 23:19:12 +02:00
error: action.data.error,
});
2017-04-23 07:55:47 +02:00
return Object.assign({}, state, {
2017-06-06 23:19:12 +02:00
draftTransaction: newDraftTransaction,
});
2017-06-06 06:21:55 +02:00
};
2017-04-23 07:55:47 +02:00
reducers[ACTIONS.SUPPORT_TRANSACTION_STARTED] = state =>
Object.assign({}, state, {
2017-09-17 22:33:52 +02:00
sendingSupport: true,
});
reducers[ACTIONS.SUPPORT_TRANSACTION_COMPLETED] = state =>
Object.assign({}, state, {
2017-09-17 22:33:52 +02:00
sendingSupport: false,
});
reducers[ACTIONS.SUPPORT_TRANSACTION_FAILED] = (state, action) =>
Object.assign({}, state, {
2017-09-17 22:33:52 +02:00
error: action.data.error,
sendingSupport: false,
});
reducers[ACTIONS.FETCH_BLOCK_SUCCESS] = (state, action) => {
const { block, block: { height } } = action.data;
const blocks = Object.assign({}, state.blocks);
blocks[height] = block;
return Object.assign({}, state, { blocks });
};
2017-04-22 15:17:01 +02:00
export default function reducer(state = defaultState, action) {
const handler = reducers[action.type];
if (handler) return handler(state, action);
return state;
}