2022-11-09 21:38:05 -05:00
|
|
|
"use client"
|
|
|
|
|
|
|
|
import { Button, useToasts, Input, ButtonDropdown } from "@geist-ui/core/dist"
|
|
|
|
import { useRouter } from "next/navigation"
|
2022-11-09 22:46:12 -05:00
|
|
|
import { useCallback, useState } from "react"
|
2022-04-09 20:48:19 -04:00
|
|
|
import generateUUID from "@lib/generate-uuid"
|
|
|
|
import styles from "./post.module.css"
|
2022-11-12 03:58:21 -05:00
|
|
|
import EditDocumentList from "./edit-document-list"
|
2022-04-09 20:48:19 -04:00
|
|
|
import { ChangeEvent } from "react"
|
|
|
|
import DatePicker from "react-datepicker"
|
2022-04-11 22:57:41 -04:00
|
|
|
import getTitleForPostCopy from "@lib/get-title-for-post-copy"
|
2022-04-14 17:25:31 -04:00
|
|
|
import Description from "./description"
|
2022-11-11 22:17:44 -05:00
|
|
|
import { PostWithFiles } from "@lib/server/prisma"
|
2022-11-12 04:28:06 -05:00
|
|
|
import PasswordModal from "../../../components/password-modal"
|
2022-11-12 03:58:21 -05:00
|
|
|
import Title from "./title"
|
2022-11-12 04:28:06 -05:00
|
|
|
import FileDropzone from "./drag-and-drop"
|
2022-11-09 21:38:05 -05:00
|
|
|
const emptyDoc = {
|
|
|
|
title: "",
|
|
|
|
content: "",
|
|
|
|
id: generateUUID()
|
|
|
|
}
|
2022-04-02 01:55:27 -04:00
|
|
|
|
2022-11-14 20:24:35 -05:00
|
|
|
export type Document = {
|
|
|
|
title: string
|
|
|
|
content: string
|
|
|
|
id: string
|
|
|
|
}
|
|
|
|
|
2022-04-02 01:55:27 -04:00
|
|
|
const Post = ({
|
2022-04-09 20:48:19 -04:00
|
|
|
initialPost,
|
|
|
|
newPostParent
|
2022-04-02 01:55:27 -04:00
|
|
|
}: {
|
2022-11-09 21:38:05 -05:00
|
|
|
initialPost?: PostWithFiles
|
2022-04-09 20:48:19 -04:00
|
|
|
newPostParent?: string
|
2022-04-02 01:55:27 -04:00
|
|
|
}) => {
|
2022-04-09 20:48:19 -04:00
|
|
|
const { setToast } = useToasts()
|
|
|
|
const router = useRouter()
|
2022-11-09 21:38:05 -05:00
|
|
|
const [title, setTitle] = useState(
|
|
|
|
getTitleForPostCopy(initialPost?.title) || ""
|
2022-04-09 20:48:19 -04:00
|
|
|
)
|
2022-11-09 21:38:05 -05:00
|
|
|
const [description, setDescription] = useState(initialPost?.description || "")
|
|
|
|
const [expiresAt, setExpiresAt] = useState(initialPost?.expiresAt)
|
2022-04-09 20:48:19 -04:00
|
|
|
|
2022-11-14 20:24:35 -05:00
|
|
|
const defaultDocs: Document[] = initialPost
|
2022-11-09 21:38:05 -05:00
|
|
|
? initialPost.files?.map((doc) => ({
|
|
|
|
title: doc.title,
|
|
|
|
content: doc.content,
|
|
|
|
id: doc.id
|
|
|
|
}))
|
|
|
|
: [emptyDoc]
|
2022-04-11 22:57:41 -04:00
|
|
|
|
2022-11-09 21:38:05 -05:00
|
|
|
const [docs, setDocs] = useState(defaultDocs)
|
2022-04-09 20:48:19 -04:00
|
|
|
|
|
|
|
const [passwordModalVisible, setPasswordModalVisible] = useState(false)
|
|
|
|
|
|
|
|
const sendRequest = useCallback(
|
|
|
|
async (
|
|
|
|
url: string,
|
|
|
|
data: {
|
|
|
|
expiresAt: Date | null
|
2022-11-14 20:24:35 -05:00
|
|
|
visibility?: string
|
2022-04-09 20:48:19 -04:00
|
|
|
title?: string
|
2022-11-14 20:24:35 -05:00
|
|
|
files?: Document[]
|
2022-04-09 20:48:19 -04:00
|
|
|
password?: string
|
|
|
|
parentId?: string
|
|
|
|
}
|
|
|
|
) => {
|
|
|
|
const res = await fetch(url, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
2022-11-12 02:59:33 -05:00
|
|
|
"Content-Type": "application/json"
|
2022-04-09 20:48:19 -04:00
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
title,
|
2022-04-14 17:25:31 -04:00
|
|
|
description,
|
2022-04-09 20:48:19 -04:00
|
|
|
files: docs,
|
|
|
|
...data
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
if (res.ok) {
|
|
|
|
const json = await res.json()
|
2022-04-14 20:18:47 -04:00
|
|
|
router.push(`/post/${json.id}`)
|
2022-04-09 20:48:19 -04:00
|
|
|
} else {
|
|
|
|
const json = await res.json()
|
2022-11-12 02:59:33 -05:00
|
|
|
console.error(json)
|
2022-04-09 20:48:19 -04:00
|
|
|
setToast({
|
2022-11-12 02:59:33 -05:00
|
|
|
text: "Please fill out all fields",
|
2022-04-09 20:48:19 -04:00
|
|
|
type: "error"
|
|
|
|
})
|
|
|
|
setPasswordModalVisible(false)
|
|
|
|
setSubmitting(false)
|
|
|
|
}
|
|
|
|
},
|
2022-04-14 17:32:20 -04:00
|
|
|
[description, docs, router, setToast, title]
|
2022-04-09 20:48:19 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const [isSubmitting, setSubmitting] = useState(false)
|
|
|
|
|
|
|
|
const onSubmit = useCallback(
|
2022-11-14 20:24:35 -05:00
|
|
|
async (visibility: string, password?: string) => {
|
2022-04-09 20:48:19 -04:00
|
|
|
if (visibility === "protected" && !password) {
|
|
|
|
setPasswordModalVisible(true)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
setPasswordModalVisible(false)
|
|
|
|
|
|
|
|
setSubmitting(true)
|
|
|
|
|
|
|
|
let hasErrored = false
|
|
|
|
|
|
|
|
if (!title) {
|
|
|
|
setToast({
|
|
|
|
text: "Please fill out the post title",
|
|
|
|
type: "error"
|
|
|
|
})
|
|
|
|
hasErrored = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!docs.length) {
|
|
|
|
setToast({
|
|
|
|
text: "Please add at least one document",
|
|
|
|
type: "error"
|
|
|
|
})
|
|
|
|
hasErrored = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const doc of docs) {
|
|
|
|
if (!doc.title) {
|
|
|
|
setToast({
|
|
|
|
text: "Please fill out all the document titles",
|
|
|
|
type: "error"
|
|
|
|
})
|
|
|
|
hasErrored = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasErrored) {
|
|
|
|
setSubmitting(false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-12 02:59:33 -05:00
|
|
|
await sendRequest("/api/post", {
|
2022-04-09 20:48:19 -04:00
|
|
|
title,
|
|
|
|
files: docs,
|
|
|
|
visibility,
|
|
|
|
password,
|
2022-11-09 21:38:05 -05:00
|
|
|
expiresAt: expiresAt || null,
|
2022-04-09 20:48:19 -04:00
|
|
|
parentId: newPostParent
|
|
|
|
})
|
|
|
|
},
|
|
|
|
[docs, expiresAt, newPostParent, sendRequest, setToast, title]
|
|
|
|
)
|
|
|
|
|
|
|
|
const onClosePasswordModal = () => {
|
|
|
|
setPasswordModalVisible(false)
|
|
|
|
setSubmitting(false)
|
|
|
|
}
|
|
|
|
|
2022-11-08 03:23:28 -05:00
|
|
|
const submitPassword = (password: string) => onSubmit("protected", password)
|
2022-04-09 20:48:19 -04:00
|
|
|
|
2022-11-14 20:24:35 -05:00
|
|
|
const onChangeExpiration = (date: Date) => setExpiresAt(date)
|
2022-04-09 20:48:19 -04:00
|
|
|
|
2022-11-14 20:24:35 -05:00
|
|
|
const onChangeTitle =
|
2022-04-09 20:48:19 -04:00
|
|
|
(e: ChangeEvent<HTMLInputElement>) => {
|
|
|
|
setTitle(e.target.value)
|
2022-11-14 20:24:35 -05:00
|
|
|
}
|
2022-04-09 20:48:19 -04:00
|
|
|
|
2022-11-14 20:24:35 -05:00
|
|
|
const onChangeDescription =
|
2022-04-14 17:32:20 -04:00
|
|
|
(e: ChangeEvent<HTMLInputElement>) => {
|
2022-04-14 17:25:31 -04:00
|
|
|
setDescription(e.target.value)
|
2022-11-14 20:24:35 -05:00
|
|
|
}
|
2022-04-14 17:25:31 -04:00
|
|
|
|
2022-11-14 20:24:35 -05:00
|
|
|
const updateDocTitle = (i: number) => (title: string) => {
|
2022-04-09 20:48:19 -04:00
|
|
|
setDocs((docs) =>
|
|
|
|
docs.map((doc, index) => (i === index ? { ...doc, title } : doc))
|
|
|
|
)
|
2022-11-14 20:24:35 -05:00
|
|
|
}
|
2022-04-09 20:48:19 -04:00
|
|
|
|
2022-11-08 03:23:28 -05:00
|
|
|
const updateDocContent = (i: number) => (content: string) => {
|
|
|
|
setDocs((docs) =>
|
|
|
|
docs.map((doc, index) => (i === index ? { ...doc, content } : doc))
|
|
|
|
)
|
|
|
|
}
|
2022-04-09 20:48:19 -04:00
|
|
|
|
2022-11-08 03:23:28 -05:00
|
|
|
const removeDoc = (i: number) => () => {
|
|
|
|
setDocs((docs) => docs.filter((_, index) => i !== index))
|
|
|
|
}
|
2022-04-09 20:48:19 -04:00
|
|
|
|
2022-11-14 20:24:35 -05:00
|
|
|
const uploadDocs = (files: Document[]) => {
|
2022-11-08 03:23:28 -05:00
|
|
|
// if no title is set and the only document is empty,
|
|
|
|
const isFirstDocEmpty =
|
|
|
|
docs.length <= 1 && (docs.length ? docs[0].title === "" : true)
|
|
|
|
const shouldSetTitle = !title && isFirstDocEmpty
|
|
|
|
if (shouldSetTitle) {
|
|
|
|
if (files.length === 1) {
|
|
|
|
setTitle(files[0].title)
|
|
|
|
} else if (files.length > 1) {
|
|
|
|
setTitle("Uploaded files")
|
2022-04-09 20:48:19 -04:00
|
|
|
}
|
2022-11-08 03:23:28 -05:00
|
|
|
}
|
2022-04-09 20:48:19 -04:00
|
|
|
|
2022-11-08 03:23:28 -05:00
|
|
|
if (isFirstDocEmpty) setDocs(files)
|
|
|
|
else setDocs((docs) => [...docs, ...files])
|
|
|
|
}
|
2022-04-09 20:48:19 -04:00
|
|
|
|
|
|
|
// pasted files
|
|
|
|
// const files = e.clipboardData.files as File[]
|
|
|
|
// if (files.length) {
|
|
|
|
// const docs = Array.from(files).map((file) => ({
|
|
|
|
// title: file.name,
|
|
|
|
// content: '',
|
|
|
|
// id: generateUUID()
|
|
|
|
// }))
|
|
|
|
// }
|
|
|
|
|
2022-11-09 21:38:05 -05:00
|
|
|
const onPaste = (e: ClipboardEvent) => {
|
|
|
|
const pastedText = e.clipboardData?.getData("text")
|
2022-04-09 20:48:19 -04:00
|
|
|
|
2022-11-09 21:38:05 -05:00
|
|
|
if (pastedText) {
|
|
|
|
if (!title) {
|
|
|
|
setTitle("Pasted text")
|
2022-04-09 20:48:19 -04:00
|
|
|
}
|
2022-11-09 21:38:05 -05:00
|
|
|
}
|
|
|
|
}
|
2022-04-09 20:48:19 -04:00
|
|
|
|
|
|
|
const CustomTimeInput = ({
|
|
|
|
date,
|
|
|
|
value,
|
|
|
|
onChange
|
|
|
|
}: {
|
|
|
|
date: Date
|
|
|
|
value: string
|
|
|
|
onChange: (date: string) => void
|
|
|
|
}) => (
|
|
|
|
<input
|
|
|
|
type="time"
|
|
|
|
value={value}
|
|
|
|
onChange={(e) => {
|
|
|
|
if (!isNaN(date.getTime())) {
|
|
|
|
onChange(e.target.value || date.toISOString().slice(11, 16))
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
style={{
|
|
|
|
backgroundColor: "var(--bg)",
|
|
|
|
border: "1px solid var(--light-gray)",
|
|
|
|
borderRadius: "var(--radius)"
|
|
|
|
}}
|
|
|
|
required
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
2022-11-12 02:59:33 -05:00
|
|
|
// 150 so the post dropdown doesn't overflow
|
2022-04-09 20:48:19 -04:00
|
|
|
<div style={{ paddingBottom: 150 }}>
|
|
|
|
<Title title={title} onChange={onChangeTitle} />
|
2022-04-14 17:25:31 -04:00
|
|
|
<Description description={description} onChange={onChangeDescription} />
|
2022-04-09 20:48:19 -04:00
|
|
|
<FileDropzone setDocs={uploadDocs} />
|
|
|
|
<EditDocumentList
|
|
|
|
onPaste={onPaste}
|
|
|
|
docs={docs}
|
|
|
|
updateDocTitle={updateDocTitle}
|
|
|
|
updateDocContent={updateDocContent}
|
|
|
|
removeDoc={removeDoc}
|
|
|
|
/>
|
|
|
|
<div className={styles.buttons}>
|
|
|
|
<Button
|
|
|
|
className={styles.button}
|
|
|
|
onClick={() => {
|
|
|
|
setDocs([
|
|
|
|
...docs,
|
|
|
|
{
|
|
|
|
title: "",
|
|
|
|
content: "",
|
|
|
|
id: generateUUID()
|
|
|
|
}
|
|
|
|
])
|
|
|
|
}}
|
|
|
|
type="default"
|
|
|
|
>
|
|
|
|
Add a File
|
|
|
|
</Button>
|
|
|
|
<div className={styles.rightButtons}>
|
2022-11-14 02:02:31 -05:00
|
|
|
<DatePicker
|
|
|
|
onChange={onChangeExpiration}
|
|
|
|
customInput={
|
|
|
|
<Input label="Expires at" clearable width="100%" height="40px" />
|
|
|
|
}
|
|
|
|
placeholderText="Won't expire"
|
|
|
|
selected={expiresAt}
|
|
|
|
showTimeInput={true}
|
|
|
|
// @ts-ignore
|
|
|
|
customTimeInput={<CustomTimeInput />}
|
|
|
|
timeInputLabel="Time:"
|
|
|
|
dateFormat="MM/dd/yyyy h:mm aa"
|
|
|
|
className={styles.datePicker}
|
|
|
|
clearButtonTitle={"Clear"}
|
|
|
|
// TODO: investigate why this causes margin shift if true
|
|
|
|
enableTabLoop={false}
|
|
|
|
minDate={new Date()}
|
|
|
|
/>
|
2022-04-09 20:48:19 -04:00
|
|
|
<ButtonDropdown loading={isSubmitting} type="success">
|
2022-11-09 21:38:05 -05:00
|
|
|
<ButtonDropdown.Item main onClick={() => onSubmit("unlisted")}>
|
2022-11-08 03:23:28 -05:00
|
|
|
Create Unlisted
|
|
|
|
</ButtonDropdown.Item>
|
2022-11-09 21:38:05 -05:00
|
|
|
<ButtonDropdown.Item onClick={() => onSubmit("private")}>
|
2022-04-09 20:48:19 -04:00
|
|
|
Create Private
|
|
|
|
</ButtonDropdown.Item>
|
|
|
|
<ButtonDropdown.Item onClick={() => onSubmit("public")}>
|
|
|
|
Create Public
|
|
|
|
</ButtonDropdown.Item>
|
|
|
|
<ButtonDropdown.Item onClick={() => onSubmit("protected")}>
|
|
|
|
Create with Password
|
|
|
|
</ButtonDropdown.Item>
|
|
|
|
</ButtonDropdown>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<PasswordModal
|
|
|
|
creating={true}
|
|
|
|
isOpen={passwordModalVisible}
|
|
|
|
onClose={onClosePasswordModal}
|
|
|
|
onSubmit={submitPassword}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)
|
2022-03-06 19:46:59 -05:00
|
|
|
}
|
|
|
|
|
2022-03-21 06:28:06 -04:00
|
|
|
export default Post
|