Skip to content

Commit

Permalink
fix: simplifying
Browse files Browse the repository at this point in the history
  • Loading branch information
HashMapsData2Value committed Oct 31, 2024
1 parent bef9bef commit b8f4d0d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 28 deletions.
32 changes: 11 additions & 21 deletions ui/pages/keys/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,13 @@ func (m ViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

// Removes a participation key from the list of keys
func removePartKeyByID(slice []api.ParticipationKey, id string) []api.ParticipationKey {
for i, item := range slice {
func removePartKeyByID(slice *[]api.ParticipationKey, id string) {
for i, item := range *slice {
if item.Id == id {
return append(slice[:i], slice[i+1:]...)
*slice = append((*slice)[:i], (*slice)[i+1:]...)
return
}
}
return slice
}

func isPartKeyInList(partKey api.ParticipationKey, keys []api.ParticipationKey) bool {
if len(keys) == 0 {
return false
}

for _, key := range keys {
if key.Id == partKey.Id {
return true
}
}
return false
}

func (m ViewModel) HandleMessage(msg tea.Msg) (ViewModel, tea.Cmd) {
Expand All @@ -48,7 +35,13 @@ func (m ViewModel) HandleMessage(msg tea.Msg) (ViewModel, tea.Cmd) {
m.Address = msg.Address
m.table.SetRows(m.makeRows(m.Data))
case DeleteFinished:
if m.SelectedKeyToDelete == nil {
panic("SelectedKeyToDelete is unexpectedly nil")
}
removePartKeyByID(m.Data, m.SelectedKeyToDelete.Id)
m.SelectedKeyToDelete = nil
m.table.SetRows(m.makeRows(m.Data))

case tea.KeyMsg:
switch msg.String() {
case "enter":
Expand All @@ -64,10 +57,7 @@ func (m ViewModel) HandleMessage(msg tea.Msg) (ViewModel, tea.Cmd) {
return m, nil
case "y": // "Yes do delete" option in the delete confirmation modal
if m.SelectedKeyToDelete != nil {
m.KeysInDeletion = append(m.KeysInDeletion, *m.SelectedKeyToDelete)
keyToDelete := m.SelectedKeyToDelete
m.SelectedKeyToDelete = nil
return m, EmitDeleteKey(keyToDelete)
return m, EmitDeleteKey(m.SelectedKeyToDelete)
}
return m, nil
case "n": // "do NOT delete" option in the delete confirmation modal
Expand Down
6 changes: 0 additions & 6 deletions ui/pages/keys/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ type ViewModel struct {
Height int

SelectedKeyToDelete *api.ParticipationKey
KeysInDeletion []api.ParticipationKey

table table.Model
controls string
Expand Down Expand Up @@ -102,11 +101,6 @@ func (m ViewModel) makeRows(keys *[]api.ParticipationKey) []table.Row {
}
for _, key := range *keys {
if key.Address == m.Address {

if isPartKeyInList(key, m.KeysInDeletion) {
continue
}

rows = append(rows, table.Row{
key.Id,
key.Address,
Expand Down
16 changes: 15 additions & 1 deletion ui/viewport.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ func DeleteKey(client *api.ClientWithResponses, key keys.DeleteKey) tea.Cmd {
}
}

// Check if there are any participation keys available for the account
// Prevents navigation to the transaction page if there are no keys
func tGuard(m ViewportViewModel) bool {
if m.Data == nil || len(*m.Data.ParticipationKeys) == 0 {
panic("No participation keys available for this account. Please add a key first.")
errorMsg := "No participation keys available for this account. Please add a key first."

Check failure on line 66 in ui/viewport.go

View workflow job for this annotation

GitHub Actions / build

unreachable code
m.errorMsg = &errorMsg
return true
}

return false
}

// Init is a no-op
func (m ViewportViewModel) Init() tea.Cmd {
return nil
Expand Down Expand Up @@ -139,13 +152,14 @@ func (m ViewportViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case "t":
m.page = TransactionPage
// If there isn't a key already, select the first record for that account
if m.keysPage.SelectedKey() == nil && m.Data != nil {
if m.keysPage.SelectedKey() == nil {
data := *m.Data.ParticipationKeys
acct := m.accountsPage.SelectedAccount()
for i, key := range data {
if key.Address == acct.Address {
return m, keys.EmitKeySelected(&data[i])
}
return m, nil
}
}
// Navigate to the transaction page
Expand Down

0 comments on commit b8f4d0d

Please sign in to comment.