2022-03-30 23:01:24 -04:00
|
|
|
import { Button, useToasts, ButtonDropdown, Toggle, Input, useClickAway } from '@geist-ui/core'
|
2022-03-06 19:46:59 -05:00
|
|
|
import { useRouter } from 'next/router';
|
2022-03-30 23:01:24 -04:00
|
|
|
import { useCallback, useRef, useState } from 'react'
|
2022-03-12 23:40:28 -05:00
|
|
|
import generateUUID from '@lib/generate-uuid';
|
2022-03-11 18:38:37 -05:00
|
|
|
import FileDropzone from './drag-and-drop';
|
2022-03-06 19:46:59 -05:00
|
|
|
import styles from './post.module.css'
|
|
|
|
import Title from './title';
|
2022-03-15 15:15:54 -04:00
|
|
|
import Cookies from 'js-cookie'
|
2022-03-21 06:28:06 -04:00
|
|
|
import type { PostVisibility, Document as DocumentType } from '@lib/types';
|
2022-03-30 23:01:24 -04:00
|
|
|
import PasswordModal from './password-modal';
|
2022-03-21 20:20:41 -04:00
|
|
|
import getPostPath from '@lib/get-post-path';
|
2022-03-23 19:42:56 -04:00
|
|
|
import EditDocumentList from '@components/edit-document-list';
|
2022-03-22 01:50:25 -04:00
|
|
|
import { ChangeEvent } from 'react';
|
2022-03-30 23:01:24 -04:00
|
|
|
import DatePicker from 'react-datepicker';
|
2022-03-06 19:46:59 -05:00
|
|
|
const Post = () => {
|
|
|
|
const { setToast } = useToasts()
|
|
|
|
const router = useRouter();
|
|
|
|
const [title, setTitle] = useState<string>()
|
2022-03-30 23:01:24 -04:00
|
|
|
const [expiresAt, setExpiresAt] = useState<Date | null>(null)
|
2022-03-29 20:06:11 -04:00
|
|
|
|
2022-03-21 06:28:06 -04:00
|
|
|
const [docs, setDocs] = useState<DocumentType[]>([{
|
2022-03-06 19:46:59 -05:00
|
|
|
title: '',
|
|
|
|
content: '',
|
|
|
|
id: generateUUID()
|
|
|
|
}])
|
2022-03-22 01:50:25 -04:00
|
|
|
|
2022-03-21 06:28:06 -04:00
|
|
|
const [passwordModalVisible, setPasswordModalVisible] = useState(false)
|
2022-03-30 23:01:24 -04:00
|
|
|
|
|
|
|
const sendRequest = useCallback(async (url: string, data: { expiresAt: Date | null, visibility?: PostVisibility, title?: string, files?: DocumentType[], password?: string, userId: string }) => {
|
2022-03-21 06:28:06 -04:00
|
|
|
const res = await fetch(url, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"Authorization": `Bearer ${Cookies.get('drift-token')}`
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
title,
|
|
|
|
files: docs,
|
|
|
|
...data,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
if (res.ok) {
|
|
|
|
const json = await res.json()
|
2022-03-21 20:20:41 -04:00
|
|
|
router.push(getPostPath(json.visibility, json.id))
|
2022-03-21 06:28:06 -04:00
|
|
|
} else {
|
|
|
|
const json = await res.json()
|
|
|
|
setToast({
|
2022-03-26 03:05:05 -04:00
|
|
|
text: json.error.message || 'Please fill out all fields',
|
2022-03-21 06:28:06 -04:00
|
|
|
type: 'error'
|
|
|
|
})
|
2022-03-21 21:51:19 -04:00
|
|
|
setPasswordModalVisible(false)
|
|
|
|
setSubmitting(false)
|
2022-03-21 06:28:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}, [docs, router, setToast, title])
|
|
|
|
|
2022-03-06 19:46:59 -05:00
|
|
|
const [isSubmitting, setSubmitting] = useState(false)
|
|
|
|
|
2022-03-30 23:01:24 -04:00
|
|
|
const onSubmit = useCallback(async (visibility: PostVisibility, password?: string) => {
|
2022-03-21 06:28:06 -04:00
|
|
|
if (visibility === 'protected' && !password) {
|
|
|
|
setPasswordModalVisible(true)
|
|
|
|
return
|
|
|
|
}
|
2022-03-30 23:01:24 -04:00
|
|
|
|
|
|
|
setPasswordModalVisible(false)
|
|
|
|
|
2022-03-21 21:51:19 -04:00
|
|
|
setSubmitting(true)
|
2022-03-21 06:28:06 -04:00
|
|
|
|
2022-03-29 20:06:11 -04:00
|
|
|
let hasErrored = false
|
|
|
|
|
|
|
|
if (!title) {
|
|
|
|
setToast({
|
|
|
|
text: 'Please fill out the post title',
|
|
|
|
type: 'error'
|
|
|
|
})
|
|
|
|
setSubmitting(false)
|
|
|
|
hasErrored = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const doc of docs) {
|
|
|
|
if (!doc.title) {
|
|
|
|
setToast({
|
|
|
|
text: 'Please fill out all the document titles',
|
|
|
|
type: 'error'
|
|
|
|
})
|
|
|
|
setSubmitting(false)
|
|
|
|
hasErrored = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasErrored) return
|
|
|
|
|
2022-03-21 06:28:06 -04:00
|
|
|
await sendRequest('/server-api/posts/create', {
|
|
|
|
title,
|
|
|
|
files: docs,
|
|
|
|
visibility,
|
|
|
|
password,
|
2022-03-30 23:01:24 -04:00
|
|
|
userId: Cookies.get('drift-userid') || '',
|
|
|
|
expiresAt
|
2022-03-06 19:46:59 -05:00
|
|
|
})
|
2022-03-30 23:01:24 -04:00
|
|
|
}, [docs, expiresAt, sendRequest, setToast, title])
|
2022-03-21 06:28:06 -04:00
|
|
|
|
|
|
|
const onClosePasswordModal = () => {
|
|
|
|
setPasswordModalVisible(false)
|
2022-03-21 21:51:19 -04:00
|
|
|
setSubmitting(false)
|
2022-03-06 19:46:59 -05:00
|
|
|
}
|
|
|
|
|
2022-03-30 23:01:24 -04:00
|
|
|
const submitPassword = useCallback((password) => onSubmit('protected', password), [onSubmit])
|
|
|
|
|
|
|
|
const onChangeExpiration = useCallback((date) => setExpiresAt(date), [])
|
|
|
|
|
2022-03-22 01:50:25 -04:00
|
|
|
const onChangeTitle = useCallback((e: ChangeEvent<HTMLInputElement>) => {
|
|
|
|
setTitle(e.target.value)
|
|
|
|
}, [setTitle])
|
|
|
|
|
|
|
|
|
|
|
|
const updateDocTitle = useCallback((i: number) => (title: string) => {
|
|
|
|
setDocs((docs) => docs.map((doc, index) => i === index ? { ...doc, title } : doc))
|
|
|
|
}, [setDocs])
|
|
|
|
|
|
|
|
const updateDocContent = useCallback((i: number) => (content: string) => {
|
|
|
|
setDocs((docs) => docs.map((doc, index) => i === index ? { ...doc, content } : doc))
|
|
|
|
}, [setDocs])
|
|
|
|
|
|
|
|
const removeDoc = useCallback((i: number) => () => {
|
|
|
|
setDocs((docs) => docs.filter((_, index) => i !== index))
|
|
|
|
}, [setDocs])
|
2022-03-06 19:46:59 -05:00
|
|
|
|
2022-03-21 06:28:06 -04:00
|
|
|
const uploadDocs = useCallback((files: DocumentType[]) => {
|
2022-03-21 00:18:38 -04:00
|
|
|
// if no title is set and the only document is empty,
|
2022-03-21 22:14:40 -04:00
|
|
|
const isFirstDocEmpty = docs.length <= 1 && (docs.length ? docs[0].title === '' : true)
|
2022-03-21 00:18:38 -04:00
|
|
|
const shouldSetTitle = !title && isFirstDocEmpty
|
|
|
|
if (shouldSetTitle) {
|
|
|
|
if (files.length === 1) {
|
|
|
|
setTitle(files[0].title)
|
|
|
|
} else if (files.length > 1) {
|
|
|
|
setTitle('Uploaded files')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isFirstDocEmpty) setDocs(files)
|
2022-03-22 01:50:25 -04:00
|
|
|
else setDocs((docs) => [...docs, ...files])
|
2022-03-21 00:43:04 -04:00
|
|
|
}, [docs, title])
|
|
|
|
|
2022-03-23 19:42:56 -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()
|
|
|
|
// }))
|
|
|
|
// }
|
|
|
|
|
|
|
|
const onPaste = useCallback((e: any) => {
|
|
|
|
const pastedText = (e.clipboardData).getData('text')
|
|
|
|
|
|
|
|
if (pastedText) {
|
|
|
|
if (!title) {
|
|
|
|
setTitle("Pasted text")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [title])
|
|
|
|
|
2022-03-31 02:03:21 -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
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
2022-03-06 19:46:59 -05:00
|
|
|
return (
|
2022-03-24 18:12:54 -04:00
|
|
|
<div style={{ paddingBottom: 150 }}>
|
2022-03-22 01:50:25 -04:00
|
|
|
<Title title={title} onChange={onChangeTitle} />
|
2022-03-21 00:18:38 -04:00
|
|
|
<FileDropzone setDocs={uploadDocs} />
|
2022-03-23 19:42:56 -04:00
|
|
|
<EditDocumentList onPaste={onPaste} docs={docs} updateDocTitle={updateDocTitle} updateDocContent={updateDocContent} removeDoc={removeDoc} />
|
2022-03-06 19:46:59 -05:00
|
|
|
<div className={styles.buttons}>
|
|
|
|
<Button
|
|
|
|
className={styles.button}
|
|
|
|
onClick={() => {
|
|
|
|
setDocs([...docs, {
|
|
|
|
title: '',
|
|
|
|
content: '',
|
|
|
|
id: generateUUID()
|
|
|
|
}])
|
|
|
|
}}
|
|
|
|
type="default"
|
|
|
|
>
|
2022-03-09 04:33:22 -05:00
|
|
|
Add a File
|
2022-03-06 19:46:59 -05:00
|
|
|
</Button>
|
2022-03-31 02:03:21 -04:00
|
|
|
<div className={styles.rightButtons}>
|
2022-03-30 23:01:24 -04:00
|
|
|
{<DatePicker
|
|
|
|
onChange={onChangeExpiration}
|
2022-03-31 02:03:21 -04:00
|
|
|
customInput={<Input label="Expires at" clearable width="100%" height="40px" />}
|
2022-03-30 23:01:24 -04:00
|
|
|
placeholderText="Won't expire"
|
|
|
|
selected={expiresAt}
|
|
|
|
showTimeInput={true}
|
2022-03-31 02:03:21 -04:00
|
|
|
// @ts-ignore
|
|
|
|
customTimeInput={<CustomTimeInput />}
|
2022-03-30 23:01:24 -04:00
|
|
|
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()}
|
|
|
|
/>}
|
|
|
|
<ButtonDropdown loading={isSubmitting} type="success">
|
|
|
|
<ButtonDropdown.Item main onClick={() => onSubmit('private')}>Create Private</ButtonDropdown.Item>
|
|
|
|
<ButtonDropdown.Item onClick={() => onSubmit('public')} >Create Public</ButtonDropdown.Item>
|
|
|
|
<ButtonDropdown.Item onClick={() => onSubmit('unlisted')} >Create Unlisted</ButtonDropdown.Item>
|
|
|
|
<ButtonDropdown.Item onClick={() => onSubmit('protected')} >Create with Password</ButtonDropdown.Item>
|
|
|
|
</ButtonDropdown>
|
|
|
|
</div>
|
2022-03-06 19:46:59 -05:00
|
|
|
</div>
|
2022-03-30 23:01:24 -04:00
|
|
|
<PasswordModal isOpen={passwordModalVisible} onClose={onClosePasswordModal} onSubmit={submitPassword} />
|
2022-03-21 18:55:21 -04:00
|
|
|
</div>
|
2022-03-06 19:46:59 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-03-21 06:28:06 -04:00
|
|
|
export default Post
|