Added infinite scrolling to companies

This commit is contained in:
Eduard Urbach 2018-03-13 23:06:16 +01:00
parent 3e2594ab9f
commit 40e2c43a8d
12 changed files with 64 additions and 63 deletions

View File

@ -11,7 +11,7 @@ import (
"github.com/animenotifier/notify.moe/utils" "github.com/animenotifier/notify.moe/utils"
) )
// All renders the companies page. // All renders an index of all companies.
func All(ctx *aero.Context) string { func All(ctx *aero.Context) string {
user := utils.GetUser(ctx) user := utils.GetUser(ctx)
@ -44,5 +44,5 @@ func All(ctx *aero.Context) string {
groups[currentGroupIndex] = append(groups[currentGroupIndex], company) groups[currentGroupIndex] = append(groups[currentGroupIndex], company)
} }
return ctx.HTML(components.Companies(groups, user)) return ctx.HTML(components.CompaniesIndex(groups, user))
} }

View File

@ -1,11 +1,11 @@
component Companies(groups [][]*arn.Company, user *arn.User) component CompaniesIndex(groups [][]*arn.Company, user *arn.User)
CompaniesTabs(user) CompaniesTabs(user)
h1.page-title All companies h1.page-title All companies
.company-groups .company-index-groups
each group in groups each group in groups
.company-group.mountable .company-index-group.mountable
h3= strings.ToUpper(group[0].Name.English[:1]) h3= strings.ToUpper(group[0].Name.English[:1])
ul ul
@ -15,8 +15,8 @@ component Companies(groups [][]*arn.Company, user *arn.User)
component CompaniesTabs(user *arn.User) component CompaniesTabs(user *arn.User)
.tabs .tabs
Tab("All", "font", "/companies") Tab("Popular", "globe", "/companies")
Tab("Popular", "globe", "/companies/popular") Tab("All", "font", "/companies/all")
.corner-buttons .corner-buttons
if user != nil && (user.Role == "editor" || user.Role == "admin") if user != nil && (user.Role == "editor" || user.Role == "admin")

View File

@ -0,0 +1,5 @@
.company-index-groups
horizontal-wrap
.company-index-group
padding 1rem

View File

@ -1,5 +0,0 @@
.company-groups
horizontal-wrap
.company-group
padding 1rem

10
pages/companies/fetch.go Normal file
View File

@ -0,0 +1,10 @@
package companies
import "github.com/animenotifier/arn"
// fetchAll returns all companies
func fetchAll() []*arn.Company {
return arn.FilterCompanies(func(company *arn.Company) bool {
return !company.IsDraft
})
}

View File

@ -1,57 +1,41 @@
package companies package companies
import ( import (
"sort"
"github.com/aerogo/aero" "github.com/aerogo/aero"
"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"
) )
const maxPopularCompanies = 100 const maxPopularCompanies = 50
// Popular renders the companies sorted by popularity. // Popular renders the best companies.
func Popular(ctx *aero.Context) string { func Popular(ctx *aero.Context) string {
user := utils.GetUser(ctx) user := utils.GetUser(ctx)
companies := []*arn.Company{} index, _ := ctx.GetInt("index")
// ID to popularity // Fetch all eligible companies
popularity := map[string]int{} allCompanies := fetchAll()
for anime := range arn.StreamAnime() { // Sort the companies by popularity
for _, studio := range anime.Studios() { arn.SortCompaniesPopularFirst(allCompanies)
popularity[studio.ID] += anime.Popularity.Watching + anime.Popularity.Completed
}
}
for companyID := range popularity { // Slice the part that we need
company, err := arn.GetCompany(companyID) companies := allCompanies[index:]
if err != nil {
continue
}
companies = append(companies, company)
}
sort.Slice(companies, func(i, j int) bool {
a := companies[i]
b := companies[j]
aPopularity := popularity[a.ID]
bPopularity := popularity[b.ID]
if aPopularity == bPopularity {
return a.Name.English < b.Name.English
}
return aPopularity > bPopularity
})
if len(companies) > maxPopularCompanies { if len(companies) > maxPopularCompanies {
companies = companies[:maxPopularCompanies] companies = companies[:maxPopularCompanies]
} }
return ctx.HTML(components.PopularCompanies(companies, popularity, user)) // Next index
nextIndex := infinitescroll.NextIndex(ctx, len(allCompanies), maxPopularCompanies, index)
// In case we're scrolling, send companies only (without the page frame)
if index > 0 {
return ctx.HTML(components.PopularCompaniesScrollable(companies, user))
}
// Otherwise, send the full page
return ctx.HTML(components.PopularCompanies(companies, nextIndex, user))
} }

