Feat: login done
This commit is contained in:
140
plays/register.go
Normal file
140
plays/register.go
Normal file
@@ -0,0 +1,140 @@
|
||||
package plays
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"gitea.konchin.com/ytshih/inp2025/game/models"
|
||||
"github.com/charmbracelet/bubbles/textinput"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
type Register struct {
|
||||
*Base
|
||||
|
||||
focusIndex int
|
||||
inputs []textinput.Model
|
||||
}
|
||||
|
||||
func NewRegister(base *Base) *Register {
|
||||
m := Register{
|
||||
Base: base,
|
||||
focusIndex: 0,
|
||||
inputs: make([]textinput.Model, 2),
|
||||
}
|
||||
|
||||
for i := range m.inputs {
|
||||
t := textinput.New()
|
||||
t.CharLimit = 32
|
||||
t.Width = 20
|
||||
|
||||
switch i {
|
||||
case 0:
|
||||
t.Placeholder = "Username"
|
||||
t.Focus()
|
||||
case 1:
|
||||
t.Placeholder = "Password"
|
||||
t.EchoMode = textinput.EchoPassword
|
||||
t.EchoCharacter = '•'
|
||||
}
|
||||
|
||||
m.inputs[i] = t
|
||||
}
|
||||
return &m
|
||||
}
|
||||
|
||||
func (m *Register) Init() tea.Cmd {
|
||||
return tea.Batch(tea.ClearScreen, textinput.Blink)
|
||||
}
|
||||
|
||||
func (m *Register) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
if _, cmd := m.Base.Update(msg); cmd != nil {
|
||||
return m, cmd
|
||||
}
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
switch msg.String() {
|
||||
case "tab", "shift+tab", "enter", "up", "down":
|
||||
s := msg.String()
|
||||
|
||||
if s == "enter" && m.focusIndex == len(m.inputs)-1 {
|
||||
return m, tea.Quit
|
||||
}
|
||||
|
||||
if s == "up" || s == "shift+tab" {
|
||||
m.focusIndex--
|
||||
} else {
|
||||
m.focusIndex++
|
||||
}
|
||||
|
||||
if m.focusIndex >= len(m.inputs) {
|
||||
m.focusIndex = 0
|
||||
} else if m.focusIndex < 0 {
|
||||
m.focusIndex = len(m.inputs) - 1
|
||||
}
|
||||
|
||||
cmds := make([]tea.Cmd, len(m.inputs))
|
||||
for i := 0; i < len(m.inputs); i++ {
|
||||
if i == m.focusIndex {
|
||||
cmds[i] = m.inputs[i].Focus()
|
||||
continue
|
||||
}
|
||||
m.inputs[i].Blur()
|
||||
}
|
||||
|
||||
return m, tea.Batch(cmds...)
|
||||
}
|
||||
}
|
||||
|
||||
cmd := m.updateInputs(msg)
|
||||
return m, cmd
|
||||
}
|
||||
|
||||
func (m *Register) updateInputs(msg tea.Msg) tea.Cmd {
|
||||
cmds := make([]tea.Cmd, len(m.inputs))
|
||||
|
||||
for i := range m.inputs {
|
||||
m.inputs[i], cmds[i] = m.inputs[i].Update(msg)
|
||||
}
|
||||
|
||||
return tea.Batch(cmds...)
|
||||
}
|
||||
|
||||
func (m *Register) View() string {
|
||||
var b strings.Builder
|
||||
|
||||
fmt.Fprintf(&b, "User register:\n")
|
||||
for i := range m.inputs {
|
||||
b.WriteString(m.inputs[i].View())
|
||||
if i < len(m.inputs) {
|
||||
b.WriteRune('\n')
|
||||
}
|
||||
}
|
||||
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (m *Register) Next(queue *[]*tea.Program) error {
|
||||
resp, err := m.Base.client.R().
|
||||
SetBody(models.User{
|
||||
Username: m.inputs[0].Value(),
|
||||
Password: m.inputs[1].Value(),
|
||||
}).
|
||||
Post("/auth/register")
|
||||
|
||||
switch resp.StatusCode() {
|
||||
case http.StatusOK:
|
||||
*queue = append(*queue,
|
||||
tea.NewProgram(NewLogin(m.Base)))
|
||||
case http.StatusBadRequest:
|
||||
*queue = append(*queue,
|
||||
tea.NewProgram(NewRedirect("Username already exist")))
|
||||
*queue = append(*queue,
|
||||
tea.NewProgram(NewRegister(m.Base)))
|
||||
case http.StatusInternalServerError:
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user