Skip to content

Commit

Permalink
feat: add a confirmation before deleting a filter (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
Koustavd18 authored Aug 13, 2024
1 parent 3ba3e28 commit e7eca08
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions pkg/model/savedFilters.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,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 = false
)

// FilterDetails represents the structure of filter data
type FilterDetails struct {
Expand All @@ -52,11 +57,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 +95,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 +174,19 @@ func (m modelFilter) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Quit
}
if msg.String() == "d" {
deleteFilterState = true
return m, nil
}
if msg.String() != "d" {
deleteFilterState = false
}
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

0 comments on commit e7eca08

Please sign in to comment.