mirror of https://github.com/MaxLeiter/Drift
client: remove need for multiple post page URLs
parent
4bcf791c86
commit
67e1b9889b
@ -0,0 +1,64 @@
|
||||
import PasswordModal from "@components/new-post/password-modal"
|
||||
import { Page, useToasts } from "@geist-ui/core"
|
||||
import { Post } from "@lib/types"
|
||||
import { useRouter } from "next/router"
|
||||
import { useState } from "react"
|
||||
|
||||
type Props = {
|
||||
setPost: (post: Post) => void
|
||||
}
|
||||
|
||||
const PasswordModalPage = ({ setPost }: Props) => {
|
||||
const router = useRouter()
|
||||
const { setToast } = useToasts()
|
||||
const [isPasswordModalOpen, setIsPasswordModalOpen] = useState(true)
|
||||
|
||||
const onSubmit = async (password: string) => {
|
||||
const res = await fetch(
|
||||
`/server-api/posts/authenticate?id=${router.query.id}&password=${password}`,
|
||||
{
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
if (!res.ok) {
|
||||
setToast({
|
||||
type: "error",
|
||||
text: "Wrong password"
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
const data = await res.json()
|
||||
if (data) {
|
||||
if (data.error) {
|
||||
setToast({
|
||||
text: data.error,
|
||||
type: "error"
|
||||
})
|
||||
} else {
|
||||
setIsPasswordModalOpen(false)
|
||||
setPost(data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const onClose = () => {
|
||||
setIsPasswordModalOpen(false)
|
||||
router.push("/")
|
||||
}
|
||||
|
||||
return (
|
||||
<PasswordModal
|
||||
creating={false}
|
||||
onClose={onClose}
|
||||
onSubmit={onSubmit}
|
||||
isOpen={isPasswordModalOpen}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export default PasswordModalPage
|
||||
@ -1,60 +0,0 @@
|
||||
import cookie from "cookie"
|
||||
import type { GetServerSideProps } from "next"
|
||||
import { Post } from "@lib/types"
|
||||
import PostPage from "@components/post-page"
|
||||
|
||||
export type PostProps = {
|
||||
post: Post
|
||||
}
|
||||
|
||||
const Post = ({ post }: PostProps) => {
|
||||
return <PostPage post={post} />
|
||||
}
|
||||
|
||||
export const getServerSideProps: GetServerSideProps = async (context) => {
|
||||
const headers = context.req.headers
|
||||
const host = headers.host
|
||||
const driftToken = cookie.parse(headers.cookie || "")[`drift-token`]
|
||||
|
||||
if (context.query.id) {
|
||||
const post = await fetch(
|
||||
"http://" + host + `/server-api/posts/${context.query.id}`,
|
||||
{
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${driftToken}`,
|
||||
"x-secret-key": process.env.SECRET_KEY || ""
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
if (!post.ok || post.status !== 200) {
|
||||
return {
|
||||
redirect: {
|
||||
destination: "/",
|
||||
permanent: false
|
||||
}
|
||||
}
|
||||
}
|
||||
try {
|
||||
const json = await post.json()
|
||||
|
||||
return {
|
||||
props: {
|
||||
post: json
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
props: {
|
||||
post: null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default Post
|
||||
Loading…
Reference in New Issue