Skip to content

Commit

Permalink
update: On delete a saved filter user have to confirm their action
Browse files Browse the repository at this point in the history
  • Loading branch information
Koustavd18 committed Aug 13, 2024
1 parent 3ba3e28 commit 9c805ea
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions pkg/model/savedFilters.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import (
"fmt"
"io"
"net/http"
"pb/pkg/config"
"strings"
"time"

"pb/pkg/config"

"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
Expand All @@ -33,9 +34,14 @@ import (
const (
applyFilterButton = "a"
deleteFilterButton = "d"
confirmDelete = "y"
cancelDelete = "n"
)

var docStyle = lipgloss.NewStyle().Margin(1, 2)
var (
docStyle = lipgloss.NewStyle().Margin(1, 2)
deleteFilterState bool
)

// FilterDetails represents the structure of filter data
type FilterDetails struct {
Expand All @@ -52,11 +58,11 @@ type Item struct {
}

var (
titleStyles = lipgloss.NewStyle().PaddingLeft(0).Bold(true).Foreground(lipgloss.Color("9"))
queryStyle = lipgloss.NewStyle().PaddingLeft(0).Foreground(lipgloss.Color("7"))
itemStyle = lipgloss.NewStyle().PaddingLeft(4).Foreground(lipgloss.Color("8"))
// selectedItemStyle = lipgloss.NewStyle().PaddingLeft(4).Foreground(lipgloss.Color("170"))
titleStyles = lipgloss.NewStyle().PaddingLeft(0).Bold(true).Foreground(lipgloss.Color("9"))
queryStyle = lipgloss.NewStyle().PaddingLeft(0).Foreground(lipgloss.Color("7"))
itemStyle = lipgloss.NewStyle().PaddingLeft(4).Foreground(lipgloss.Color("8"))
selectedItemStyle = lipgloss.NewStyle().PaddingLeft(1).Foreground(lipgloss.AdaptiveColor{Light: "16", Dark: "226"})
confirmModal = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.AdaptiveColor{Light: "16", Dark: "226"})
)

type itemDelegate struct{}
Expand Down Expand Up @@ -90,6 +96,18 @@ func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list
}

func (d itemDelegate) ShortHelp() []key.Binding {
if deleteFilterState {
return []key.Binding{
key.NewBinding(
key.WithKeys(confirmDelete),
key.WithHelp(confirmDelete, confirmModal.Render("confirm delete")),
),
key.NewBinding(
key.WithKeys(cancelDelete),
key.WithHelp(cancelDelete, confirmModal.Render("cancel delete")),
),
}
}
return []key.Binding{
key.NewBinding(
key.WithKeys(applyFilterButton),
Expand Down Expand Up @@ -157,9 +175,18 @@ func (m modelFilter) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Quit
}
if msg.String() == "d" {
// selectedFilterDelete = m.list.SelectedItem().(Item)
deleteFilterState = true
return m, nil

}
if msg.String() == "y" {
selectedFilterDelete = m.list.SelectedItem().(Item)
return m, tea.Quit

}
if msg.String() == "n" {
deleteFilterState = false
return m, nil
}
case tea.WindowSizeMsg:
h, v := docStyle.GetFrameSize()
Expand Down Expand Up @@ -264,4 +291,4 @@ func FilterToApply() Item {
// FilterToDelete returns the selected filter by user in the interactive list to delete
func FilterToDelete() Item {
return selectedFilterDelete
}
}

0 comments on commit 9c805ea

Please sign in to comment.