From 22f44ee0eccad84a7b427bbc4a17650cdb18eabb Mon Sep 17 00:00:00 2001 From: Untone Date: Wed, 28 Aug 2024 12:50:04 +0300 Subject: [PATCH] tab-navigation-fix --- src/components/Views/Author/Author.tsx | 75 +++++++++++++------- src/routes/author/[slug]/[...tab].tsx | 97 ++++++++++++++++---------- 2 files changed, 110 insertions(+), 62 deletions(-) diff --git a/src/components/Views/Author/Author.tsx b/src/components/Views/Author/Author.tsx index 9fd9ecdb..3819ae21 100644 --- a/src/components/Views/Author/Author.tsx +++ b/src/components/Views/Author/Author.tsx @@ -1,6 +1,6 @@ -import { A, useLocation } from '@solidjs/router' +import { A, useLocation, useParams } from '@solidjs/router' import { clsx } from 'clsx' -import { For, Match, Show, Switch, createEffect, createMemo, createSignal } from 'solid-js' +import { For, Match, Show, Switch, createEffect, createMemo, createSignal, on } from 'solid-js' import { Loading } from '~/components/_shared/Loading' import { coreApiUrl } from '~/config' import { useAuthors } from '~/context/authors' @@ -37,6 +37,8 @@ export const AuthorView = (props: AuthorViewProps) => { // contexts const { t } = useLocalize() const loc = useLocation() + const params = useParams() + const [currentTab, setCurrentTab] = createSignal(props.selectedTab) const { session } = useSession() const client = createMemo(() => graphqlClientCreate(coreApiUrl, session()?.access_token)) @@ -58,6 +60,18 @@ export const AuthorView = (props: AuthorViewProps) => { paginate((props.shouts || []).slice(1), PRERENDERED_ARTICLES_COUNT, LOAD_MORE_PAGE_SIZE) ) + // Переход по табам + createEffect( + on( + () => params.tab, + (tab: string) => { + // Обновляем текущую вкладку + setCurrentTab(tab || '') + }, + {} + ) + ) + // Объединенный эффект для загрузки автора и его подписок createEffect(async () => { const meData = session()?.user?.app_data?.profile as Author @@ -105,6 +119,30 @@ export const AuthorView = (props: AuthorViewProps) => { setCommented((prev) => prev.filter((comment) => comment.id !== id)) } + const TabNavigator = () => ( +
+ +
+ ) + return (
@@ -118,27 +156,7 @@ export const AuthorView = (props: AuthorViewProps) => { />
-
- -
+
@@ -153,7 +171,8 @@ export const AuthorView = (props: AuthorViewProps) => {
- + +
@@ -177,7 +196,9 @@ export const AuthorView = (props: AuthorViewProps) => {
- + + +
@@ -203,7 +224,8 @@ export const AuthorView = (props: AuthorViewProps) => {
- + +
@@ -239,6 +261,7 @@ export const AuthorView = (props: AuthorViewProps) => { +
) diff --git a/src/routes/author/[slug]/[...tab].tsx b/src/routes/author/[slug]/[...tab].tsx index fc71c33e..a05d3539 100644 --- a/src/routes/author/[slug]/[...tab].tsx +++ b/src/routes/author/[slug]/[...tab].tsx @@ -1,5 +1,5 @@ -import { RouteSectionProps } from '@solidjs/router' -import { ErrorBoundary, Suspense, createEffect, createMemo, createSignal, on } from 'solid-js' +import { RouteSectionProps, createAsync } from '@solidjs/router' +import { ErrorBoundary, Suspense, createEffect, createSignal, on } from 'solid-js' import { AuthorView } from '~/components/Views/Author' import { FourOuFourView } from '~/components/Views/FourOuFour' import { LoadMoreItems, LoadMoreWrapper } from '~/components/_shared/LoadMoreWrapper' @@ -39,9 +39,10 @@ const fetchAuthor = async (slug: string) => { export const route = { load: async ({ params, location: { query } }: RouteSectionProps<{ articles: Shout[] }>) => { const offset: number = Number.parseInt(query.offset, 10) + console.debug('route loading with offset', offset) return { author: await fetchAuthor(params.slug), - shouts: await fetchAuthorShouts(params.slug, offset), + articles: await fetchAuthorShouts(params.slug, offset), topics: await fetchAllTopics() } } @@ -50,39 +51,17 @@ export const route = { export type AuthorPageProps = { articles?: Shout[]; author?: Author; topics?: Topic[] } export default function AuthorPage(props: RouteSectionProps) { + const { t } = useLocalize() + + // load author's profile const { addAuthor, authorsEntities } = useAuthors() const [author, setAuthor] = createSignal(undefined) - - const { t } = useLocalize() - const title = createMemo(() => `${author()?.name || ''}`) - - createEffect(() => { - if (author()) { - console.debug('[routes] author/[slug] author loaded fx') - window?.gtag?.('event', 'page_view', { - page_title: author()?.name || '', - page_location: window?.location.href || '', - page_path: window?.location.pathname || '' - }) - } - }) - - const cover = createMemo(() => - author()?.pic - ? getImageUrl(author()?.pic || '', { width: 1200 }) - : getImageUrl('production/image/logo_image.png') - ) - - // author shouts - const { addFeed, feedByAuthor } = useFeed() - const shoutsByAuthor = createMemo(() => feedByAuthor()[props.params.slug]) - createEffect( - on( - [() => props.params.slug || '', author], - async ([slug, profile]) => { + on(author, + async (profile) => { + // update only if no profile loaded if (!profile) { - const loadedAuthor = authorsEntities()[slug] || (await fetchAuthor(slug)) + const loadedAuthor = authorsEntities()[props.params.slug] || (await fetchAuthor(props.params.slug)) if (loadedAuthor) { addAuthor(loadedAuthor) setAuthor(loadedAuthor) @@ -93,6 +72,48 @@ export default function AuthorPage(props: RouteSectionProps) { ) ) + // author's data, view counter + const [title, setTitle] = createSignal('') + const [desc, setDesc] = createSignal('') + const [cover, setCover] = createSignal('') + const [viewed, setViewed] = createSignal(false) + createEffect( + on( + [author, () => window], + ([a, win]) => { + if (a && win) { + console.debug('[routes] author/[slug] author loaded fx') + if (!a) return + setTitle(() => `${t('Discours')}${a.name ? ` :: ${a.name}` : ''}`) + setDesc(() => a.about || a.bio || '') + setCover(() => (a.pic ? getImageUrl(a.pic || '', { width: 1200 }) : 'log.png')) + + // views google counter increment + if (!viewed()) { + window?.gtag?.('event', 'page_view', { + page_title: author()?.name || '', + page_location: window?.location.href || '', + page_path: window?.location.pathname || '' + }) + setViewed(true) + } + } + }, + {} + ) + ) + + // author's shouts + const { addFeed, feedByAuthor } = useFeed() + const [loadMoreHidden, setLoadMoreHidden] = createSignal(true) + const authorShouts = createAsync(async () => { + const sss: Shout[] = props.data.articles as Shout[] || feedByAuthor()[props.params.slug] || [] + const result = sss || (await fetchAuthorShouts(props.params.slug, 0)) + if (!result) setLoadMoreHidden(true) + return result + }) + + // load more shouts const loadAuthorShoutsMore = async (offset: number) => { const loadedShouts = await fetchAuthorShouts(props.params.slug, offset) loadedShouts && addFeed(loadedShouts) @@ -103,19 +124,23 @@ export default function AuthorPage(props: RouteSectionProps) { }> }> - +