Skip to content

Commit

Permalink
Merge pull request #30 from algorandfoundation/feat/generate-modal-va…
Browse files Browse the repository at this point in the history
…lidation

Feat/generate modal validation
  • Loading branch information
tasosbit authored Dec 3, 2024
2 parents 6266dd9 + 03895ba commit 382d97a
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 33 deletions.
9 changes: 9 additions & 0 deletions internal/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"time"

"github.com/algorand/go-algorand-sdk/v2/types"
"github.com/algorandfoundation/hack-tui/api"
)

Expand Down Expand Up @@ -118,3 +119,11 @@ func AccountsFromState(state *StateModel, t Time, client api.ClientWithResponses

return values
}

func ValidateAddress(address string) bool {
_, err := types.DecodeAddress(address)
if err != nil {
return false
}
return true
}
22 changes: 19 additions & 3 deletions ui/modals/generate/controller.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package generate

import (
"strconv"
"time"

"github.com/algorandfoundation/hack-tui/internal"
"github.com/algorandfoundation/hack-tui/ui/app"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"strconv"
"time"
)

func (m ViewModel) Init() tea.Cmd {
Expand All @@ -23,13 +25,16 @@ func (m *ViewModel) SetStep(step Step) {
case AddressStep:
m.Controls = "( esc to cancel )"
m.Title = DefaultTitle
m.InputError = ""
m.BorderColor = DefaultBorderColor
case DurationStep:
m.Controls = "( (s)witch range )"
m.Title = "Validity Range"
m.InputTwo.SetValue("")
m.InputTwo.Focus()
m.InputTwo.PromptStyle = focusedStyle
m.InputTwo.TextStyle = focusedStyle
m.InputTwoError = ""
m.Input.Blur()
case WaitingStep:
m.Controls = ""
Expand Down Expand Up @@ -72,11 +77,22 @@ func (m ViewModel) HandleMessage(msg tea.Msg) (*ViewModel, tea.Cmd) {
case "enter":
switch m.Step {
case AddressStep:
addr := m.Input.Value()
if !internal.ValidateAddress(addr) {
m.InputError = "Error: invalid address"
return &m, nil
}
m.InputError = ""
m.SetStep(DurationStep)
return &m, app.EmitShowModal(app.GenerateModal)
case DurationStep:
val, err := strconv.Atoi(m.InputTwo.Value())
if err != nil || val <= 0 {
m.InputTwoError = "Error: duration must be a positive number"
return &m, nil
}
m.InputTwoError = ""
m.SetStep(WaitingStep)
val, _ := strconv.Atoi(m.InputTwo.Value())
var dur time.Duration
switch m.Range {
case Day:
Expand Down
7 changes: 5 additions & 2 deletions ui/modals/generate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
func Test_New(t *testing.T) {
m := New("ABC", test.GetState(nil))

m.SetAddress("ACB")
m.SetAddress("TUIDKH2C7MUHZDD77MAMUREJRKNK25SYXB7OAFA6JFBB24PEL5UX4S4GUU")

if m.Address != "ABC" {
if m.Address != "TUIDKH2C7MUHZDD77MAMUREJRKNK25SYXB7OAFA6JFBB24PEL5UX4S4GUU" {
t.Error("Did not set address")
}

Expand Down Expand Up @@ -44,6 +44,7 @@ func Test_New(t *testing.T) {

m.SetStep(DurationStep)
m.Range = Week
m.InputTwo.SetValue("1")
m, cmd = m.HandleMessage(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("enter"),
Expand All @@ -54,6 +55,7 @@ func Test_New(t *testing.T) {

m.SetStep(DurationStep)
m.Range = Month
m.InputTwo.SetValue("1")
m, cmd = m.HandleMessage(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("enter"),
Expand All @@ -64,6 +66,7 @@ func Test_New(t *testing.T) {

m.SetStep(DurationStep)
m.Range = Year
m.InputTwo.SetValue("1")
m, cmd = m.HandleMessage(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("enter"),
Expand Down
40 changes: 22 additions & 18 deletions ui/modals/generate/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ type ViewModel struct {
Width int
Height int

Address string
Input *textinput.Model
InputTwo *textinput.Model
Spinner *spinner.Model
Step Step
Range Range
Address string
Input *textinput.Model
InputError string
InputTwo *textinput.Model
InputTwoError string
Spinner *spinner.Model
Step Step
Range Range

Title string
Controls string
Expand All @@ -43,7 +45,7 @@ type ViewModel struct {
cursorMode cursor.Mode
}

func (m ViewModel) SetAddress(address string) {
func (m *ViewModel) SetAddress(address string) {
m.Address = address
m.Input.SetValue(address)
}
Expand All @@ -57,25 +59,27 @@ func New(address string, state *internal.StateModel) *ViewModel {
input2 := textinput.New()

m := ViewModel{
Address: address,
State: state,
Input: &input,
InputTwo: &input2,
Step: AddressStep,
Range: Day,
Title: DefaultTitle,
Controls: DefaultControls,
BorderColor: DefaultBorderColor,
Address: address,
State: state,
Input: &input,
InputError: "",
InputTwo: &input2,
InputTwoError: "",
Step: AddressStep,
Range: Day,
Title: DefaultTitle,
Controls: DefaultControls,
BorderColor: DefaultBorderColor,
}
input.Cursor.Style = cursorStyle
input.CharLimit = 68
input.CharLimit = 58
input.Placeholder = "Wallet Address"
input.Focus()
input.PromptStyle = focusedStyle
input.TextStyle = focusedStyle

input2.Cursor.Style = cursorStyle
input2.CharLimit = 68
input2.CharLimit = 58
input2.Placeholder = "Length of time"

input2.PromptStyle = noStyle
Expand Down
4 changes: 3 additions & 1 deletion ui/modals/generate/testdata/Test_Snapshot/Waiting.golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@

Generating Participation Keys...

Please wait. This operation can take a few minutes.
Please wait. This operation can take a few minutes.

18 changes: 17 additions & 1 deletion ui/modals/generate/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package generate

import (
"fmt"

"github.com/algorandfoundation/hack-tui/ui/style"
"github.com/charmbracelet/lipgloss"
)

Expand All @@ -17,6 +19,12 @@ func (m ViewModel) View() string {
m.Input.View(),
"",
)
if m.InputError != "" {
render = lipgloss.JoinVertical(lipgloss.Left,
render,
style.Red.Render(m.InputError),
)
}
case DurationStep:
render = lipgloss.JoinVertical(lipgloss.Left,
"",
Expand All @@ -26,11 +34,19 @@ func (m ViewModel) View() string {
m.InputTwo.View(),
"",
)
if m.InputTwoError != "" {
render = lipgloss.JoinVertical(lipgloss.Left,
render,
style.Red.Render(m.InputTwoError),
)
}
case WaitingStep:
render = lipgloss.JoinVertical(lipgloss.Left,
"",
"Generating Participation Keys...",
"",
"Please wait. This operation can take a few minutes.")
"Please wait. This operation can take a few minutes.",
"")
}

return lipgloss.NewStyle().Width(70).Render(render)
Expand Down
12 changes: 7 additions & 5 deletions ui/modals/transaction/testdata/Test_Snapshot/NotVisible.golden
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Sign this transaction to register your account keys:

QR Code too large to display. Please adjust terminal dimensions or font size.
-or-
Click here to sign via Lora.
Sign this transaction to register your account keys:

Mobile QR is available but it does not fit on screen.
Adjust terminal dimensions or font size to display.

-or-
Click here to sign via Lora.
4 changes: 3 additions & 1 deletion ui/modals/transaction/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ func (m ViewModel) View() string {
lipgloss.Center,
intro,
"",
style.Red.Render(ansi.Wordwrap("QR Code too large to display. Please adjust terminal dimensions or font size.", m.Width, " ")),
style.Red.Render(ansi.Wordwrap("Mobile QR is available but it does not fit on screen.", m.Width, " ")),
style.Red.Render(ansi.Wordwrap("Adjust terminal dimensions or font size to display.", m.Width, " ")),
"",
"-or-",
loraText,
)
Expand Down
6 changes: 5 additions & 1 deletion ui/viewport.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ func (m ViewportViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
return m, nil
case "ctrl+c":
return m, tea.Quit
case "q":
// Close the app when anything other than generate modal is visible
if !m.modal.Open || (m.modal.Open && m.modal.Type != app.GenerateModal) {
return m, tea.Quit
}
}

case tea.WindowSizeMsg:
Expand Down
2 changes: 1 addition & 1 deletion ui/viewport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func Test_ViewportViewRender(t *testing.T) {
// Send quit key
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("ctrl+c"),
Runes: []rune("q"),
})

tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
Expand Down

0 comments on commit 382d97a

Please sign in to comment.