Added characters page
This commit is contained in:
22
pages/characters/best.go
Normal file
22
pages/characters/best.go
Normal file
@ -0,0 +1,22 @@
|
||||
package characters
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/aerogo/aero"
|
||||
)
|
||||
|
||||
// Best characters.
|
||||
func Best(ctx *aero.Context) string {
|
||||
characters := fetchAll()
|
||||
|
||||
sort.Slice(characters, func(i, j int) bool {
|
||||
if len(characters[i].Likes) == len(characters[j].Likes) {
|
||||
return characters[i].Name.Canonical < characters[j].Name.Canonical
|
||||
}
|
||||
|
||||
return len(characters[i].Likes) > len(characters[j].Likes)
|
||||
})
|
||||
|
||||
return render(ctx, characters)
|
||||
}
|
@ -14,19 +14,19 @@ component Characters(characters []*arn.Character, nextIndex int, tag string, use
|
||||
Icon("pencil")
|
||||
span Edit draft
|
||||
|
||||
//- #load-more-target.characters.characters-page
|
||||
//- CharactersScrollable(characters, user)
|
||||
#load-more-target.characters.characters-page
|
||||
CharactersScrollable(characters, user)
|
||||
|
||||
//- if nextIndex != -1
|
||||
//- .buttons
|
||||
//- LoadMore(nextIndex)
|
||||
if nextIndex != -1
|
||||
.buttons
|
||||
LoadMore(nextIndex)
|
||||
|
||||
component CharactersScrollable(characters []*arn.Character, user *arn.User)
|
||||
each character in characters
|
||||
Character(character, user)
|
||||
|
||||
component CharactersTabs(tag string)
|
||||
//- .tab-groups
|
||||
//- .tabs
|
||||
//- Tab("Latest", "video-camera", "/characters")
|
||||
//- Tab("Best", "heart", "/characters/best")
|
||||
.tab-groups
|
||||
.tabs
|
||||
Tab("Latest", "child", "/characters")
|
||||
Tab("Best", "heart", "/characters/best")
|
9
pages/characters/fetch.go
Normal file
9
pages/characters/fetch.go
Normal file
@ -0,0 +1,9 @@
|
||||
package characters
|
||||
|
||||
import "github.com/animenotifier/arn"
|
||||
|
||||
func fetchAll() []*arn.Character {
|
||||
return arn.FilterCharacters(func(character *arn.Character) bool {
|
||||
return !character.IsDraft
|
||||
})
|
||||
}
|
18
pages/characters/latest.go
Normal file
18
pages/characters/latest.go
Normal file
@ -0,0 +1,18 @@
|
||||
package characters
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/aerogo/aero"
|
||||
)
|
||||
|
||||
// Latest characters.
|
||||
func Latest(ctx *aero.Context) string {
|
||||
characters := fetchAll()
|
||||
|
||||
sort.Slice(characters, func(i, j int) bool {
|
||||
return characters[i].Created > characters[j].Created
|
||||
})
|
||||
|
||||
return render(ctx, characters)
|
||||
}
|
44
pages/characters/render.go
Normal file
44
pages/characters/render.go
Normal file
@ -0,0 +1,44 @@
|
||||
package characters
|
||||
|
||||
import (
|
||||
"github.com/aerogo/aero"
|
||||
"github.com/animenotifier/arn"
|
||||
"github.com/animenotifier/notify.moe/components"
|
||||
"github.com/animenotifier/notify.moe/utils"
|
||||
"github.com/animenotifier/notify.moe/utils/infinitescroll"
|
||||
)
|
||||
|
||||
const (
|
||||
charactersFirstLoad = 104
|
||||
charactersPerScroll = 39
|
||||
)
|
||||
|
||||
// render renders the characters page with the given characters.
|
||||
func render(ctx *aero.Context, allCharacters []*arn.Character) string {
|
||||
user := utils.GetUser(ctx)
|
||||
index, _ := ctx.GetInt("index")
|
||||
tag := ctx.Get("tag")
|
||||
|
||||
// Slice the part that we need
|
||||
characters := allCharacters[index:]
|
||||
maxLength := charactersFirstLoad
|
||||
|
||||
if index > 0 {
|
||||
maxLength = charactersPerScroll
|
||||
}
|
||||
|
||||
if len(characters) > maxLength {
|
||||
characters = characters[:maxLength]
|
||||
}
|
||||
|
||||
// Next index
|
||||
nextIndex := infinitescroll.NextIndex(ctx, len(allCharacters), maxLength, index)
|
||||
|
||||
// In case we're scrolling, send Characters only (without the page frame)
|
||||
if index > 0 {
|
||||
return ctx.HTML(components.CharactersScrollable(characters, user))
|
||||
}
|
||||
|
||||
// Otherwise, send the full page
|
||||
return ctx.HTML(components.Characters(characters, nextIndex, tag, user))
|
||||
}
|
Reference in New Issue
Block a user