import { createReducer } from 'utils/functions' ; import { TypeKeys } from './constants' ; import { Node , AddNodeAction , RemoveNodeAction , EditNodeAction , SelectNodeAction } from './actions' ; export const defaultNode = 'MyCryptoAPI' ; export interface NodeState { selectedNode : string ; nodes : Node [ ] ; } export const INITIAL_STATE = { selectedNode : defaultNode , nodes : [ { name : defaultNode , url : 'https://monero.mycryptoapi.com' } ] } ; function addNode ( state , action ) { return { ... state , nodes : [ ... state . nodes , action . payload ] } ; } function editNode ( state , action ) { const { index , node } = action . payload ; const nodes = [ ... state . nodes . slice ( 0 , index ) , node , ... state . nodes . slice ( index + 1 ) ] ; return { ... state , nodes } ; } function selectNode ( state , action ) { return { ... state , selectedNode : ! ! action . payload ? action . payload : defaultNode } ; } function removeNode ( state , action ) { const { payload } = action ; const nodes = [ ... state . nodes . filter ( ( _ , i ) => i !== payload ) ] ; return { ... state , nodes } ; } export const nodeReducer = createReducer ( INITIAL_STATE , { [ TypeKeys . ADD_NODE ] : addNode , [ TypeKeys . EDIT_NODE ] : editNode , [ TypeKeys . SELECT_NODE ] : selectNode , [ TypeKeys . REMOVE_NODE ] : removeNode } ) ;