Skip to content

Commit

Permalink
fix: fix endless loop in project view
Browse files Browse the repository at this point in the history
  • Loading branch information
bashbunni committed Apr 3, 2022
1 parent dece9ba commit 40ead4f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
14 changes: 13 additions & 1 deletion frontend/projectui/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ func createProjectCmd(name string, pr *project.GormRepository) tea.Cmd {
func renameProjectCmd(id uint, pr *project.GormRepository, name string) tea.Cmd {
return func() tea.Msg {
pr.RenameProject(id, name)
return renameProjectMsg{}
projects, err := pr.GetAllProjects()
if err != nil {
return errMsg{err}
}
items := projectsToItems(projects)

return renameProjectMsg(items)
}
}

Expand All @@ -31,3 +37,9 @@ func deleteProjectCmd(id uint, pr *project.GormRepository) tea.Cmd {
return updateProjectListMsg{}
}
}

func selectProjectCmd(ActiveProjectID uint) tea.Cmd {
return func() tea.Msg {
return SelectMsg{ActiveProjectID: ActiveProjectID}
}
}
4 changes: 3 additions & 1 deletion frontend/projectui/messages.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package projectui

import "github.com/charmbracelet/bubbles/list"

type errMsg struct{ error } // TODO: have this implement Error()
type updateProjectListMsg struct{}
type renameProjectMsg struct{}
type renameProjectMsg []list.Item
27 changes: 8 additions & 19 deletions frontend/projectui/project_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ import (

// TODO: add multi-page navigation

var (
cmd tea.Cmd
cmds []tea.Cmd
)

type SelectMsg struct {
ActiveProjectID uint
}
Expand Down Expand Up @@ -59,6 +54,8 @@ func (m Model) Init() tea.Cmd {
}

func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
top, right, bottom, left := constants.DocStyle.GetMargin()
Expand All @@ -72,12 +69,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.list.SetItems(items)
m.mode = ""
case renameProjectMsg:
projects, err := m.pr.GetAllProjects()
if err != nil {
log.Fatal(err)
}
items := projectsToItems(projects)
m.list.SetItems(items)
m.list.SetItems(msg)
m.mode = ""
case tea.KeyMsg:
if m.input.Focused() {
Expand Down Expand Up @@ -109,20 +101,17 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case msg.String() == "ctrl+c":
return m, tea.Quit
case key.Matches(msg, constants.Keymap.Enter):
return m, func() tea.Msg {
return SelectMsg{ActiveProjectID: m.getActiveProjectID()}
}
return m, selectProjectCmd(m.getActiveProjectID())
case key.Matches(msg, constants.Keymap.Rename):
m.mode = "edit"
m.input.Focus()
cmds = append(cmds, textinput.Blink)
case key.Matches(msg, constants.Keymap.Delete):
items := m.list.Items()
activeItem := items[m.list.Index()]
cmds = append(cmds, deleteProjectCmd(activeItem.(project.Project).ID, m.pr))
cmds = append(cmds, deleteProjectCmd(m.getActiveProjectID(), m.pr))
default:
m.list, cmd = m.list.Update(msg)
cmds = append(cmds, cmd)
}
m.list, cmd = m.list.Update(msg)
cmds = append(cmds, cmd)
}
}
return m, tea.Batch(cmds...)
Expand Down
12 changes: 10 additions & 2 deletions frontend/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,27 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
m.project = projectModel
cmd = newCmd
return m, cmd
case entryView:
m.entry = *entryui.New(m.er, m.activeProjectID, p)
newEntry, newCmd := m.entry.Update(msg)
entryModel, ok := newEntry.(entryui.Model)
if !ok {
panic("could not perform assertion on projectui model")
panic("could not perform assertion on entryui model")
}
m.entry = entryModel
cmd = newCmd
return m, cmd
}
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}

func (m mainModel) View() string {
return m.project.View()
switch m.state {
case entryView:
return m.entry.View()
default:
return m.project.View()
}
}

0 comments on commit 40ead4f

Please sign in to comment.