2017-12-18 23:29:26 +00:00
|
|
|
import { DeterministicWalletAction, DeterministicWalletData } from 'actions/deterministicWallets';
|
2017-09-25 02:06:28 +00:00
|
|
|
import { TypeKeys } from 'actions/deterministicWallets/constants';
|
2017-08-28 17:43:57 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export interface State {
|
|
|
|
wallets: DeterministicWalletData[];
|
|
|
|
desiredToken: string | undefined;
|
|
|
|
}
|
2017-08-28 17:43:57 +00:00
|
|
|
|
|
|
|
export const INITIAL_STATE: State = {
|
|
|
|
wallets: [],
|
|
|
|
desiredToken: ''
|
|
|
|
};
|
|
|
|
|
|
|
|
export function deterministicWallets(
|
|
|
|
state: State = INITIAL_STATE,
|
|
|
|
action: DeterministicWalletAction
|
|
|
|
): State {
|
|
|
|
switch (action.type) {
|
2017-09-25 02:06:28 +00:00
|
|
|
case TypeKeys.DW_SET_WALLETS:
|
2017-08-28 17:43:57 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
wallets: action.payload
|
|
|
|
};
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
case TypeKeys.DW_SET_DESIRED_TOKEN:
|
2017-08-28 17:43:57 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
desiredToken: action.payload
|
|
|
|
};
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
case TypeKeys.DW_UPDATE_WALLET:
|
2017-08-28 17:43:57 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
wallets: updateWalletValues(state.wallets, action.payload)
|
|
|
|
};
|
|
|
|
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-12 19:45:52 +00:00
|
|
|
function updateWalletValues(
|
|
|
|
wallets: DeterministicWalletData[],
|
|
|
|
newWallet: Partial<DeterministicWalletData>
|
|
|
|
) {
|
2017-08-28 17:43:57 +00:00
|
|
|
return wallets.map(w => {
|
|
|
|
if (w.address === newWallet.address) {
|
|
|
|
return {
|
|
|
|
...w,
|
|
|
|
...newWallet
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return w;
|
|
|
|
});
|
|
|
|
}
|