lbry-desktop/ui/redux/reducers/coinSwap.js
infinite-persistence b2630f6f21 Save entire swap info instead of just the address.
- Users should be able to see the entered and promised amount, otherwise they might forget how much to send over.
- This change also prepares for the future upgrade to support multiple coins.
2021-04-13 14:02:25 -04:00

52 lines
1.6 KiB
JavaScript

// @flow
import * as ACTIONS from 'constants/action_types';
import { ACTIONS as LBRY_REDUX_ACTIONS } from 'lbry-redux';
import { handleActions } from 'util/redux-utils';
const defaultState: CoinSwapState = {
coinSwaps: [],
};
export default handleActions(
{
[ACTIONS.ADD_COIN_SWAP]: (state: CoinSwapState, action: CoinSwapAction): CoinSwapState => {
const { coinSwaps } = state;
const { coin, sendAddress, sendAmount, lbcAmount } = action.data;
let newCoinSwaps = coinSwaps.slice();
if (!newCoinSwaps.find((x) => x.sendAddress === sendAddress)) {
newCoinSwaps.push({
coin: coin,
sendAddress: sendAddress,
sendAmount: sendAmount,
lbcAmount: lbcAmount,
});
}
return {
coinSwaps: newCoinSwaps,
};
},
[ACTIONS.REMOVE_COIN_SWAP]: (state: CoinSwapState, action: CoinSwapRemoveAction): CoinSwapState => {
const { coinSwaps } = state;
const { sendAddress } = action.data;
let newCoinSwaps = coinSwaps.slice();
newCoinSwaps = newCoinSwaps.filter((x) => x.sendAddress !== sendAddress);
return {
coinSwaps: newCoinSwaps,
};
},
[LBRY_REDUX_ACTIONS.USER_STATE_POPULATE]: (
state: CoinSwapState,
action: { data: { coinSwaps: ?Array<CoinSwapInfo> } }
) => {
const { coinSwaps } = action.data;
const sanitizedCoinSwaps = coinSwaps && coinSwaps.filter((x) => typeof x.sendAddress === 'string');
return {
...state,
coinSwaps: sanitizedCoinSwaps && sanitizedCoinSwaps.length ? sanitizedCoinSwaps : state.coinSwaps,
};
},
},
defaultState
);