all-authors-load-fix
All checks were successful
deploy / test (push) Successful in 1m11s

This commit is contained in:
Untone 2023-12-27 23:34:41 +03:00
parent 3d18410763
commit 31a8b68b16

View File

@ -6,7 +6,7 @@ import { createEffect, createMemo, createSignal, For, Show } from 'solid-js'
import { useLocalize } from '../../context/localize' import { useLocalize } from '../../context/localize'
import { useRouter } from '../../stores/router' import { useRouter } from '../../stores/router'
import { loadAllAuthors, loadAuthors, setAuthorsSort, useAuthorsStore } from '../../stores/zine/authors' import { loadAuthors, setAuthorsSort, useAuthorsStore } from '../../stores/zine/authors'
import { dummyFilter } from '../../utils/dummyFilter' import { dummyFilter } from '../../utils/dummyFilter'
import { getImageUrl } from '../../utils/getImageUrl' import { getImageUrl } from '../../utils/getImageUrl'
import { scrollHandler } from '../../utils/scroll' import { scrollHandler } from '../../utils/scroll'
@ -30,7 +30,8 @@ const ALPHABET = [...'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫ
export const AllAuthorsView = (props: Props) => { export const AllAuthorsView = (props: Props) => {
const { t, lang } = useLocalize() const { t, lang } = useLocalize()
const [offset, setOffset] = createSignal(0) const [offsetByShouts, setOffsetByShouts] = createSignal(0)
const [offsetByFollowers, setOffsetByFollowers] = createSignal(0)
const { searchParams, changeSearchParams } = useRouter<AllAuthorsPageSearchParams>() const { searchParams, changeSearchParams } = useRouter<AllAuthorsPageSearchParams>()
const { sortedAuthors } = useAuthorsStore({ const { sortedAuthors } = useAuthorsStore({
authors: props.authors, authors: props.authors,
@ -38,7 +39,7 @@ export const AllAuthorsView = (props: Props) => {
}) })
const [searchQuery, setSearchQuery] = createSignal('') const [searchQuery, setSearchQuery] = createSignal('')
const offset = searchParams()?.by === 'shouts' ? offsetByShouts : offsetByFollowers
createEffect(() => { createEffect(() => {
let by = searchParams().by let by = searchParams().by
if (by) { if (by) {
@ -49,23 +50,29 @@ export const AllAuthorsView = (props: Props) => {
} }
}) })
const loadMore = async (by: AllAuthorsPageSearchParams['by'] = '') => { const loadMoreByShouts = async () => {
await loadAuthors({ by: { stat: by }, limit: PAGE_SIZE, offset: offset() }) await loadAuthors({ by: { stat: 'shouts' }, limit: PAGE_SIZE, offset: offsetByShouts() })
setOffset((o) => o + PAGE_SIZE) setOffsetByShouts((o) => o + PAGE_SIZE)
}
const loadMoreByFollowers = async () => {
await loadAuthors({ by: { stat: 'followers' }, limit: PAGE_SIZE, offset: offsetByFollowers() })
setOffsetByFollowers((o) => o + PAGE_SIZE)
} }
const isStatsLoaded = createMemo(() => sortedAuthors() && sortedAuthors().some((author) => author.stat)) const isStatsLoaded = createMemo(() => sortedAuthors() && sortedAuthors().some((author) => author.stat))
createEffect(async () => { createEffect(async () => {
if (!isStatsLoaded()) { if (!isStatsLoaded()) {
await loadMore('shouts') await loadMoreByShouts()
await loadMore('followers') await loadMoreByFollowers()
} }
}) })
const showMore = async () => { const showMore = async () =>
await loadMore(searchParams().by) await {
} shouts: loadMoreByShouts,
followers: loadMoreByFollowers,
}[searchParams().by]()
const byLetter = createMemo<{ [letter: string]: Author[] }>(() => { const byLetter = createMemo<{ [letter: string]: Author[] }>(() => {
return sortedAuthors().reduce( return sortedAuthors().reduce(