diff --git a/model.go b/model.go index 388000d..d32bf09 100644 --- a/model.go +++ b/model.go @@ -4,34 +4,40 @@ import ( "github.com/dannypsnl/redux/v2/rematch" ) -type PeersState struct { - Peers []Peer - Current int +type AppState struct { + Node NodeInfo // info about current node + Peers []Peer // list of peers for the node + Current int // currently selected peer } -type PeersModel struct { +type AppModel struct { rematch.Reducer - State PeersState + State AppState } -func (m *PeersModel) Current(state PeersState, peerIndex int) PeersState { +func (m *AppModel) SetInfo(s AppState, node NodeInfo) AppState { + s.Node = node + return s +} + +func (m *AppModel) Current(s AppState, peerIndex int) AppState { // NOTE Not sure if I should just ignore invalid values or panic - if peerIndex >= 0 && peerIndex < len(state.Peers) { - state.Current = peerIndex + if peerIndex >= 0 && peerIndex < len(s.Peers) { + s.Current = peerIndex } - return state + return s } -func (m *PeersModel) Update(state PeersState, peers []Peer) PeersState { +func (m *AppModel) Update(s AppState, peers []Peer) AppState { // The argument is a copy so we can modify it and return it - state.Peers = peers + s.Peers = peers // if not current peer is set use first one - if state.Current == -1 && len(peers) > 0 { - state.Current = 0 + if s.Current == -1 && len(peers) > 0 { + s.Current = 0 } // if set but doesn't exist in the list move up - if state.Current >= len(peers) { - state.Current = len(peers) - 1 + if s.Current >= len(peers) { + s.Current = len(peers) - 1 } - return state + return s } diff --git a/state.go b/state.go index 331f6e6..7c54fc2 100644 --- a/state.go +++ b/state.go @@ -10,7 +10,7 @@ import ( // This might need renaming, since it also contains the Client. // I need the client to make the RPC calls. type State struct { - Reducer *PeersModel + Reducer *AppModel Store *store.Store Client *StatusGoClient updatePeers *rematch.Action @@ -19,8 +19,8 @@ type State struct { func NewState(client *StatusGoClient) *State { // Generate the reducer from our model. - Reducer := &PeersModel{ - State: PeersState{ + Reducer := &AppModel{ + State: AppState{ Peers: make([]Peer, 0), Current: -1, // Should mean non selected. }, @@ -52,8 +52,8 @@ func (s *State) GetCurrent() *Peer { func (s *State) SetCurrent(index int) { s.Store.Dispatch(s.setCurrent.With(index)) } -func (s *State) GetState() PeersState { - return s.Store.StateOf(s.Reducer).(PeersState) +func (s *State) GetState() AppState { + return s.Store.StateOf(s.Reducer).(AppState) } // For fetching current state of peers from status-go