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

132 lines
3.3 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: 0,
transactions: [],
fetchingTransactions: false,
receiveAddress: address,
gettingNewAddress: false,
2017-06-06 23:19:12 +02:00
draftTransaction: buildDraftTransaction(),
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) {
2017-06-06 23:19:12 +02:00
const oldTransactions = Object.assign({}, state.transactions);
const byId = Object.assign({}, oldTransactions.byId);
const { transactions } = action.data;
2017-06-06 23:19:12 +02:00
transactions.forEach(transaction => {
byId[transaction.txid] = transaction;
});
const newTransactions = Object.assign({}, oldTransactions, {
2017-06-06 23:19:12 +02:00
byId: byId,
});
2017-04-22 15:17:01 +02:00
return Object.assign({}, state, {
transactions: newTransactions,
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-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;
}