Skip to content

Commit

Permalink
fix: redirect to course page (#1507)
Browse files Browse the repository at this point in the history
* chore: sync PostSchema with builder

* fix: redirect to course page when postType is course

* refactor: move redirect to client as getStaticProps doesn't support redirects
  • Loading branch information
zacjones93 authored Feb 13, 2025
1 parent cd8c913 commit 931e351
Showing 1 changed file with 49 additions and 12 deletions.
61 changes: 49 additions & 12 deletions src/pages/[post].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,45 @@ import {
} from '@/hooks/mux/use-video-player-overlay'
import VideoPlayerOverlay from '@/components/posts/video-player-overlay'
import {MuxPlayerProvider, useMuxPlayer} from '@/hooks/use-mux-player'
import router from 'next/router'

export const PostTypeSchema = z.union([
z.literal('article'),
z.literal('lesson'),
z.literal('podcast'),
z.literal('tip'),
z.literal('course'),
])

export const PostStateSchema = z.union([
z.literal('draft'),
z.literal('published'),
z.literal('archived'),
z.literal('deleted'),
])

export const PostVisibilitySchema = z.union([
z.literal('public'),
z.literal('private'),
z.literal('unlisted'),
])

export const PostAccessSchema = z.union([z.literal('free'), z.literal('pro')])

export const FieldsSchema = z.object({
body: z.string().optional(),
slug: z.string().optional(),
state: z.string().optional(),
title: z.string().optional(),
access: z.string().optional(),
github: z.string().optional(),
gitpod: z.string().optional(),
postType: z.string().optional(),
visibility: z.string().optional(),
description: z.string().optional(),
eggheadLessonId: z.number().optional(),
title: z.string(),
postType: PostTypeSchema.default('lesson'),
summary: z.string().optional().nullable(),
body: z.string().nullable().optional(),
state: PostStateSchema.default('draft'),
visibility: PostVisibilitySchema.default('public'),
access: PostAccessSchema.default('pro'),
eggheadLessonId: z.number().nullish(),
eggheadPlaylistId: z.number().nullish(),
slug: z.string(),
description: z.string().nullish(),
github: z.string().nullish(),
gitpod: z.string().nullish(),
primaryTagId: z.string().nullish(),
})
export type Fields = z.infer<typeof FieldsSchema>
Expand Down Expand Up @@ -286,6 +312,7 @@ export const getStaticProps: GetServerSideProps = async function ({params}) {
}

const {post, videoResource, tags} = result

const lesson = await fetch(
`${process.env.NEXT_PUBLIC_AUTH_DOMAIN}/api/v1/lessons/${post.fields.eggheadLessonId}`,
).then((res) => res.json())
Expand Down Expand Up @@ -366,11 +393,21 @@ export default function PostPage({
const imageParams = new URLSearchParams()
imageParams.set('title', post.fields?.title ?? '')

React.useEffect(() => {
if (post.fields.postType === 'course') {
router.replace(`/courses/${post.fields.slug}`)
}
}, [post.fields.postType, post.fields.slug, router])

if (post.fields.postType === 'course') {
return null
}

return (
<div>
<NextSeo
title={post.fields.title}
description={post.fields.description}
description={post.fields.description ?? ''}
canonical={`${process.env.NEXT_PUBLIC_DEPLOYMENT_URL}/${post.fields.slug}`}
twitter={{
cardType: 'summary_large_image',
Expand Down

0 comments on commit 931e351

Please sign in to comment.