2022-11-09 21:38:05 -05:00
|
|
|
import { Button, ButtonGroup, Loading, useToasts } from "@geist-ui/core/dist"
|
2022-11-09 22:46:12 -05:00
|
|
|
import { TOKEN_COOKIE_NAME } from "@lib/constants"
|
2022-04-14 17:27:38 -04:00
|
|
|
import type { PostVisibility } from "@lib/types"
|
2022-11-12 04:28:06 -05:00
|
|
|
import PasswordModal from "@components/password-modal"
|
2022-11-09 22:46:12 -05:00
|
|
|
import { getCookie } from "cookies-next"
|
2022-04-14 17:27:38 -04:00
|
|
|
import { useCallback, useState } from "react"
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
postId: string
|
2022-11-12 02:59:33 -05:00
|
|
|
visibility: string
|
|
|
|
setVisibility: (visibility: string) => void
|
2022-04-14 17:27:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const VisibilityControl = ({ postId, visibility, setVisibility }: Props) => {
|
|
|
|
const [isSubmitting, setSubmitting] = useState(false)
|
|
|
|
const [passwordModalVisible, setPasswordModalVisible] = useState(false)
|
|
|
|
const { setToast } = useToasts()
|
|
|
|
|
|
|
|
const sendRequest = useCallback(
|
2022-11-12 02:59:33 -05:00
|
|
|
async (visibility: string, password?: string) => {
|
2022-11-12 19:06:23 -05:00
|
|
|
const res = await fetch(`/api/post/${postId}`, {
|
2022-04-14 17:27:38 -04:00
|
|
|
method: "PUT",
|
|
|
|
headers: {
|
2022-11-12 02:59:33 -05:00
|
|
|
"Content-Type": "application/json"
|
2022-04-14 17:27:38 -04:00
|
|
|
},
|
|
|
|
body: JSON.stringify({ visibility, password })
|
|
|
|
})
|
|
|
|
|
|
|
|
if (res.ok) {
|
|
|
|
const json = await res.json()
|
|
|
|
setVisibility(json.visibility)
|
|
|
|
} else {
|
|
|
|
setToast({
|
2022-11-12 02:59:33 -05:00
|
|
|
text: "An error occurred",
|
2022-04-14 17:27:38 -04:00
|
|
|
type: "error"
|
|
|
|
})
|
|
|
|
setPasswordModalVisible(false)
|
|
|
|
}
|
|
|
|
},
|
2022-04-14 17:32:20 -04:00
|
|
|
[postId, setToast, setVisibility]
|
2022-04-14 17:27:38 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const onSubmit = useCallback(
|
|
|
|
async (visibility: PostVisibility, password?: string) => {
|
|
|
|
if (visibility === "protected" && !password) {
|
|
|
|
setPasswordModalVisible(true)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
setPasswordModalVisible(false)
|
|
|
|
const timeout = setTimeout(() => setSubmitting(true), 100)
|
|
|
|
|
|
|
|
await sendRequest(visibility, password)
|
|
|
|
clearTimeout(timeout)
|
|
|
|
setSubmitting(false)
|
|
|
|
},
|
|
|
|
[sendRequest]
|
|
|
|
)
|
|
|
|
|
|
|
|
const onClosePasswordModal = () => {
|
|
|
|
setPasswordModalVisible(false)
|
|
|
|
setSubmitting(false)
|
|
|
|
}
|
|
|
|
|
2022-11-12 02:59:33 -05:00
|
|
|
const submitPassword = (password: string) => onSubmit("protected", password)
|
2022-04-14 17:27:38 -04:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{isSubmitting ? (
|
|
|
|
<Loading />
|
|
|
|
) : (
|
|
|
|
<ButtonGroup margin={0}>
|
|
|
|
<Button
|
|
|
|
disabled={visibility === "private"}
|
|
|
|
onClick={() => onSubmit("private")}
|
|
|
|
>
|
|
|
|
Make private
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
disabled={visibility === "public"}
|
|
|
|
onClick={() => onSubmit("public")}
|
|
|
|
>
|
|
|
|
Make Public
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
disabled={visibility === "unlisted"}
|
|
|
|
onClick={() => onSubmit("unlisted")}
|
|
|
|
>
|
|
|
|
Unlist
|
|
|
|
</Button>
|
|
|
|
<Button onClick={() => onSubmit("protected")}>
|
|
|
|
{visibility === "protected"
|
|
|
|
? "Change Password"
|
|
|
|
: "Protect with password"}
|
|
|
|
</Button>
|
|
|
|
</ButtonGroup>
|
|
|
|
)}
|
|
|
|
<PasswordModal
|
|
|
|
creating={true}
|
|
|
|
isOpen={passwordModalVisible}
|
|
|
|
onClose={onClosePasswordModal}
|
|
|
|
onSubmit={submitPassword}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default VisibilityControl
|