2022-04-12 19:48:12 -04:00
|
|
|
import PasswordModal from "@components/new-post/password-modal"
|
2022-11-12 02:59:33 -05:00
|
|
|
import { useToasts } from "@geist-ui/core/dist"
|
|
|
|
import { Post } from "@lib/server/prisma"
|
|
|
|
import { useRouter } from "next/navigation"
|
2022-04-12 19:48:12 -04:00
|
|
|
import { useState } from "react"
|
|
|
|
|
|
|
|
type Props = {
|
2022-04-13 00:14:10 -04:00
|
|
|
setPost: (post: Post) => void
|
2022-11-12 02:59:33 -05:00
|
|
|
postId: Post["id"]
|
2022-04-12 19:48:12 -04:00
|
|
|
}
|
|
|
|
|
2022-11-12 02:59:33 -05:00
|
|
|
const PasswordModalPage = ({ setPost, postId }: Props) => {
|
2022-04-13 00:14:10 -04:00
|
|
|
const router = useRouter()
|
|
|
|
const { setToast } = useToasts()
|
|
|
|
const [isPasswordModalOpen, setIsPasswordModalOpen] = useState(true)
|
2022-04-12 19:48:12 -04:00
|
|
|
|
2022-04-13 00:14:10 -04:00
|
|
|
const onSubmit = async (password: string) => {
|
|
|
|
const res = await fetch(
|
2022-11-12 02:59:33 -05:00
|
|
|
`/api/posts/authenticate?id=${postId}&password=${password}`,
|
2022-04-13 00:14:10 -04:00
|
|
|
{
|
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2022-04-12 19:48:12 -04:00
|
|
|
|
2022-04-13 00:14:10 -04:00
|
|
|
if (!res.ok) {
|
|
|
|
setToast({
|
|
|
|
type: "error",
|
|
|
|
text: "Wrong password"
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-04-12 19:48:12 -04:00
|
|
|
|
2022-04-13 00:14:10 -04:00
|
|
|
const data = await res.json()
|
|
|
|
if (data) {
|
|
|
|
if (data.error) {
|
|
|
|
setToast({
|
|
|
|
text: data.error,
|
|
|
|
type: "error"
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
setIsPasswordModalOpen(false)
|
|
|
|
setPost(data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-12 19:48:12 -04:00
|
|
|
|
2022-04-13 00:14:10 -04:00
|
|
|
const onClose = () => {
|
|
|
|
setIsPasswordModalOpen(false)
|
|
|
|
router.push("/")
|
|
|
|
}
|
2022-04-12 19:48:12 -04:00
|
|
|
|
2022-04-13 00:14:10 -04:00
|
|
|
return (
|
|
|
|
<PasswordModal
|
|
|
|
creating={false}
|
|
|
|
onClose={onClose}
|
|
|
|
onSubmit={onSubmit}
|
|
|
|
isOpen={isPasswordModalOpen}
|
|
|
|
/>
|
|
|
|
)
|
2022-04-12 19:48:12 -04:00
|
|
|
}
|
|
|
|
|
2022-04-13 00:14:10 -04:00
|
|
|
export default PasswordModalPage
|