Skip to content

Commit

Permalink
fix(ui): state updates for online/offline
Browse files Browse the repository at this point in the history
  • Loading branch information
PhearZero committed Nov 21, 2024
1 parent db34376 commit c66b330
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
6 changes: 0 additions & 6 deletions ui/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@ import (
"context"
"github.com/algorandfoundation/hack-tui/internal/test"
uitest "github.com/algorandfoundation/hack-tui/ui/internal/test"
"net/http"
"testing"
"time"
)

func Intercept(ctx context.Context, req *http.Request) error {
req.Response = &http.Response{}
return nil
}

func Test_GenerateCmd(t *testing.T) {
client := test.GetClient(false)
fn := GenerateCmd("ABC", time.Second*60, uitest.GetState(client))
Expand Down
14 changes: 14 additions & 0 deletions ui/modal/controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package modal

import (
"github.com/algorandfoundation/hack-tui/internal"
"github.com/algorandfoundation/hack-tui/ui/app"
"github.com/algorandfoundation/hack-tui/ui/modals/generate"
"github.com/algorandfoundation/hack-tui/ui/style"
Expand All @@ -27,6 +28,19 @@ func (m ViewModel) HandleMessage(msg tea.Msg) (*ViewModel, tea.Cmd) {
m.Open = true
m.exceptionModal.Message = msg.Error()
m.SetType(app.ExceptionModal)
case internal.StateModel:
m.State = &msg
m.transactionModal.State = &msg
m.infoModal.State = &msg

if m.Type == app.TransactionModal && !m.transactionModal.Active {
if msg.Accounts[m.Address].Participation.VoteFirstValid == m.transactionModal.Participation.Key.VoteFirstValid {
m.SetActive(true)
m.infoModal.Active = true
m.SetType(app.InfoModal)
}

Check warning on line 41 in ui/modal/controller.go

View check run for this annotation

Codecov / codecov/patch

ui/modal/controller.go#L31-L41

Added lines #L31 - L41 were not covered by tests
}

case app.ModalEvent:
if msg.Type == app.InfoModal {
m.generateModal.SetStep(generate.AddressStep)
Expand Down
7 changes: 4 additions & 3 deletions ui/pages/keys/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ func (m ViewModel) HandleMessage(msg tea.Msg) (ViewModel, tea.Cmd) {
// When the State changes
case internal.StateModel:
m.Data = msg.ParticipationKeys
m.table.SetRows(m.makeRows(m.Data))
m.table.SetRows(*m.makeRows(m.Data))
m.Participation = msg.Accounts[m.Address].Participation
// When the Account is Selected
case app.AccountSelected:
m.Address = msg.Address
m.Participation = msg.Participation
m.table.SetRows(m.makeRows(m.Data))
m.table.SetRows(*m.makeRows(m.Data))
// When a confirmation Modal is finished deleting
case app.DeleteFinished:
internal.RemovePartKeyByID(m.Data, msg.Id)
m.table.SetRows(m.makeRows(m.Data))
m.table.SetRows(*m.makeRows(m.Data))
// When the user interacts with the render
case tea.KeyMsg:
switch msg.String() {
Expand Down
8 changes: 4 additions & 4 deletions ui/pages/keys/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func New(address string, keys *[]api.ParticipationKey) ViewModel {
// Create Table
m.table = table.New(
table.WithColumns(m.makeColumns(80)),
table.WithRows(m.makeRows(keys)),
table.WithRows(*m.makeRows(keys)),
table.WithFocused(true),
table.WithHeight(m.Height),
table.WithWidth(m.Width),
Expand Down Expand Up @@ -119,10 +119,10 @@ func (m ViewModel) makeColumns(width int) []table.Column {

// makeRows processes a slice of ParticipationKeys and returns a sorted slice of table rows
// filtered by the ViewModel's address.
func (m ViewModel) makeRows(keys *[]api.ParticipationKey) []table.Row {
func (m ViewModel) makeRows(keys *[]api.ParticipationKey) *[]table.Row {
rows := make([]table.Row, 0)
if keys == nil || m.Address == "" {
return rows
return &rows
}

var activeId *string
Expand All @@ -147,5 +147,5 @@ func (m ViewModel) makeRows(keys *[]api.ParticipationKey) []table.Row {
sort.SliceStable(rows, func(i, j int) bool {
return rows[i][0] < rows[j][0]
})
return rows
return &rows
}
2 changes: 2 additions & 0 deletions ui/viewport.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ func (m ViewportViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds = append(cmds, cmd)
m.keysPage, cmd = m.keysPage.HandleMessage(msg)
cmds = append(cmds, cmd)
m.modal, cmd = m.modal.HandleMessage(msg)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
case app.DeleteFinished:
if len(m.keysPage.Rows()) <= 1 {
Expand Down

0 comments on commit c66b330

Please sign in to comment.