From 73e1f575f8e7d8a9099bd68f1d006e0a57109576 Mon Sep 17 00:00:00 2001 From: Ilya Y <75578537+ilya-bkv@users.noreply.github.com> Date: Sun, 25 Feb 2024 10:31:11 +0300 Subject: [PATCH] Hotfix/all authors bugfix (#418) bufgix to authors --- src/components/AuthorsList/AuthorsList.tsx | 72 ++++++++++++------- .../Views/AllAuthors/AllAuthors.tsx | 36 +++++----- src/stores/zine/authors.ts | 15 +--- 3 files changed, 66 insertions(+), 57 deletions(-) diff --git a/src/components/AuthorsList/AuthorsList.tsx b/src/components/AuthorsList/AuthorsList.tsx index 236a586e..2f3bb060 100644 --- a/src/components/AuthorsList/AuthorsList.tsx +++ b/src/components/AuthorsList/AuthorsList.tsx @@ -1,5 +1,5 @@ import { clsx } from 'clsx' -import { For, Show, createEffect, createSignal } from 'solid-js' +import { For, Show, createEffect, createSignal, on, onMount } from 'solid-js' import { useFollowing } from '../../context/following' import { useLocalize } from '../../context/localize' import { apiClient } from '../../graphql/client/core' @@ -11,24 +11,29 @@ import styles from './AuthorsList.module.scss' type Props = { class?: string - query: 'shouts' | 'followers' + query: 'shouts' | 'authors' + searchQuery?: string + allAuthorsLength?: number } const PAGE_SIZE = 20 export const AuthorsList = (props: Props) => { const { t } = useLocalize() const { isOwnerSubscribed } = useFollowing() + const { authorsByShouts, authorsByFollowers } = useAuthorsStore() const [loading, setLoading] = createSignal(false) const [currentPage, setCurrentPage] = createSignal({ shouts: 0, followers: 0 }) - const { authorsByShouts, authorsByFollowers } = useAuthorsStore() + const [allLoaded, setAllLoaded] = createSignal(false) - const fetchAuthors = async (queryType: 'shouts' | 'followers', page: number) => { + const fetchAuthors = async (queryType: 'shouts' | 'authors', page: number) => { setLoading(true) + + console.log('!!! AAA:') const offset = PAGE_SIZE * page const result = await apiClient.loadAuthorsBy({ by: { order: queryType }, limit: PAGE_SIZE, - offset: offset, + offset, }) if (queryType === 'shouts') { @@ -41,25 +46,38 @@ export const AuthorsList = (props: Props) => { } const loadMoreAuthors = () => { - const queryType = props.query - const nextPage = currentPage()[queryType] + 1 - fetchAuthors(queryType, nextPage).then(() => - setCurrentPage({ ...currentPage(), [queryType]: nextPage }), + const nextPage = currentPage()[props.query] + 1 + fetchAuthors(props.query, nextPage).then(() => + setCurrentPage({ ...currentPage(), [props.query]: nextPage }), ) } - createEffect(() => { - const queryType = props.query - if ( - currentPage()[queryType] === 0 && - (authorsByShouts().length === 0 || authorsByFollowers().length === 0) - ) { - loadMoreAuthors() - } - }) + createEffect( + on( + () => props.query, + (query) => { + const authorsList = query === 'shouts' ? authorsByShouts() : authorsByFollowers() + if (authorsList.length === 0 || currentPage()[query] === 0) { + setCurrentPage((prev) => ({ ...prev, [query]: 0 })) + fetchAuthors(query, 0).then(() => setCurrentPage((prev) => ({ ...prev, [query]: 1 }))) + } + }, + ), + ) const authorsList = () => (props.query === 'shouts' ? authorsByShouts() : authorsByFollowers()) + // TODO: do it with backend + // createEffect(() => { + // if (props.searchQuery) { + // // search logic + // } + // }) + + createEffect(() => { + setAllLoaded(authorsByShouts().length === authorsList.length) + }) + return (