getting entries

This commit is contained in:
GitBluub
2024-05-05 19:44:27 +02:00
parent bed5152899
commit 5618eda530
7 changed files with 129 additions and 37 deletions
+46 -1
View File
@@ -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 {
+1 -3
View File
@@ -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) {
+34 -2
View File
@@ -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
+7 -7
View File
@@ -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 {
+8 -5
View File
@@ -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 {
+21
View File
@@ -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"),
),
}
}
+12 -19
View File
@@ -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"},