Skip to content

Commit 84ab6c5

Browse files
committed
feat: Prototype message-driven flow for ItemAdded
ItemAdded now uses commands, improving internal consistency and laying the groundwork for better error handling/UI updates.
1 parent 1af51e5 commit 84ab6c5

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

internal/tui/app/model.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type AppModel struct {
3636
focusedView ViewName
3737
keys []key.Binding
3838
help help.Model
39+
errorMsg string
3940
}
4041

4142
func (a AppModel) Init() tea.Cmd {
@@ -79,7 +80,12 @@ func (a AppModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
7980
a.focusedView = ViewName(msg.ViewName)
8081
a.Views[a.focusedView].OnFocus()
8182
return a, nil
83+
case messages.ShowError:
84+
log.Error("user operation failed", "error", msg.Message)
85+
a.errorMsg = msg.Message
86+
return a, nil
8287
case tea.KeyMsg:
88+
a.errorMsg = ""
8389
switch {
8490
case key.Matches(msg, keybinds.Keys.Quit):
8591
return a, tea.Quit
@@ -97,6 +103,12 @@ func (a AppModel) View() string {
97103
header := a.Header()
98104
view := a.Views[a.focusedView].View()
99105
help := a.Help()
106+
107+
if a.errorMsg != "" {
108+
errorBar := styles.ErrorBarStyle.Width(a.width).Render("Error: " + a.errorMsg)
109+
return lipgloss.JoinVertical(lipgloss.Top, header, view, errorBar, help, footer)
110+
}
111+
100112
return lipgloss.JoinVertical(lipgloss.Top, header, view, help, footer)
101113
}
102114

internal/tui/messages/messages.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@ type NavigateToView struct {
2323
ViewName string
2424
Data interface{}
2525
}
26+
27+
type RefreshItemsList struct{}
28+
29+
type ShowError struct {
30+
Message string
31+
}

internal/tui/styles/app-styles.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ var (
1313
TabHeadingActive = lipgloss.NewStyle().Background(accent).Foreground(headingForeground).Width(25).AlignHorizontal(lipgloss.Center).Border(lipgloss.NormalBorder(), false, false, false, true)
1414
HelpStyle = lipgloss.NewStyle().Padding(1, 0, 1, 2)
1515
AppHelpStyle = lipgloss.NewStyle().Padding(1, 0).Foreground(helpFG)
16+
ErrorBarStyle = lipgloss.NewStyle().Background(lipgloss.Color("#FF0000")).Foreground(lipgloss.Color("#FFFFFF")).Padding(0, 1)
1617
)

internal/tui/views/collections-view.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,18 @@ func (c CollectionsView) Update(msg tea.Msg) (ViewInterface, tea.Cmd) {
5353
c.list, cmd = c.list.Update(msg)
5454
cmds = append(cmds, cmd)
5555
case messages.ItemAdded:
56-
c.manager.Create(context.Background(), msg.Item)
56+
_, err := c.manager.Create(context.Background(), msg.Item)
57+
if err != nil {
58+
return c, func() tea.Msg {
59+
return messages.ShowError{Message: err.Error()}
60+
}
61+
}
62+
return c, func() tea.Msg {
63+
return messages.RefreshItemsList{}
64+
}
65+
case messages.RefreshItemsList:
66+
c.list.RefreshItems()
67+
return c, nil
5768
case messages.ItemEdited:
5869
c.manager.Update(context.Background(), msg.ItemID, msg.Item)
5970
case messages.DeleteItem:

0 commit comments

Comments
 (0)