2017-09-25 02:06:28 +00:00
|
|
|
import {
|
2017-07-13 21:02:39 +00:00
|
|
|
AddCustomTokenAction,
|
2017-09-25 02:06:28 +00:00
|
|
|
CustomTokenAction,
|
2017-07-13 21:02:39 +00:00
|
|
|
RemoveCustomTokenAction
|
|
|
|
} from 'actions/customTokens';
|
2017-09-25 02:06:28 +00:00
|
|
|
import { TypeKeys } from 'actions/customTokens/constants';
|
2018-02-12 20:43:07 +00:00
|
|
|
import { Token } from 'types/network';
|
2017-07-13 21:02:39 +00:00
|
|
|
|
|
|
|
export type State = Token[];
|
|
|
|
|
2017-07-27 17:05:09 +00:00
|
|
|
export const INITIAL_STATE: State = [];
|
2017-07-13 21:02:39 +00:00
|
|
|
|
|
|
|
function addCustomToken(state: State, action: AddCustomTokenAction): State {
|
|
|
|
if (state.find(token => token.symbol === action.payload.symbol)) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
return [...state, action.payload];
|
|
|
|
}
|
|
|
|
|
2017-12-18 23:29:26 +00:00
|
|
|
function removeCustomToken(state: State, action: RemoveCustomTokenAction): State {
|
2017-07-13 21:02:39 +00:00
|
|
|
return state.filter(token => token.symbol !== action.payload);
|
|
|
|
}
|
|
|
|
|
2017-12-18 23:29:26 +00:00
|
|
|
export function customTokens(state: State = INITIAL_STATE, action: CustomTokenAction): State {
|
2017-07-13 21:02:39 +00:00
|
|
|
switch (action.type) {
|
2017-09-25 02:06:28 +00:00
|
|
|
case TypeKeys.CUSTOM_TOKEN_ADD:
|
2017-07-13 21:02:39 +00:00
|
|
|
return addCustomToken(state, action);
|
2017-09-25 02:06:28 +00:00
|
|
|
case TypeKeys.CUSTOM_TOKEN_REMOVE:
|
2017-07-13 21:02:39 +00:00
|
|
|
return removeCustomToken(state, action);
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|