forked from podverse/podverse-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
70 lines (62 loc) · 2.38 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { GetServerSideProps } from 'next'
import { Podcast } from 'podverse-shared'
import { Page } from '~/lib/utility/page'
import Podcasts from './podcasts'
import { PV } from '~/resources'
import { getCategoryBySlug } from '~/services/category'
import { getPodcastsByQuery } from '~/services/podcast'
import { getDefaultServerSideProps } from '~/services/serverSideHelpers'
export default Podcasts
interface ServerProps extends Page {
serverCategoryId: string | null
serverFilterFrom: string
serverFilterPage: number
serverFilterSort: string
serverPodcastsListData: Podcast[]
serverPodcastsListDataCount: number
serverIsHomePage: boolean
}
/* Server-Side Logic */
/* NOTE: This logic is identical to the getServerSideProps in /pages/podcasts.tsx */
export const getServerSideProps: GetServerSideProps = async (ctx) => {
const { locale, query } = ctx
const { category: categorySlug } = query
const selectedCategory = getCategoryBySlug(categorySlug as string)
const serverCategoryId = selectedCategory?.id || null
const defaultServerProps = await getDefaultServerSideProps(ctx, locale)
const { serverGlobalFilters, serverUserInfo } = defaultServerProps
const serverFilterFrom = serverUserInfo && !selectedCategory ? PV.Filters.from._subscribed : PV.Filters.from._category
const serverFilterSort =
serverUserInfo && !selectedCategory ? PV.Filters.sort._mostRecent : PV.Filters.sort._topPastWeek
const serverFilterPage = 1
let podcastsListData = []
let podcastsListDataCount = 0
if (selectedCategory) {
const response = await getPodcastsByQuery({
categories: [serverCategoryId],
sort: serverFilterSort,
hasVideo: serverGlobalFilters.videoOnlyMode
})
podcastsListData = response.data[0]
podcastsListDataCount = response.data[1]
} else if (serverUserInfo) {
const response = await getPodcastsByQuery({
podcastIds: serverUserInfo?.subscribedPodcastIds,
sort: serverFilterSort,
hasVideo: serverGlobalFilters.videoOnlyMode
})
podcastsListData = response.data[0]
podcastsListDataCount = response.data[1]
}
const serverProps: ServerProps = {
...defaultServerProps,
serverCategoryId,
serverFilterFrom,
serverFilterPage,
serverFilterSort,
serverIsHomePage: true,
serverPodcastsListData: podcastsListData,
serverPodcastsListDataCount: podcastsListDataCount
}
return { props: serverProps }
}