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

53 lines
1.6 KiB
JavaScript
Raw Normal View History

2021-03-25 12:24:49 +01:00
// @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: [],
2021-03-25 12:24:49 +01:00
};
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,
});
2021-03-25 12:24:49 +01:00
}
2021-03-25 12:24:49 +01:00
return {
coinSwaps: newCoinSwaps,
2021-03-25 12:24:49 +01:00
};
},
[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);
2021-03-25 12:24:49 +01:00
return {
coinSwaps: newCoinSwaps,
2021-03-25 12:24:49 +01:00
};
},
[LBRY_REDUX_ACTIONS.USER_STATE_POPULATE]: (
state: CoinSwapState,
action: { data: { coinSwaps: ?Array<CoinSwapInfo> } }
2021-03-25 12:24:49 +01:00
) => {
const { coinSwaps } = action.data;
const sanitizedCoinSwaps = coinSwaps && coinSwaps.filter((x) => typeof x.sendAddress === 'string');
2021-03-25 12:24:49 +01:00
return {
...state,
coinSwaps: sanitizedCoinSwaps && sanitizedCoinSwaps.length ? sanitizedCoinSwaps : state.coinSwaps,
2021-03-25 12:24:49 +01:00
};
},
},
defaultState
);