View File

@ -1,11 +1,17 @@
component PopularCompanies(companies []*arn.Company, popularity map[string]int, user *arn.User) component PopularCompanies(companies []*arn.Company, nextIndex int, user *arn.User)
CompaniesTabs(user) CompaniesTabs(user)
h1.page-title Popular companies h1.page-title Popular companies
.companies ol#load-more-target.companies
ol PopularCompaniesScrollable(companies, user)
if nextIndex != -1
.buttons
LoadMore(nextIndex)
component PopularCompaniesScrollable(companies []*arn.Company, user *arn.User)
each company in companies each company in companies
li.mountable li.company.mountable
a.ajax(href=company.Link())= company.Name.English a.ajax(href=company.Link())= company.Name.English
span= " (" + strconv.Itoa(popularity[company.ID]) + ")" //- span= " (" + strconv.Itoa(popularity[company.ID]) + ")"

View File

@ -11,7 +11,7 @@ import (
const maxDescriptionLength = 170 const maxDescriptionLength = 170
// Get company. // Get renders a company page.
func Get(ctx *aero.Context) string { func Get(ctx *aero.Context) string {
user := utils.GetUser(ctx) user := utils.GetUser(ctx)
id := ctx.Get("id") id := ctx.Get("id")

View File

@ -131,8 +131,9 @@ func Configure(app *aero.Application) {
l.Page("/company/:id", company.Get) l.Page("/company/:id", company.Get)
l.Page("/company/:id/edit", company.Edit) l.Page("/company/:id/edit", company.Edit)
l.Page("/company/:id/history", company.History) l.Page("/company/:id/history", company.History)
l.Page("/companies", companies.All) l.Page("/companies", companies.Popular)
l.Page("/companies/popular", companies.Popular) l.Page("/companies/from/:index", companies.Popular)
l.Page("/companies/all", companies.All)
// Settings // Settings
l.Page("/settings", settings.Get(components.SettingsPersonal)) l.Page("/settings", settings.Get(components.SettingsPersonal))

View File

@ -16,7 +16,7 @@ func Best(ctx *aero.Context) string {
// Fetch all eligible quotes // Fetch all eligible quotes
allQuotes := fetchAll() allQuotes := fetchAll()
// Sort the quotes by date // Sort the quotes by number of likes
arn.SortQuotesPopularFirst(allQuotes) arn.SortQuotesPopularFirst(allQuotes)
// Slice the part that we need // Slice the part that we need

View File

@ -1,4 +1,4 @@
component Quotes(quotes []*arn.Quote, loadMoreIndex int, user *arn.User) component Quotes(quotes []*arn.Quote, nextIndex int, user *arn.User)
h1.page-title Quotes h1.page-title Quotes
QuotesTabs QuotesTabs
@ -17,9 +17,9 @@ component Quotes(quotes []*arn.Quote, loadMoreIndex int, user *arn.User)
#load-more-target.quotes #load-more-target.quotes
QuotesScrollable(quotes, user) QuotesScrollable(quotes, user)
if loadMoreIndex != -1 if nextIndex != -1
.buttons .buttons
LoadMore(loadMoreIndex) LoadMore(nextIndex)
component QuotesScrollable(quotes []*arn.Quote, user *arn.User) component QuotesScrollable(quotes []*arn.Quote, user *arn.User)
each quote in quotes each quote in quotes

View File

@ -16,7 +16,7 @@ func Best(ctx *aero.Context) string {
// Fetch all eligible tracks // Fetch all eligible tracks
allTracks := fetchAll() allTracks := fetchAll()
// Sort the tracks by date // Sort the tracks by number of likes
arn.SortSoundTracksPopularFirst(allTracks) arn.SortSoundTracksPopularFirst(allTracks)
// Slice the part that we need // Slice the part that we need