schedule-history: now date overflow handled to today date
Some checks failed
/ push (push) Has been cancelled

This commit is contained in:
Tigor Hutasuhut 2024-06-03 16:42:40 +07:00
parent 44128365c0
commit c114469721
2 changed files with 77 additions and 36 deletions

View file

@ -17,16 +17,24 @@ type ScheduleHistoryListByDateParams struct {
func (params *ScheduleHistoryListByDateParams) FillFromQuery(query Queryable) { func (params *ScheduleHistoryListByDateParams) FillFromQuery(query Queryable) {
var err error var err error
params.Date, err = time.Parse("2006-01-02", query.Get("date")) params.Date, err = time.Parse("2006-01-02", query.Get("date"))
now := time.Now()
if err != nil { if err != nil {
params.Date = time.Now() params.Date = now
}
queryDate := time.Date(params.Date.Year(), params.Date.Month(), params.Date.Day(), 0, 0, 0, 0, now.Location())
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
if queryDate.After(today) {
params.Date = today
} }
} }
func (params *ScheduleHistoryListByDateParams) CountQuery() (expr []bob.Mod[*dialect.SelectQuery]) { func (params *ScheduleHistoryListByDateParams) CountQuery() (expr []bob.Mod[*dialect.SelectQuery]) {
unixTopTime := time.Date(params.Date.Year(), params.Date.Month(), params.Date.Day(), 23, 59, 59, 0, params.Date.Location()).Unix() unixTopTime := time.Date(params.Date.Year(), params.Date.Month(), params.Date.Day(), 23, 59, 59, 0, params.Date.Location()).Unix()
unixLowTime := time.Date(params.Date.Year(), params.Date.Month(), params.Date.Day(), 0, 0, 0, 0, params.Date.Location()).Unix() unixLowTime := time.Date(params.Date.Year(), params.Date.Month(), params.Date.Day(), 0, 0, 0, 0, params.Date.Location()).Unix()
expr = append(expr, models.SelectWhere.ScheduleHistories.CreatedAt.GTE(unixLowTime)) expr = append(expr,
expr = append(expr, models.SelectWhere.ScheduleHistories.CreatedAt.LTE(unixTopTime)) models.SelectWhere.ScheduleHistories.CreatedAt.GTE(unixLowTime),
models.SelectWhere.ScheduleHistories.CreatedAt.LTE(unixTopTime),
)
return return
} }

View file

@ -42,39 +42,7 @@ templ Content(c *views.Context, data Data) {
<main class="prose min-w-full"> <main class="prose min-w-full">
<h1>Schedule History ({ time.Local.String() })</h1> <h1>Schedule History ({ time.Local.String() })</h1>
<div class="divider my-0"></div> <div class="divider my-0"></div>
<div @dateBar(data, true)
class="flex flex-wrap justify-between my-4 items-center"
hx-boost="true"
hx-select="#root-content"
hx-swap="outerHTML"
hx-target="#root-content"
>
if data.isCurrentDay() {
<a
href="/history"
class="btn btn-primary btn-outline btn-square text-base-100"
>
@icons.Refresh("w-6 h-6")
</a>
} else {
<a
href={ templ.SafeURL(fmt.Sprintf("/history?date=%s", data.Params.Date.Add(time.Hour*24).Format(time.DateOnly))) }
class="btn btn-primary btn-outline btn-square text-base-100"
>
@icons.ChevronBoldLeft("w-6 h-6")
</a>
}
<span class="max-xs:hidden text-primary font-bold sm:text-2xl">{ data.Params.Date.Format("Monday, 02 January 2006") }</span>
<span class="xs:hidden text-primary font-bold">{ data.Params.Date.Format("Mon, 02 Jan") }</span>
<div class="tooltip" data-tip="Next">
<a
href={ templ.SafeURL(fmt.Sprintf("/history?date=%s", data.Params.Date.Add(time.Hour*-24).Format(time.DateOnly))) }
class="btn btn-primary btn-outline btn-square text-base-100 no-underline"
>
@icons.ChevronBoldRight("w-6 h-6")
</a>
</div>
</div>
if len(data.ScheduleHistories) == 0 { if len(data.ScheduleHistories) == 0 {
<h2>There are no history schedules found for current date.</h2> <h2>There are no history schedules found for current date.</h2>
} }
@ -143,9 +111,74 @@ templ Content(c *views.Context, data Data) {
} }
</div> </div>
} }
if len(data.ScheduleHistories) > 20 {
@dateBar(data, false)
}
@actionButton(data)
</main> </main>
} }
templ actionButton(data Data) {
<div class="xs:hidden">
@components.ActionButton(
actionButtonNext(data),
actionButtonPrev(data),
)
</div>
}
templ dateBar(data Data, showDate bool) {
<div
class="flex flex-wrap justify-between my-4 items-center"
hx-boost="true"
hx-select="#root-content"
hx-swap="outerHTML"
hx-target="#root-content"
>
if data.isCurrentDay() {
<a
href="/history"
class="btn btn-primary btn-outline btn-square text-base-100"
>
@icons.Refresh("w-6 h-6")
</a>
} else {
<a
href={ templ.SafeURL(fmt.Sprintf("/history?date=%s", data.Params.Date.Add(time.Hour*24).Format(time.DateOnly))) }
class="btn btn-primary btn-outline btn-square text-base-100"
>
@icons.ChevronBoldLeft("w-6 h-6")
</a>
}
if showDate {
<span class="max-xs:hidden text-primary font-bold sm:text-2xl">{ data.Params.Date.Format("Monday, 02 January 2006") }</span>
<span class="xs:hidden text-primary font-bold">{ data.Params.Date.Format("Mon, 02 Jan") }</span>
}
<div class="tooltip" data-tip="Next">
<a
href={ templ.SafeURL(fmt.Sprintf("/history?date=%s", data.Params.Date.Add(time.Hour*-24).Format(time.DateOnly))) }
class="btn btn-primary btn-outline btn-square text-base-100 no-underline"
>
@icons.ChevronBoldRight("w-6 h-6")
</a>
</div>
</div>
}
templ actionButtonNext(data Data) {
<a
href={ templ.SafeURL(fmt.Sprintf("/history?date=%s", data.Params.Date.Add(time.Hour*-24).Format(time.DateOnly))) }
class="btn btn-ghost btn-sm no-underline m-0"
>Next</a>
}
templ actionButtonPrev(data Data) {
<a
href={ templ.SafeURL(fmt.Sprintf("/history?date=%s", data.Params.Date.Add(time.Hour*24).Format(time.DateOnly))) }
class="btn btn-ghost btn-sm no-underline m-0"
>Previous</a>
}
templ subredditLink(subreddit string) { templ subredditLink(subreddit string) {
<a href={ templ.URL(fmt.Sprintf("/subreddits/details/%s", subreddit)) } class="text-primary">{ subreddit }</a> <a href={ templ.URL(fmt.Sprintf("/subreddits/details/%s", subreddit)) } class="text-primary">{ subreddit }</a>
} }