view: updated home page to include recently added images

This commit is contained in:
Tigor Hutasuhut 2024-04-27 22:07:20 +07:00
parent 62e3f9778e
commit f8921b0cc9
8 changed files with 94 additions and 17 deletions

View file

@ -196,7 +196,7 @@ func (api *API) downloadSubredditImage(ctx context.Context, post reddit.Post, su
DeviceID: omit.From(device.ID),
Title: omit.From(post.GetTitle()),
PostID: omit.From(post.GetID()),
PostURL: omit.From(post.GetImageURL()),
PostURL: omit.From(post.GetPostURL()),
PostCreated: omit.From(post.GetCreated().Format(time.RFC3339)),
PostName: omit.From(post.GetName()),
Poster: omit.From(post.GetAuthor()),
@ -373,7 +373,7 @@ func (api *API) copyImageToTempDir(ctx context.Context, img reddit.PostImage) (t
split := strings.Split(url.Path, "/")
imageFilename := split[len(split)-1]
tmpDirname := path.Join(os.TempDir(), "redmage")
err = os.MkdirAll(tmpDirname, 0777)
err = os.MkdirAll(tmpDirname, 0o777)
if err != nil {
return nil, errs.Wrapw(err, "failed to create temporary dir", "dir_name", tmpDirname)
}

View file

@ -354,7 +354,7 @@ func (post *Post) GetPermalink() string {
}
func (post *Post) GetPostURL() string {
return fmt.Sprintf("https://reddit.com/%s", post.Data.Permalink)
return fmt.Sprintf("https://reddit.com%s", post.Data.Permalink)
}
func (post *Post) GetID() string {

View file

@ -1,9 +1,8 @@
POST http://localhost:8080/api/v1/subreddits HTTP/1.1
Host: localhost:8080
Content-Length: 109
{
"name": "animemidriff",
"name": "CultureImpact",
"enable_schedule": 1,
"schedule": "@daily",
"countback": 300

View file

@ -1,8 +1,7 @@
POST http://localhost:8080/api/v1/subreddits/start HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Content-Length: 35
{
"subreddit": "fantasymoe"
"subreddit": "CultureImpact"
}

View file

@ -1,8 +1,12 @@
package routes
import (
"encoding/json"
"net/http"
"time"
"github.com/tigorlazuardi/redmage/api"
"github.com/tigorlazuardi/redmage/pkg/errs"
"github.com/tigorlazuardi/redmage/pkg/log"
"github.com/tigorlazuardi/redmage/views"
"github.com/tigorlazuardi/redmage/views/homeview"
@ -14,16 +18,34 @@ func (routes *Routes) PageHome(rw http.ResponseWriter, r *http.Request) {
vc := views.NewContext(routes.Config, r)
params := parseSubredditListQuery(r)
listSubredditParams := parseSubredditListQuery(r)
list, err := routes.API.ListSubreddits(ctx, params)
list, err := routes.API.ListSubreddits(ctx, listSubredditParams)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
log.New(ctx).Err(err).Error("failed to list subreddits")
code, message := errs.HTTPMessage(err)
rw.WriteHeader(code)
_ = json.NewEncoder(rw).Encode(map[string]string{"error": message})
return
}
imageListParams := api.ImageListParams{}
imageListParams.FillFromQuery(r.URL.Query())
imageListParams.CreatedAt = time.Now().Add(-time.Hour * 24 * 3) // images in the last 3 days.
imageList, err := routes.API.ImagesListWithDevicesAndSubreddits(ctx, imageListParams)
if err != nil {
log.New(ctx).Err(err).Error("failed to list subreddits")
code, message := errs.HTTPMessage(err)
rw.WriteHeader(code)
_ = json.NewEncoder(rw).Encode(map[string]string{"error": message})
return
}
data := homeview.Data{
SubredditsList: list,
Error: err,
SubredditsList: list,
RecentlyAddedImages: imageList,
Error: err,
}
if err := homeview.Home(vc, data).Render(ctx, rw); err != nil {

View file

@ -3,7 +3,7 @@ package components
import "github.com/tigorlazuardi/redmage/views"
templ Body(c *views.Context) {
<body class="bg-base-100 min-h-screen" hx-ext="response-targets">
<body class="bg-base-100 min-h-screen w-screen" hx-ext="response-targets">
@Navigation(c) {
<div class="flex">
@Navbar(c)

View file

@ -5,8 +5,9 @@ import "github.com/tigorlazuardi/redmage/views"
import "github.com/tigorlazuardi/redmage/api"
type Data struct {
SubredditsList api.ListSubredditsResult
Error error
SubredditsList api.ListSubredditsResult
RecentlyAddedImages api.ImageListResult
Error error
}
templ Home(c *views.Context, data Data) {
@ -24,10 +25,15 @@ templ Home(c *views.Context, data Data) {
}
}
templ home(c *views.Context, data Data) {
templ home(_ *views.Context, data Data) {
<div class="prose">
<section>
<section class="mb-4 mx-auto">
<h1>Recently Added</h1>
<div class="flex gap-4 flex-wrap pb-4 pr-8 w-[100vw] lg:w-[80vw] justify-around">
for _, image := range data.RecentlyAddedImages.Images {
@RecentlyAddedImageCard(image, 0)
}
</div>
</section>
<section>
<h1>Subreddits</h1>

View file

@ -0,0 +1,51 @@
package homeview
import "github.com/tigorlazuardi/redmage/models"
import "github.com/tigorlazuardi/redmage/api/reddit"
import "fmt"
type ImageCardOption uint
func (o ImageCardOption) Has(opt ImageCardOption) bool {
return o&opt != 0
}
const (
HideTitle ImageCardOption = 1 << iota
HideDescription
HideSubreddit
HidePoster
)
templ RecentlyAddedImageCard(data *models.Image, opts ImageCardOption) {
<div class="not-prose card bg-base-100 shadow-xl w-[250px]">
<figure>
<a
href={ templ.URL(fmt.Sprintf("/img/%s", data.ImageRelativePath)) }
>
<img
class="object-contain w-[250px] h-[250px]"
src={ fmt.Sprintf("/img/%s", data.ThumbnailRelativePath) }
alt={ data.Title }
/>
</a>
</figure>
<div class="card-body">
if !opts.Has(HideTitle) {
<a
href={ templ.URL(data.PostURL) }
class="card-title font-normal underline text-base text-primary-content"
>{ data.Title }</a>
}
<a class="text-primary-content underline" href={ templ.URL(data.PosterURL) }>{ data.Poster }</a>
if data.R.Subreddit != nil {
if !opts.Has(HideSubreddit) {
<a
class="text-primary-content underline text-base"
href={ templ.URL(fmt.Sprintf("https://reddit.com/%s/%s", reddit.SubredditType(data.R.Subreddit.Subtype), data.R.Subreddit.Name)) }
>{ data.R.Subreddit.Name } </a>
}
}
</div>
</div>
}