2024-07-13 09:36:23 +00:00
|
|
|
import { Params, RouteSectionProps, createAsync } from '@solidjs/router'
|
2024-09-25 22:48:30 +00:00
|
|
|
import { Show, createEffect, createSignal, on } from 'solid-js'
|
2024-07-21 02:17:42 +00:00
|
|
|
import { TopicsNav } from '~/components/TopicsNav'
|
2024-07-04 07:51:15 +00:00
|
|
|
import { Expo } from '~/components/Views/Expo'
|
2024-09-25 22:48:30 +00:00
|
|
|
import ExpoNav from '~/components/Views/Expo/ExpoNav'
|
|
|
|
import { LoadMoreItems, LoadMoreWrapper } from '~/components/_shared/LoadMoreWrapper'
|
2024-07-04 07:51:15 +00:00
|
|
|
import { PageLayout } from '~/components/_shared/PageLayout'
|
2024-09-25 22:48:30 +00:00
|
|
|
import { EXPO_LAYOUTS, EXPO_TITLES, SHOUTS_PER_PAGE, useFeed } from '~/context/feed'
|
2024-07-04 07:51:15 +00:00
|
|
|
import { useLocalize } from '~/context/localize'
|
|
|
|
import { loadShouts } from '~/graphql/api/public'
|
2024-09-25 22:48:30 +00:00
|
|
|
import { LoadShoutsFilters, LoadShoutsOptions, Shout } from '~/graphql/schema/core.gen'
|
|
|
|
import { ExpoLayoutType } from '~/types/common'
|
|
|
|
import { restoreScrollPosition, saveScrollPosition } from '~/utils/scroll'
|
|
|
|
import { byCreated } from '~/utils/sort'
|
2024-07-03 21:25:03 +00:00
|
|
|
|
|
|
|
const fetchExpoShouts = async (layouts: string[]) => {
|
|
|
|
const result = await loadShouts({
|
|
|
|
filters: { layouts },
|
|
|
|
limit: SHOUTS_PER_PAGE,
|
|
|
|
offset: 0
|
|
|
|
} as LoadShoutsOptions)
|
|
|
|
return result || []
|
|
|
|
}
|
|
|
|
|
|
|
|
export const route = {
|
|
|
|
load: async ({ params }: { params: Params }) => {
|
2024-09-16 12:57:00 +00:00
|
|
|
const layouts = params.layout ? [params.layout] : EXPO_LAYOUTS
|
2024-07-03 21:25:03 +00:00
|
|
|
const shoutsLoader = await fetchExpoShouts(layouts)
|
|
|
|
return (await shoutsLoader()) as Shout[]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-05 17:23:07 +00:00
|
|
|
export default (props: RouteSectionProps<Shout[]>) => {
|
2024-07-03 21:25:03 +00:00
|
|
|
const { t } = useLocalize()
|
2024-09-25 22:48:30 +00:00
|
|
|
const { expoFeed, setExpoFeed, feedByLayout } = useFeed()
|
|
|
|
const [loadMoreVisible, setLoadMoreVisible] = createSignal(false)
|
|
|
|
const getTitle = (l?: string) => EXPO_TITLES[(l as ExpoLayoutType) || '']
|
|
|
|
|
2024-07-03 21:25:03 +00:00
|
|
|
const shouts = createAsync(
|
|
|
|
async () =>
|
2024-09-16 12:57:00 +00:00
|
|
|
props.data || (await fetchExpoShouts(props.params.layout ? [props.params.layout] : EXPO_LAYOUTS))
|
2024-07-03 21:25:03 +00:00
|
|
|
)
|
2024-09-24 11:56:16 +00:00
|
|
|
|
2024-09-25 22:48:30 +00:00
|
|
|
// Функция для загрузки дополнительных шотов
|
|
|
|
const loadMore = async () => {
|
|
|
|
saveScrollPosition()
|
|
|
|
const limit = SHOUTS_PER_PAGE
|
|
|
|
const layouts = props.params.layout ? [props.params.layout] : EXPO_LAYOUTS
|
|
|
|
const offset = expoFeed()?.length || 0
|
|
|
|
const filters: LoadShoutsFilters = { layouts, featured: true }
|
|
|
|
const options: LoadShoutsOptions = { filters, limit, offset }
|
|
|
|
const shoutsFetcher = loadShouts(options)
|
|
|
|
const result = await shoutsFetcher()
|
|
|
|
setLoadMoreVisible(Boolean(result?.length))
|
|
|
|
if (result) {
|
|
|
|
setExpoFeed((prev) => Array.from(new Set([...(prev || []), ...result])).sort(byCreated))
|
2024-07-03 21:25:03 +00:00
|
|
|
}
|
2024-09-25 22:48:30 +00:00
|
|
|
restoreScrollPosition()
|
|
|
|
return result as LoadMoreItems
|
2024-09-24 11:56:16 +00:00
|
|
|
}
|
2024-09-25 22:48:30 +00:00
|
|
|
// Эффект для загрузки данных при изменении layout
|
|
|
|
createEffect(
|
|
|
|
on(
|
|
|
|
() => props.params.layout as ExpoLayoutType,
|
|
|
|
async (layout?: ExpoLayoutType) => {
|
|
|
|
const layouts = layout ? [layout] : EXPO_LAYOUTS
|
|
|
|
const offset = (layout ? feedByLayout()[layout]?.length : expoFeed()?.length) || 0
|
|
|
|
const options: LoadShoutsOptions = {
|
|
|
|
filters: { layouts, featured: true },
|
|
|
|
limit: SHOUTS_PER_PAGE,
|
|
|
|
offset
|
|
|
|
}
|
|
|
|
const shoutsFetcher = loadShouts(options)
|
|
|
|
const result = await shoutsFetcher()
|
|
|
|
setExpoFeed(result || [])
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
2024-07-03 21:25:03 +00:00
|
|
|
return (
|
2024-09-24 11:56:16 +00:00
|
|
|
<PageLayout
|
|
|
|
withPadding={true}
|
|
|
|
zeroBottomPadding={true}
|
|
|
|
title={`${t('Discours')} :: ${getTitle(props.params.layout || '')}`}
|
|
|
|
>
|
2024-07-09 09:13:13 +00:00
|
|
|
<TopicsNav />
|
2024-09-25 22:48:30 +00:00
|
|
|
<ExpoNav layout={(props.params.layout || '') as ExpoLayoutType | ''} />
|
|
|
|
<LoadMoreWrapper loadFunction={loadMore} pageSize={SHOUTS_PER_PAGE} hidden={!loadMoreVisible()}>
|
|
|
|
<Show when={shouts()} keyed>
|
|
|
|
{(sss: Shout[]) => <Expo shouts={sss} layout={props.params.layout as ExpoLayoutType} />}
|
|
|
|
</Show>
|
|
|
|
</LoadMoreWrapper>
|
2024-07-03 21:25:03 +00:00
|
|
|
</PageLayout>
|
|
|
|
)
|
|
|
|
}
|