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 = {
|
2021-04-04 09:10:55 +02:00
|
|
|
coinSwaps: [],
|
2021-03-25 12:24:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default handleActions(
|
|
|
|
{
|
2021-04-04 09:10:55 +02:00
|
|
|
[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-04-04 09:10:55 +02:00
|
|
|
|
2021-03-25 12:24:49 +01:00
|
|
|
return {
|
2021-04-04 09:10:55 +02:00
|
|
|
coinSwaps: newCoinSwaps,
|
2021-03-25 12:24:49 +01:00
|
|
|
};
|
|
|
|
},
|
2021-04-04 09:10:55 +02: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 {
|
2021-04-04 09:10:55 +02:00
|
|
|
coinSwaps: newCoinSwaps,
|
2021-03-25 12:24:49 +01:00
|
|
|
};
|
|
|
|
},
|
|
|
|
[LBRY_REDUX_ACTIONS.USER_STATE_POPULATE]: (
|
|
|
|
state: CoinSwapState,
|
2021-04-04 09:10:55 +02:00
|
|
|
action: { data: { coinSwaps: ?Array<CoinSwapInfo> } }
|
2021-03-25 12:24:49 +01:00
|
|
|
) => {
|
2021-04-04 09:10:55 +02: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,
|
2021-04-04 09:10:55 +02:00
|
|
|
coinSwaps: sanitizedCoinSwaps && sanitizedCoinSwaps.length ? sanitizedCoinSwaps : state.coinSwaps,
|
2021-03-25 12:24:49 +01:00
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
|
|
|
defaultState
|
|
|
|
);
|