Added infinitescroll in the user soundtracks tab

This commit is contained in:
Scott 2018-03-14 15:56:09 +01:00
parent df0b6abd68
commit 585a429e52
3 changed files with 61 additions and 17 deletions

View File

@ -170,7 +170,9 @@ func Configure(app *aero.Application) {
l.Page("/user/:nick/forum/threads", profile.GetThreadsByUser) l.Page("/user/:nick/forum/threads", profile.GetThreadsByUser)
l.Page("/user/:nick/forum/posts", profile.GetPostsByUser) l.Page("/user/:nick/forum/posts", profile.GetPostsByUser)
l.Page("/user/:nick/soundtracks/added", profile.GetSoundTracksByUser) l.Page("/user/:nick/soundtracks/added", profile.GetSoundTracksByUser)
l.Page("/user/:nick/soundtracks/added/from/:index", profile.GetSoundTracksByUser)
l.Page("/user/:nick/soundtracks/liked", profile.GetSoundTracksLikedByUser) l.Page("/user/:nick/soundtracks/liked", profile.GetSoundTracksLikedByUser)
l.Page("/user/:nick/soundtracks/liked/from/:index", profile.GetSoundTracksLikedByUser)
l.Page("/user/:nick/stats", profile.GetStatsByUser) l.Page("/user/:nick/stats", profile.GetStatsByUser)
l.Page("/user/:nick/followers", profile.GetFollowers) l.Page("/user/:nick/followers", profile.GetFollowers)
l.Page("/user/:nick/animelist", animelist.Get) l.Page("/user/:nick/animelist", animelist.Get)

View File

@ -7,6 +7,7 @@ import (
"github.com/animenotifier/arn" "github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components" "github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils" "github.com/animenotifier/notify.moe/utils"
"github.com/animenotifier/notify.moe/utils/infinitescroll"
) )
// GetSoundTracksByUser shows all soundtracks of a particular user. // GetSoundTracksByUser shows all soundtracks of a particular user.
@ -19,17 +20,35 @@ func GetSoundTracksByUser(ctx *aero.Context) string {
return ctx.Error(http.StatusNotFound, "User not found", err) return ctx.Error(http.StatusNotFound, "User not found", err)
} }
tracks := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool { index, _ := ctx.GetInt("index")
return !track.IsDraft && len(track.Media) > 0 && track.CreatedBy == viewUser.ID
})
arn.SortSoundTracksLatestFirst(tracks) // Fetch all eligible tracks
allTracks := fetchAllByUser(viewUser.ID)
return ctx.HTML(components.TrackList(tracks, viewUser, user, ctx.URI())) // Sort the tracks by number of likes
arn.SortSoundTracksLatestFirst(allTracks)
// Slice the part that we need
tracks := allTracks[index:]
if len(tracks) > maxTracks {
tracks = tracks[:maxTracks]
}
// Next index
nextIndex := infinitescroll.NextIndex(ctx, len(allTracks), maxTracks, index)
// In case we're scrolling, send soundtracks only (without the page frame)
if index > 0 {
return ctx.HTML(components.SoundTracksScrollable(tracks, user))
}
// Otherwise, send the full page
return ctx.HTML(components.TrackList(tracks,viewUser, nextIndex, user, ctx.URI()))
} }
// GetSoundTracksByUser shows all soundtracks of a particular user. // GetSoundTracksLikedByUser shows all soundtracks of a particular user.
func GetSoundTracksLikedByUser(ctx *aero.Context) string { func GetSoundTracksLikedByUser(ctx *aero.Context) string {
nick := ctx.Get("nick") nick := ctx.Get("nick")
user := utils.GetUser(ctx) user := utils.GetUser(ctx)
@ -39,12 +58,29 @@ func GetSoundTracksLikedByUser(ctx *aero.Context) string {
return ctx.Error(http.StatusNotFound, "User not found", err) return ctx.Error(http.StatusNotFound, "User not found", err)
} }
tracks := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool { index, _ := ctx.GetInt("index")
return !track.IsDraft && len(track.Media) > 0 && track.LikedBy(viewUser.ID)
})
arn.SortSoundTracksLatestFirst(tracks) // Fetch all eligible tracks
allTracks := fetchAllLikedByUser(viewUser.ID)
return ctx.HTML(components.TrackList(tracks, viewUser, user, ctx.URI())) // Sort the tracks by number of likes
arn.SortSoundTracksLatestFirst(allTracks)
// Slice the part that we need
tracks := allTracks[index:]
if len(tracks) > maxTracks {
tracks = tracks[:maxTracks]
}
// Next index
nextIndex := infinitescroll.NextIndex(ctx, len(allTracks), maxTracks, index)
// In case we're scrolling, send soundtracks only (without the page frame)
if index > 0 {
return ctx.HTML(components.SoundTracksScrollable(tracks, user))
}
// Otherwise, send the full page
return ctx.HTML(components.TrackList(tracks,viewUser, nextIndex, user, ctx.URI()))
} }

View File

@ -1,12 +1,18 @@
component TrackList(tracks []*arn.SoundTrack, viewUser *arn.User, user *arn.User, uri string) component TrackList(tracks []*arn.SoundTrack, viewUser *arn.User, nextIndex int, user *arn.User, uri string)
ProfileHeader(viewUser, user, uri) ProfileHeader(viewUser, user, uri)
if strings.Contains(uri, "/added")
h1.page-title= "Tracks added by " + viewUser.Nick h1.page-title= "Tracks added by " + viewUser.Nick
else
h1.page-title= "Tracks liked by " + viewUser.Nick
if len(tracks) == 0 if len(tracks) == 0
p.no-data.mountable= viewUser.Nick + " hasn't added any tracks yet." p.no-data.mountable= viewUser.Nick + " hasn't added any tracks yet."
else else
.soundtracks #load-more-target.soundtracks
each track in tracks SoundTracksScrollable(tracks, user)
SoundTrack(track)
if nextIndex != -1
.buttons
LoadMore(nextIndex)