home-fetch-all-topics
This commit is contained in:
parent
35f39da99e
commit
0323c913b3
|
@ -33,7 +33,7 @@ export const AllTopics = (props: Props) => {
|
||||||
const topics = createMemo(() => sortedTopics() || props.topics)
|
const topics = createMemo(() => sortedTopics() || props.topics)
|
||||||
const [searchParams, changeSearchParams] = useSearchParams<{ by?: string }>()
|
const [searchParams, changeSearchParams] = useSearchParams<{ by?: string }>()
|
||||||
createEffect(on(() => searchParams?.by || 'shouts', setTopicsSort, { defer: true }))
|
createEffect(on(() => searchParams?.by || 'shouts', setTopicsSort, { defer: true }))
|
||||||
onMount(() => setTimeout(() => !searchParams?.by && changeSearchParams({ by: 'shouts' }), 1))
|
onMount(() => !searchParams?.by && changeSearchParams({ by: 'shouts' }))
|
||||||
|
|
||||||
// sorted derivative
|
// sorted derivative
|
||||||
const byLetter = createMemo<{ [letter: string]: Topic[] }>(() => {
|
const byLetter = createMemo<{ [letter: string]: Topic[] }>(() => {
|
||||||
|
|
|
@ -36,6 +36,7 @@ export interface HomeViewProps {
|
||||||
topMonthShouts: Shout[]
|
topMonthShouts: Shout[]
|
||||||
topViewedShouts: Shout[]
|
topViewedShouts: Shout[]
|
||||||
topCommentedShouts: Shout[]
|
topCommentedShouts: Shout[]
|
||||||
|
topics?: Topic[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export const HomeView = (props: HomeViewProps) => {
|
export const HomeView = (props: HomeViewProps) => {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { type RouteDefinition, type RouteSectionProps, createAsync } from '@solidjs/router'
|
import { type RouteDefinition, type RouteSectionProps, createAsync } from '@solidjs/router'
|
||||||
import { Show, Suspense, createSignal, onMount } from 'solid-js'
|
import { Show, Suspense, createEffect, createSignal, onMount } from 'solid-js'
|
||||||
import { loadShouts } from '~/graphql/api/public'
|
import { useTopics } from '~/context/topics'
|
||||||
|
import { loadShouts, loadTopics } from '~/graphql/api/public'
|
||||||
import { LoadShoutsOptions } from '~/graphql/schema/core.gen'
|
import { LoadShoutsOptions } from '~/graphql/schema/core.gen'
|
||||||
import { byStat } from '~/lib/sortby'
|
import { byStat } from '~/lib/sortby'
|
||||||
import { restoreScrollPosition, saveScrollPosition } from '~/utils/scroll'
|
import { restoreScrollPosition, saveScrollPosition } from '~/utils/scroll'
|
||||||
|
@ -12,6 +13,11 @@ import { ReactionsProvider } from '../context/reactions'
|
||||||
|
|
||||||
export const SHOUTS_PER_PAGE = 20
|
export const SHOUTS_PER_PAGE = 20
|
||||||
|
|
||||||
|
const fetchAllTopics = async () => {
|
||||||
|
const allTopicsLoader = loadTopics()
|
||||||
|
return await allTopicsLoader()
|
||||||
|
}
|
||||||
|
|
||||||
const fetchHomeTopData = async () => {
|
const fetchHomeTopData = async () => {
|
||||||
const topCommentedLoader = loadShouts({
|
const topCommentedLoader = loadShouts({
|
||||||
filters: { featured: true },
|
filters: { featured: true },
|
||||||
|
@ -50,13 +56,13 @@ export const route = {
|
||||||
filters: { featured: true },
|
filters: { featured: true },
|
||||||
limit
|
limit
|
||||||
})
|
})
|
||||||
return { ...(await fetchHomeTopData()), featuredShouts: await featuredLoader() }
|
return { ...(await fetchHomeTopData()), featuredShouts: await featuredLoader(), topics: await fetchAllTopics() }
|
||||||
}
|
}
|
||||||
} satisfies RouteDefinition
|
} satisfies RouteDefinition
|
||||||
|
|
||||||
export default function HomePage(props: RouteSectionProps<HomeViewProps>) {
|
export default function HomePage(props: RouteSectionProps<HomeViewProps>) {
|
||||||
const limit = 20
|
const limit = 20
|
||||||
|
const { addTopics } = useTopics()
|
||||||
const { t } = useLocalize()
|
const { t } = useLocalize()
|
||||||
const [featuredOffset, setFeaturedOffset] = createSignal<number>(0)
|
const [featuredOffset, setFeaturedOffset] = createSignal<number>(0)
|
||||||
|
|
||||||
|
@ -71,6 +77,7 @@ export default function HomePage(props: RouteSectionProps<HomeViewProps>) {
|
||||||
|
|
||||||
// async ssr-friendly router-level cached data source
|
// async ssr-friendly router-level cached data source
|
||||||
const data = createAsync(async (prev?: HomeViewProps) => {
|
const data = createAsync(async (prev?: HomeViewProps) => {
|
||||||
|
const topics = props.data?.topics || await fetchAllTopics()
|
||||||
const featuredShoutsLoader = featuredLoader(featuredOffset())
|
const featuredShoutsLoader = featuredLoader(featuredOffset())
|
||||||
const featuredShouts = [
|
const featuredShouts = [
|
||||||
...(prev?.featuredShouts || []),
|
...(prev?.featuredShouts || []),
|
||||||
|
@ -82,10 +89,12 @@ export default function HomePage(props: RouteSectionProps<HomeViewProps>) {
|
||||||
...prev,
|
...prev,
|
||||||
...props.data,
|
...props.data,
|
||||||
topViewedShouts,
|
topViewedShouts,
|
||||||
featuredShouts
|
featuredShouts,
|
||||||
|
topics
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
})
|
})
|
||||||
|
createEffect(() => data()?.topics && addTopics(data()?.topics || []))
|
||||||
|
|
||||||
const [canLoadMoreFeatured, setCanLoadMoreFeatured] = createSignal(true)
|
const [canLoadMoreFeatured, setCanLoadMoreFeatured] = createSignal(true)
|
||||||
const loadMoreFeatured = async () => {
|
const loadMoreFeatured = async () => {
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
import { RouteDefinition, RouteLoadFuncArgs, type RouteSectionProps, createAsync } from '@solidjs/router'
|
import { RouteDefinition, RouteLoadFuncArgs, type RouteSectionProps, createAsync } from '@solidjs/router'
|
||||||
import { Suspense } from 'solid-js'
|
import { Suspense, createEffect } from 'solid-js'
|
||||||
import { AllAuthors } from '~/components/Views/AllAuthors'
|
import { AllAuthors } from '~/components/Views/AllAuthors'
|
||||||
import { Loading } from '~/components/_shared/Loading'
|
import { Loading } from '~/components/_shared/Loading'
|
||||||
import { PageLayout } from '~/components/_shared/PageLayout'
|
import { PageLayout } from '~/components/_shared/PageLayout'
|
||||||
|
import { useAuthors } from '~/context/authors'
|
||||||
import { useLocalize } from '~/context/localize'
|
import { useLocalize } from '~/context/localize'
|
||||||
import { ReactionsProvider } from '~/context/reactions'
|
import { ReactionsProvider } from '~/context/reactions'
|
||||||
import { loadAuthors } from '~/graphql/api/public'
|
import { loadAuthors } from '~/graphql/api/public'
|
||||||
|
@ -47,7 +48,9 @@ export const route = {
|
||||||
|
|
||||||
export default function AllAuthorsPage(props: RouteSectionProps<{ authors: Author[] }>) {
|
export default function AllAuthorsPage(props: RouteSectionProps<{ authors: Author[] }>) {
|
||||||
const { t } = useLocalize()
|
const { t } = useLocalize()
|
||||||
|
const { addAuthors } = useAuthors()
|
||||||
const authors = createAsync<Author[]>(async () => props.data.authors || (await fetchData()))
|
const authors = createAsync<Author[]>(async () => props.data.authors || (await fetchData()))
|
||||||
|
createEffect(() => addAuthors(authors() || []))
|
||||||
return (
|
return (
|
||||||
<PageLayout withPadding={true} title={`${t('Discours')} :: ${t('All authors')}`}>
|
<PageLayout withPadding={true} title={`${t('Discours')} :: ${t('All authors')}`}>
|
||||||
<ReactionsProvider>
|
<ReactionsProvider>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user