From 7d83a8dc50389e32b71b8e08c92f05a5736a6fd8 Mon Sep 17 00:00:00 2001 From: ripp4rd0c Date: Sat, 16 Mar 2024 20:33:57 +0200 Subject: [PATCH] missing delete entry functionality added --- tui/commands.go | 11 +++++++++-- tui/entry.go | 21 ++++++++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/tui/commands.go b/tui/commands.go index bda189d..b533f37 100644 --- a/tui/commands.go +++ b/tui/commands.go @@ -13,7 +13,7 @@ import ( const defaultEditor = "vim" -/* PROJECTS */ +/* ENTRIES */ func openEditorCmd() tea.Cmd { file, err := os.CreateTemp(os.TempDir(), "") @@ -51,7 +51,14 @@ func (m Entry) createEntryCmd(file *os.File) tea.Cmd { } } -/* ENTRIES */ +func (m Entry) deleteEntryCmd(id uint) tea.Cmd { + return func() tea.Msg { + constants.Er.DeleteEntryByID(id) + return m.setupEntries() + } +} + +/* PROJECTS */ func createProjectCmd(name string, pr *project.GormRepository) tea.Cmd { return func() tea.Msg { diff --git a/tui/entry.go b/tui/entry.go index cca3f9c..ec9daf7 100644 --- a/tui/entry.go +++ b/tui/entry.go @@ -77,7 +77,14 @@ func (m *Entry) setViewportContent() { if len(m.entries) == 0 { content = "There are no entries for this project :)" } else { - content = entry.FormatEntry(m.entries[m.paginator.Page]) + var entryNum int + if m.paginator.Page >= len(m.entries) && len(m.entries) > 0 { + entryNum = m.paginator.Page - 1 + } else { + entryNum = m.paginator.Page + } + + content = entry.FormatEntry(m.entries[entryNum]) } str, err := glamour.Render(content, "dark") if err != nil { @@ -116,6 +123,18 @@ func (m Entry) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case key.Matches(msg, constants.Keymap.Quit): m.quitting = true return m, tea.Quit + // add delete entry functionality + case key.Matches(msg, constants.Keymap.Delete): + if len(m.entries) > 0 { + currEntryId := m.entries[m.paginator.Page].ID + + // if i'm on the last page, go back one page, else it will crash + if m.paginator.Page == len(m.entries)-1 { + m.paginator.PrevPage() + } + + return m, m.deleteEntryCmd(currEntryId) + } } }