✨ Added the list of quotes sorted by last and best with a correct design.
The design has been verified to be responsive and compatible with the dark theme
This commit is contained in:
mixins
pages
scripts/Actions
styles/include
66
pages/quotes/best.go
Normal file
66
pages/quotes/best.go
Normal file
@ -0,0 +1,66 @@
|
||||
package quotes
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/aerogo/aero"
|
||||
"github.com/animenotifier/arn"
|
||||
"github.com/animenotifier/notify.moe/components"
|
||||
"github.com/animenotifier/notify.moe/utils"
|
||||
)
|
||||
|
||||
// Best renders the quotes page ordered by the most favorites first.
|
||||
func Best(ctx *aero.Context) string {
|
||||
user := utils.GetUser(ctx)
|
||||
|
||||
quotes := arn.FilterQuotes(func(track *arn.Quote) bool {
|
||||
return !track.IsDraft && len(track.Description) > 0
|
||||
})
|
||||
|
||||
arn.SortQuotesPopularFirst(quotes)
|
||||
|
||||
if len(quotes) > maxQuotes {
|
||||
quotes = quotes[:maxQuotes]
|
||||
}
|
||||
|
||||
return ctx.HTML(components.Quotes(quotes, maxQuotes, user))
|
||||
}
|
||||
|
||||
// BestFrom renders the quotes from the given index.
|
||||
func BestFrom(ctx *aero.Context) string {
|
||||
user := utils.GetUser(ctx)
|
||||
index, err := ctx.GetInt("index")
|
||||
|
||||
if err != nil {
|
||||
return ctx.Error(http.StatusBadRequest, "Invalid start index", err)
|
||||
}
|
||||
|
||||
allQuotes := arn.FilterQuotes(func(track *arn.Quote) bool {
|
||||
return !track.IsDraft && len(track.Description) > 0
|
||||
})
|
||||
|
||||
if index < 0 || index >= len(allQuotes) {
|
||||
return ctx.Error(http.StatusBadRequest, "Invalid start index (maximum is "+strconv.Itoa(len(allQuotes))+")", nil)
|
||||
}
|
||||
|
||||
arn.SortQuotesPopularFirst(allQuotes)
|
||||
|
||||
quotes := allQuotes[index:]
|
||||
|
||||
if len(quotes) > maxQuotes {
|
||||
quotes = quotes[:maxQuotes]
|
||||
}
|
||||
|
||||
nextIndex := index + maxQuotes
|
||||
|
||||
if nextIndex >= len(allQuotes) {
|
||||
// End of data - no more scrolling
|
||||
ctx.Response().Header().Set("X-LoadMore-Index", "-1")
|
||||
} else {
|
||||
// Send the index for the next request
|
||||
ctx.Response().Header().Set("X-LoadMore-Index", strconv.Itoa(nextIndex))
|
||||
}
|
||||
|
||||
return ctx.HTML(components.QuotesScrollable(quotes, user))
|
||||
}
|
Reference in New Issue
Block a user