schedule-history: if current is latest, fetch more history if not full
Some checks failed
/ push (push) Has been cancelled

This commit is contained in:
Tigor Hutasuhut 2024-06-05 14:20:21 +07:00
parent bc005f8b30
commit 0c623d1bf1
3 changed files with 46 additions and 18 deletions

View file

@ -50,21 +50,21 @@ func (params ScheduleHistoryListParams) CountQuery() (expr []bob.Mod[*dialect.Se
if params.Subreddit != "" { if params.Subreddit != "" {
expr = append(expr, models.SelectWhere.ScheduleHistories.Subreddit.EQ(params.Subreddit)) expr = append(expr, models.SelectWhere.ScheduleHistories.Subreddit.EQ(params.Subreddit))
} }
if !params.Time.IsZero() {
if params.Reversed {
expr = append(expr,
models.SelectWhere.ScheduleHistories.CreatedAt.GTE(params.Time.Unix()),
)
} else {
expr = append(expr, models.SelectWhere.ScheduleHistories.CreatedAt.LT(params.Time.Unix()))
}
}
return expr return expr
} }
func (params ScheduleHistoryListParams) Query() (expr []bob.Mod[*dialect.SelectQuery]) { func (params ScheduleHistoryListParams) Query() (expr []bob.Mod[*dialect.SelectQuery]) {
expr = append(expr, params.CountQuery()...) expr = append(expr, params.CountQuery()...)
if !params.Time.IsZero() {
if params.Reversed {
expr = append(expr,
models.SelectWhere.ScheduleHistories.CreatedAt.GT(params.Time.Unix()),
)
} else {
expr = append(expr, models.SelectWhere.ScheduleHistories.CreatedAt.LT(params.Time.Unix()))
}
}
if params.Limit > 0 { if params.Limit > 0 {
expr = append(expr, sm.Limit(params.Limit)) expr = append(expr, sm.Limit(params.Limit))
} }

View file

@ -3,6 +3,7 @@ package routes
import ( import (
"net/http" "net/http"
"github.com/tigorlazuardi/redmage/api"
"github.com/tigorlazuardi/redmage/pkg/errs" "github.com/tigorlazuardi/redmage/pkg/errs"
"github.com/tigorlazuardi/redmage/pkg/log" "github.com/tigorlazuardi/redmage/pkg/log"
"github.com/tigorlazuardi/redmage/views" "github.com/tigorlazuardi/redmage/views"
@ -29,13 +30,40 @@ func (routes *Routes) PageScheduleHistory(rw http.ResponseWriter, req *http.Requ
} }
return return
} }
data.ScheduleHistories = result data.ScheduleHistories = result
if latest, _ := routes.API.ScheduleHistoryLatest(ctx); latest != nil { latest, err := routes.API.ScheduleHistoryLatest(ctx)
if first := data.ScheduleHistories.GetFirst(); first != nil { if err != nil {
if first.ID == latest.ID { log.New(ctx).Err(err).Error("Failed to list schedule histories")
data.IsCurrent = true code, message := errs.HTTPMessage(err)
rw.WriteHeader(code)
data.Error = message
if err := schedulehistories.View(c, data).Render(ctx, rw); err != nil {
log.New(ctx).Err(err).Error("Failed to render schedule histories view")
}
return
}
if first := data.ScheduleHistories.GetFirst(); first != nil {
if first.ID == latest.ID {
data.IsCurrent = true
}
if data.IsCurrent && len(data.ScheduleHistories.Schedules) < int(data.ScheduleHistories.Total) {
data.Params = api.ScheduleHistoryListParams{
Subreddit: data.Params.Subreddit,
Limit: data.Params.Limit,
}
data.ScheduleHistories, err = routes.API.ScheduleHistoryList(ctx, data.Params)
if err != nil {
log.New(ctx).Err(err).Error("Failed to list schedule histories")
code, message := errs.HTTPMessage(err)
rw.WriteHeader(code)
data.Error = message
if err := schedulehistories.View(c, data).Render(ctx, rw); err != nil {
log.New(ctx).Err(err).Error("Failed to render schedule histories view")
}
return
} }
} }
} }

View file

@ -45,7 +45,7 @@ templ Content(c *views.Context, data Data) {
} }
if len(data.ScheduleHistories.Schedules) > 0 { if len(data.ScheduleHistories.Schedules) > 0 {
for _, history := range data.ScheduleHistories.SplitByDay() { for _, history := range data.ScheduleHistories.SplitByDay() {
<h1 class="my-6">{ history.Date.Format("Monday, 02 January 2006") }</h1> <h1 class="mb-6 mt-8 max-xs:text-xl">{ history.Date.Format("Monday, 02 January 2006") }</h1>
<div class="divider my-2"></div> <div class="divider my-2"></div>
<div class="grid sm:grid-cols-[1fr,9fr] gap-x-4 gap-y-2 sm:gap-y-4"> <div class="grid sm:grid-cols-[1fr,9fr] gap-x-4 gap-y-2 sm:gap-y-4">
<span class="font-bold max-sm:hidden text-center">Time</span> <span class="font-bold max-sm:hidden text-center">Time</span>
@ -127,12 +127,12 @@ templ actionButton(data Data) {
func actionButtonItems(data Data) []templ.Component { func actionButtonItems(data Data) []templ.Component {
out := make([]templ.Component, 0, 2) out := make([]templ.Component, 0, 2)
if !data.IsCurrent {
out = append(out, actionButtonPrev(data))
}
if len(data.ScheduleHistories.Schedules) >= int(data.Params.Limit) || data.Params.Reversed { if len(data.ScheduleHistories.Schedules) >= int(data.Params.Limit) || data.Params.Reversed {
out = append(out, actionButtonNext(data)) out = append(out, actionButtonNext(data))
} }
if !data.IsCurrent {
out = append(out, actionButtonPrev(data))
}
if data.IsCurrent { if data.IsCurrent {
out = append(out, actionButtonRefresh()) out = append(out, actionButtonRefresh())
} }