2022-12-04 04:31:51 -05:00
|
|
|
'use client';
|
|
|
|
|
2022-11-14 20:24:35 -05:00
|
|
|
import { Post, PostWithFilesAndAuthor } from "@lib/server/prisma"
|
2022-11-12 04:28:06 -05:00
|
|
|
import PasswordModal from "@components/password-modal"
|
2022-11-12 02:59:33 -05:00
|
|
|
import { useRouter } from "next/navigation"
|
2022-04-12 19:48:12 -04:00
|
|
|
import { useState } from "react"
|
2022-11-28 21:33:06 -05:00
|
|
|
import { useToasts } from "@components/toasts"
|
2022-04-12 19:48:12 -04:00
|
|
|
|
|
|
|
type Props = {
|
2022-11-14 20:24:35 -05:00
|
|
|
setPost: (post: PostWithFilesAndAuthor) => void
|
|
|
|
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) => {
|
2022-11-12 19:06:23 -05:00
|
|
|
const res = await fetch(`/api/post/${postId}?password=${password}`, {
|
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json"
|
2022-04-13 00:14:10 -04:00
|
|
|
}
|
2022-11-12 19:06:23 -05:00
|
|
|
})
|
2022-04-12 19:48:12 -04:00
|
|
|
|
2022-04-13 00:14:10 -04:00
|
|
|
if (!res.ok) {
|
|
|
|
setToast({
|
|
|
|
type: "error",
|
2022-11-28 21:33:06 -05:00
|
|
|
message: "Wrong password"
|
2022-04-13 00:14:10 -04:00
|
|
|
})
|
|
|
|
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({
|
2022-11-28 21:33:06 -05:00
|
|
|
message: data.error,
|
2022-04-13 00:14:10 -04:00
|
|
|
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
|