diff --git a/tui/cmd/http.go b/tui/cmd/http.go index f90f8a4..99297e2 100644 --- a/tui/cmd/http.go +++ b/tui/cmd/http.go @@ -138,23 +138,36 @@ func register(username string, password string, email string) tea.Cmd { type getEntriesSuccessMsg []models.Entry func getEntries(jwt *string) tea.Cmd { + log.Print("yey") return func() tea.Msg { + log.Print("oula") url := fmt.Sprintf("%s/entries", serverUrl) - req, _ := http.NewRequest(http.MethodGet, url, nil) + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + log.Print(err) + return http.ErrMissingBoundary + } if jwt == nil { + log.Print("uhu?") return missingJwtMsg{} } req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", *jwt)) req.Header.Add("Content-type", "application/json") + log.Print("b") data, err := getData(req) + log.Print("c") if err != nil { + log.Print(err) return httpErrorMsg(err) } var entries []models.Entry err = json.Unmarshal(data, &entries) if err != nil { + log.Print(err) return httpErrorMsg(err) } + log.Print("a") + log.Print(entries) return getEntriesSuccessMsg(entries) } } @@ -182,6 +195,38 @@ func getFeeds(jwt *string) tea.Cmd { } } +type addFeedSuccessMsg struct{} + +func addFeed(jwt *string, link string, tags []string) tea.Cmd { + return func() tea.Msg { + + url := fmt.Sprintf("%s/feeds", serverUrl) + body := struct { + Link string `json:"link"` + Tags []string `json:"tags"` + }{ + Link: link, Tags: tags, + } + out, err := json.Marshal(body) + if err != nil { + log.Fatal(err) + } + req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(out)) + req.Header.Add("Content-type", "application/json") + req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", *jwt)) + data, err := getData(req) + if err != nil { + return err + } + var addFeedResp AuthRes + err = json.Unmarshal(data, &addFeedResp) + if err != nil { + return httpErrorMsg(err) + } + return addFeedSuccessMsg{} + } +} + type ignorePostSuccessMsg uuid.UUID func ignorePost(jwt *string, e models.Entry) tea.Cmd { diff --git a/tui/cmd/main.go b/tui/cmd/main.go index 5853b0c..8e2368a 100644 --- a/tui/cmd/main.go +++ b/tui/cmd/main.go @@ -54,9 +54,7 @@ func New() *Model { } func (m Model) getEverything() tea.Cmd { - return func() tea.Msg { - return tea.Batch(getEntries(m.Auth.Jwt)) // getTags, getFeeds) - } + return tea.Batch(getEntries(m.Auth.Jwt)) // getTags, getFeeds) } func (m *Model) initList(width int, height int) { diff --git a/tui/cmd/update.go b/tui/cmd/update.go index c0da9fe..228b465 100644 --- a/tui/cmd/update.go +++ b/tui/cmd/update.go @@ -4,11 +4,12 @@ import ( "strings" "github.com/charmbracelet/bubbles/key" + "github.com/charmbracelet/bubbles/list" tea "github.com/charmbracelet/bubbletea" huh "github.com/charmbracelet/huh" - . "github.com/zoryia/vex/tui/models" . "github.com/zoryia/vex/tui/pages" + "github.com/zoryia/vex/tui/pages/feeds" ) func (m Model) LoginUpdate(msg tea.Msg) (tea.Model, []tea.Cmd) { @@ -29,6 +30,13 @@ func (m Model) GlobalUpdate(msg tea.Msg) (Model, tea.Cmd) { m.Feeds.List.SetWidth(msg.Width) m.Feeds.List.SetHeight(msg.Height) + case getEntriesSuccessMsg: + var entries []list.Item + for _, e := range msg { + entries = append(entries, e) + } + m.list.SetItems(entries) + case invalidJwtMsg: m.Auth.Jwt = new(string) m.page = LOGIN @@ -170,7 +178,31 @@ func entriesUpdate(m Model, msg tea.Msg) (Model, tea.Cmd) { return m, tea.Batch(cmds...) } func feedsUpdate(m Model, msg tea.Msg) (Model, tea.Cmd) { - return m, nil + var cmds []tea.Cmd + var cmd tea.Cmd + + switch msg := msg.(type) { + case tea.KeyMsg: + switch { + case key.Matches(msg, feeds.FeedsKeyMaps().AddFeed): + m.Feeds.AddFeed.Run() + } + } + registerForm, cmd := m.Auth.RegisterForm.Update(msg) + if f, ok := registerForm.(*huh.Form); ok { + m.Auth.RegisterForm = f + cmds = append(cmds, cmd) + } + + if m.Auth.RegisterForm.State == huh.StateCompleted { + url := m.Auth.RegisterForm.GetString("url") + tags := m.Auth.RegisterForm.Get("tags").([]string) + cmds = append(cmds, addFeed(m.Auth.Jwt, url, tags)) + } + m.Feeds.List, cmd = m.Feeds.List.Update(msg) + cmds = append(cmds, cmd) + + return m, tea.Batch(cmds...) } func tagsUpdate(m Model, msg tea.Msg) (Model, tea.Cmd) { return m, nil diff --git a/tui/models/entry.go b/tui/models/entry.go index 625bd77..ae33fbe 100644 --- a/tui/models/entry.go +++ b/tui/models/entry.go @@ -12,14 +12,14 @@ type Entry struct { ArticleTitle string `json:"title"` Content string `json:"content"` Link string `json:"link"` - Date time.Time `json:"time"` + Date time.Time `json:"date"` - Author *string `json:"author"` // author not always specified - IsRead bool `json:"isRead"` - IsBookmarked bool `json:"IsBookmarked"` - IsIgnored bool `json:"isIgnored"` - IsReadLater bool `json:"isReadLater"` - Feed Feed `json:"feed"` + Authors []string `json:"authors"` // author not always specified + IsRead bool `json:"isRead"` + IsBookmarked bool `json:"IsBookmarked"` + IsIgnored bool `json:"isIgnored"` + IsReadLater bool `json:"isReadLater"` + Feed Feed `json:"feed"` } func (e Entry) FilterValue() string { diff --git a/tui/models/feed.go b/tui/models/feed.go index baae1db..c947d6b 100644 --- a/tui/models/feed.go +++ b/tui/models/feed.go @@ -2,16 +2,19 @@ package models import ( "fmt" + "time" "github.com/google/uuid" ) type Feed struct { - Id uuid.UUID `json:"id"` - Name string `json:"name"` - Url string `json:"url"` - FaviconUrl string `json:"faviconUrl"` - Tags []string `json:"tags"` + Id uuid.UUID `json:"id"` + Name string `json:"name"` + Url string `json:"link"` + FaviconUrl string `json:"faviconUrl"` + Tags []string `json:"tags"` + AddedDate time.Time `json:"addedDate"` + SubmitterId uuid.UUID `json:"submitterId"` } func (f Feed) FilterValue() string { diff --git a/tui/pages/feeds/keymaps.go b/tui/pages/feeds/keymaps.go new file mode 100644 index 0000000..bab8407 --- /dev/null +++ b/tui/pages/feeds/keymaps.go @@ -0,0 +1,21 @@ +package feeds + +import "github.com/charmbracelet/bubbles/key" + +type ListKeyMap struct { + GoToPosts key.Binding + AddFeed key.Binding +} + +func FeedsKeyMaps() *ListKeyMap { + return &ListKeyMap{ + GoToPosts: key.NewBinding( + key.WithKeys("p"), + key.WithHelp("p", "Go to posts"), + ), + AddFeed: key.NewBinding( + key.WithKeys("a"), + key.WithHelp("a", "Add feed"), + ), + } +} diff --git a/tui/pages/feeds/model.go b/tui/pages/feeds/model.go index be4060f..0b51e08 100644 --- a/tui/pages/feeds/model.go +++ b/tui/pages/feeds/model.go @@ -18,30 +18,23 @@ func (m Model) View() string { return m.List.View() } -func FeedsKeys() []key.Binding { - return []key.Binding{ - key.NewBinding( - key.WithKeys("a"), - key.WithHelp("a", "Add a new feed"), - ), - key.NewBinding( - key.WithKeys("p"), - key.WithHelp("p", "Go to posts"), - ), - key.NewBinding( - key.WithKeys("x", "d"), - key.WithHelp("x/d", "Ignore feed"), - ), - } -} - func initFeedsList() list.Model { feeds := list.New([]list.Item{}, list.NewDefaultDelegate(), 0, 0) feeds.Title = "Feeds" feeds.SetFilteringEnabled(false) feeds.DisableQuitKeybindings() - feeds.AdditionalShortHelpKeys = FeedsKeys - feeds.AdditionalFullHelpKeys = FeedsKeys + feeds.AdditionalShortHelpKeys = func() []key.Binding { + return []key.Binding{ + FeedsKeyMaps().AddFeed, + FeedsKeyMaps().GoToPosts, + } + } + feeds.AdditionalFullHelpKeys = func() []key.Binding { + return []key.Binding{ + FeedsKeyMaps().AddFeed, + FeedsKeyMaps().GoToPosts, + } + } feeds.SetItems([]list.Item{ models.Feed{Id: uuid.UUID{}, Tags: []string{"Devops", "Kubernetes"}, Name: "zwindler", Url: "zwindler.blog", FaviconUrl: "zwindler.blog.favicon"}, models.Feed{Id: uuid.UUID{}, Tags: []string{"Devops", "Kubernetes"}, Name: "zwindler", Url: "zwindler.blog", FaviconUrl: "zwindler.blog.favicon"},