lbry-desktop/ui/js/redux/reducers/wallet.js

158 lines
3.9 KiB
JavaScript
Raw Normal View History

2017-06-06 23:19:12 +02:00
import * as types from "constants/action_types";
2017-04-22 15:17:01 +02:00
2017-06-06 06:21:55 +02:00
const reducers = {};
2017-06-06 23:19:12 +02:00
const address = 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: address,
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[types.FETCH_TRANSACTIONS_STARTED] = function(state, action) {
return Object.assign({}, state, {
2017-06-06 23:19:12 +02:00
fetchingTransactions: true,
});
2017-06-06 06:21:55 +02:00
};
2017-04-22 15:17:01 +02:00
reducers[types.FETCH_TRANSACTIONS_COMPLETED] = function(state, action) {
let 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[types.GET_NEW_ADDRESS_STARTED] = function(state, action) {
return Object.assign({}, state, {
2017-06-06 23:19:12 +02:00
gettingNewAddress: true,
});
2017-06-06 06:21:55 +02:00
};
2017-04-22 15:17:01 +02:00
reducers[types.GET_NEW_ADDRESS_COMPLETED] = function(state, action) {
2017-06-06 23:19:12 +02:00
const { address } = action.data;
2017-04-22 15:17:01 +02:00
2017-06-06 23:19:12 +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[types.UPDATE_BALANCE] = function(state, action) {
return Object.assign({}, state, {
2017-06-06 23:19:12 +02:00
balance: action.data.balance,
});
2017-06-06 06:21:55 +02:00
};
2017-04-22 15:17:01 +02:00
2017-04-22 16:01:57 +02:00
reducers[types.CHECK_ADDRESS_IS_MINE_STARTED] = function(state, action) {
return Object.assign({}, state, {
2017-06-06 23:19:12 +02:00
checkingAddressOwnership: true,
});
2017-06-06 06:21:55 +02:00
};
2017-04-22 16:01:57 +02:00
reducers[types.CHECK_ADDRESS_IS_MINE_COMPLETED] = function(state, action) {
return Object.assign({}, state, {
2017-06-06 23:19:12 +02:00
checkingAddressOwnership: false,
});
2017-06-06 06:21:55 +02:00
};
2017-04-22 16:01:57 +02:00
2017-04-23 07:55:47 +02:00
reducers[types.SET_DRAFT_TRANSACTION_AMOUNT] = function(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[types.SET_DRAFT_TRANSACTION_ADDRESS] = function(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[types.SEND_TRANSACTION_STARTED] = function(state, action) {
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[types.SEND_TRANSACTION_COMPLETED] = function(state, action) {
return Object.assign({}, state, {
2017-06-06 23:19:12 +02:00
draftTransaction: buildDraftTransaction(),
});
2017-06-06 06:21:55 +02:00
};
2017-04-23 07:55:47 +02:00
reducers[types.SEND_TRANSACTION_FAILED] = function(state, action) {
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
2017-09-17 22:33:52 +02:00
reducers[types.SUPPORT_TRANSACTION_STARTED] = function(state, action) {
return Object.assign({}, state, {
sendingSupport: true,
});
};
reducers[types.SUPPORT_TRANSACTION_COMPLETED] = function(state, action) {
return Object.assign({}, state, {
sendingSupport: false,
});
};
reducers[types.SUPPORT_TRANSACTION_FAILED] = function(state, action) {
return Object.assign({}, state, {
error: action.data.error,
sendingSupport: false,
});
};
reducers[types.FETCH_BLOCK_SUCCESS] = (state, action) => {
const { block, block: { height } } = action.data,
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;
}