Added infinite scrolling
This commit is contained in:
@ -10,7 +10,7 @@ import (
|
||||
"github.com/animenotifier/notify.moe/utils"
|
||||
)
|
||||
|
||||
const maxTracks = 9
|
||||
const maxTracks = 12
|
||||
|
||||
// Get renders the soundtracks page.
|
||||
func Get(ctx *aero.Context) string {
|
||||
@ -30,7 +30,7 @@ func Get(ctx *aero.Context) string {
|
||||
tracks = tracks[:maxTracks]
|
||||
}
|
||||
|
||||
return ctx.HTML(components.SoundTracks(tracks, user))
|
||||
return ctx.HTML(components.SoundTracks(tracks, maxTracks, user))
|
||||
}
|
||||
|
||||
// From renders the soundtracks from the given index.
|
||||
@ -42,7 +42,7 @@ func From(ctx *aero.Context) string {
|
||||
return ctx.Error(http.StatusBadRequest, "Invalid start index", err)
|
||||
}
|
||||
|
||||
tracks, err := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool {
|
||||
allTracks, err := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool {
|
||||
return !track.IsDraft && len(track.Media) > 0
|
||||
})
|
||||
|
||||
@ -50,17 +50,27 @@ func From(ctx *aero.Context) string {
|
||||
return ctx.Error(http.StatusInternalServerError, "Error fetching soundtracks", err)
|
||||
}
|
||||
|
||||
if index < 0 || index >= len(tracks) {
|
||||
return ctx.Error(http.StatusBadRequest, "Invalid start index (maximum is "+strconv.Itoa(len(tracks))+")", nil)
|
||||
if index < 0 || index >= len(allTracks) {
|
||||
return ctx.Error(http.StatusBadRequest, "Invalid start index (maximum is "+strconv.Itoa(len(allTracks))+")", nil)
|
||||
}
|
||||
|
||||
arn.SortSoundTracksLatestFirst(tracks)
|
||||
arn.SortSoundTracksLatestFirst(allTracks)
|
||||
|
||||
tracks = tracks[index:]
|
||||
tracks := allTracks[index:]
|
||||
|
||||
if len(tracks) > maxTracks {
|
||||
tracks = tracks[:maxTracks]
|
||||
}
|
||||
|
||||
nextIndex := index + maxTracks
|
||||
|
||||
if nextIndex >= len(allTracks) {
|
||||
// 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.SoundTracksScrollable(tracks, user))
|
||||
}
|
||||
|
Reference in New Issue
Block a user