This commit is contained in:
Untone 2024-07-07 17:07:11 +03:00
parent f4f4e80816
commit e7bcb4c6d4
3 changed files with 54 additions and 34 deletions

View File

@ -40,8 +40,10 @@ export const AllAuthors = (props: Props) => {
// filter // filter
const [searchQuery, setSearchQuery] = createSignal('') const [searchQuery, setSearchQuery] = createSignal('')
const [filteredAuthors, setFilteredAuthors] = createSignal<Author[]>([]) const [filteredAuthors, setFilteredAuthors] = createSignal<Author[]>([])
createEffect(() => createEffect(
authors() && setFilteredAuthors((_prev: Author[]) => dummyFilter(authors(), searchQuery(), lang()) as Author[]) () =>
authors() &&
setFilteredAuthors((_prev: Author[]) => dummyFilter(authors(), searchQuery(), lang()) as Author[])
) )
// sort by // sort by

View File

@ -1,4 +1,11 @@
import { RouteDefinition, RouteSectionProps, createAsync, redirect, useLocation, useParams } from '@solidjs/router' import {
RouteDefinition,
RouteSectionProps,
createAsync,
redirect,
useLocation,
useParams
} from '@solidjs/router'
import { HttpStatusCode } from '@solidjs/start' import { HttpStatusCode } from '@solidjs/start'
import { ErrorBoundary, Show, createEffect, createMemo, createSignal, on, onMount } from 'solid-js' import { ErrorBoundary, Show, createEffect, createMemo, createSignal, on, onMount } from 'solid-js'
import { Loading } from '~/components/_shared/Loading' import { Loading } from '~/components/_shared/Loading'
@ -12,7 +19,6 @@ import { FullArticle } from '../components/Article/FullArticle'
import { PageLayout } from '../components/_shared/PageLayout' import { PageLayout } from '../components/_shared/PageLayout'
import { ReactionsProvider } from '../context/reactions' import { ReactionsProvider } from '../context/reactions'
const fetchShout = async (slug: string): Promise<Shout> => { const fetchShout = async (slug: string): Promise<Shout> => {
const shoutLoader = getShout({ slug }) const shoutLoader = getShout({ slug })
const shout = await shoutLoader() const shout = await shoutLoader()
@ -22,7 +28,6 @@ const fetchShout = async (slug: string): Promise<Shout> => {
return shout return shout
} }
export const route: RouteDefinition = { export const route: RouteDefinition = {
load: async ({ params }) => { load: async ({ params }) => {
try { try {
@ -46,12 +51,14 @@ export default (props: RouteSectionProps<{ article: Shout }>) => {
const article = createAsync(async () => { const article = createAsync(async () => {
if (params.slug && articleEntities?.()) { if (params.slug && articleEntities?.()) {
return articleEntities()?.[params.slug] || props.data.article || await fetchShout(params.slug) return articleEntities()?.[params.slug] || props.data.article || (await fetchShout(params.slug))
} }
throw redirect('/404', { status: 404 }) throw redirect('/404', { status: 404 })
}) })
const title = createMemo(() => `${article()?.authors?.[0]?.name || t('Discours')} :: ${article()?.title || ''}`) const title = createMemo(
() => `${article()?.authors?.[0]?.name || t('Discours')} :: ${article()?.title || ''}`
)
onMount(async () => { onMount(async () => {
if (gaIdentity && article()?.id) { if (gaIdentity && article()?.id) {
@ -64,14 +71,20 @@ export default (props: RouteSectionProps<{ article: Shout }>) => {
} }
}) })
createEffect(on(article, (a?: Shout) => { createEffect(
on(
article,
(a?: Shout) => {
if (!a) return if (!a) return
window?.gtag?.('event', 'page_view', { window?.gtag?.('event', 'page_view', {
page_title: a.title, page_title: a.title,
page_location: window?.location.href || '', page_location: window?.location.href || '',
page_path: loc.pathname page_path: loc.pathname
}) })
}, { defer: true })) },
{ defer: true }
)
)
return ( return (
<ErrorBoundary fallback={() => <HttpStatusCode code={404} />}> <ErrorBoundary fallback={() => <HttpStatusCode code={404} />}>

View File

@ -26,14 +26,14 @@ export const route = {
const by = query.by const by = query.by
const isAll = !by || by === 'name' const isAll = !by || by === 'name'
return { return {
authors: isAll && await fetchAllAuthors(), authors: isAll && (await fetchAllAuthors()),
topFollowedAuthors: await fetchAuthorsWithStat(10, 'followers'), topFollowedAuthors: await fetchAuthorsWithStat(10, 'followers'),
topShoutsAuthors: await fetchAuthorsWithStat(10, 'shouts') topShoutsAuthors: await fetchAuthorsWithStat(10, 'shouts')
} as AllAuthorsData } as AllAuthorsData
} }
} satisfies RouteDefinition } satisfies RouteDefinition
type AllAuthorsData = { authors: Author[], topFollowedAuthors: Author[], topShoutsAuthors: Author[] } type AllAuthorsData = { authors: Author[]; topFollowedAuthors: Author[]; topShoutsAuthors: Author[] }
// addAuthors to context // addAuthors to context
@ -52,16 +52,20 @@ export default function AllAuthorsPage(props: RouteSectionProps<AllAuthorsData>)
}) })
// update context when data is loaded // update context when data is loaded
createEffect(on([data, () => addAuthors], createEffect(
([data, aa])=> { on(
if(data && aa) { [data, () => addAuthors],
([data, aa]) => {
if (data && aa) {
aa(data.authors as Author[]) aa(data.authors as Author[])
aa(data.topFollowedAuthors as Author[]) aa(data.topFollowedAuthors as Author[])
aa(data.topShoutsAuthors as Author[]) aa(data.topShoutsAuthors as Author[])
console.debug('[routes.author] added all authors:', data.authors) console.debug('[routes.author] added all authors:', data.authors)
} }
}, { defer: true} },
)) { defer: true }
)
)
return ( return (
<PageLayout withPadding={true} title={`${t('Discours')} :: ${t('All authors')}`}> <PageLayout withPadding={true} title={`${t('Discours')} :: ${t('All authors')}`}>
@ -71,7 +75,8 @@ export default function AllAuthorsPage(props: RouteSectionProps<AllAuthorsData>)
isLoaded={Boolean(data()?.authors)} isLoaded={Boolean(data()?.authors)}
authors={data()?.authors || []} authors={data()?.authors || []}
topFollowedAuthors={data()?.topFollowedAuthors} topFollowedAuthors={data()?.topFollowedAuthors}
topWritingAuthors={data()?.topShoutsAuthors}/> topWritingAuthors={data()?.topShoutsAuthors}
/>
</Suspense> </Suspense>
</ReactionsProvider> </ReactionsProvider>
</PageLayout> </PageLayout>