notea/pages/[id].tsx

67 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-02-20 15:11:53 +03:00
import LayoutMain from 'components/layout/layout-main'
2021-05-08 09:13:55 +03:00
import { useSession } from 'libs/server/middlewares/session'
import { applySettings } from 'libs/server/middlewares/settings'
import { applyAuth, applyRedirectLogin } from 'libs/server/middlewares/auth'
import { applyNote } from 'libs/server/middlewares/note'
import LayoutPublic from 'components/layout/layout-public'
2021-04-20 16:01:15 +03:00
import { EditContainer } from 'components/container/edit-container'
import { PostContainer } from 'components/container/post-container'
2021-05-08 09:13:55 +03:00
import { applyCsrf } from 'libs/server/middlewares/csrf'
import { ssr, SSRContext, ServerProps } from 'libs/server/connect'
import { applyUA } from 'libs/server/middlewares/ua'
2021-05-09 03:56:51 +03:00
import { applyPostWithAuth } from 'libs/server/middlewares/post'
2021-05-09 04:18:50 +03:00
import { isNoteLink } from 'libs/shared/note'
2021-05-20 17:20:22 +03:00
import { applyReset } from 'libs/server/middlewares/reset'
2021-02-17 09:07:35 +03:00
2021-05-08 09:13:55 +03:00
export default function EditNotePage({
tree,
note,
pageMode,
baseURL,
isLoggedIn,
2021-05-09 03:56:51 +03:00
post,
2021-05-08 09:13:55 +03:00
}: ServerProps) {
if (isLoggedIn) {
2021-04-20 16:01:15 +03:00
return (
2021-05-08 09:13:55 +03:00
<LayoutMain tree={tree} note={note}>
<EditContainer />
</LayoutMain>
2021-04-20 16:01:15 +03:00
)
}
2021-05-08 09:13:55 +03:00
2021-02-17 09:07:35 +03:00
return (
2021-05-08 09:13:55 +03:00
<LayoutPublic tree={tree} note={note}>
2021-05-09 03:56:51 +03:00
<PostContainer post={post} pageMode={pageMode} baseURL={baseURL} />
2021-05-08 09:13:55 +03:00
</LayoutPublic>
2021-02-17 09:07:35 +03:00
)
}
2021-05-08 09:13:55 +03:00
export const getServerSideProps = async (
ctx: SSRContext & {
query: {
id: string
}
}
) => {
2021-05-09 04:18:50 +03:00
if (!isNoteLink('/' + ctx.query.id)) {
2021-05-08 09:13:55 +03:00
return { props: {} }
}
2021-05-09 01:47:43 +03:00
2021-05-08 09:13:55 +03:00
await ssr()
.use(useSession)
.use(applyAuth)
.use(applyNote(ctx.query.id))
.use(applyRedirectLogin(ctx.resolvedUrl))
2021-05-20 17:20:22 +03:00
.use(applyReset)
2021-05-08 09:13:55 +03:00
.use(applySettings)
.use(applyCsrf)
.use(applyUA)
2021-05-09 03:56:51 +03:00
.use(applyPostWithAuth)
2021-05-08 09:13:55 +03:00
.run(ctx.req, ctx.res)
2021-02-20 15:11:53 +03:00
2021-05-08 09:13:55 +03:00
return {
props: ctx.req.props,
redirect: ctx.req.redirect,
}
}