2022-01-21 23:21:46 +00:00
|
|
|
import clsx from 'clsx'
|
|
|
|
import _ from 'lodash'
|
|
|
|
import { ArrowCircleLeftIcon } from '@heroicons/react/solid'
|
|
|
|
import { useState } from 'react'
|
|
|
|
import { Fold } from '../../../../common/fold'
|
|
|
|
import { parseWordsAsTags } from '../../../../common/util/parse'
|
|
|
|
import { Col } from '../../../components/layout/col'
|
|
|
|
import { Spacer } from '../../../components/layout/spacer'
|
|
|
|
import { Page } from '../../../components/page'
|
|
|
|
import { TagsList } from '../../../components/tags-list'
|
|
|
|
import {
|
|
|
|
foldPath,
|
|
|
|
getFoldBySlug,
|
|
|
|
updateFold,
|
|
|
|
} from '../../../lib/firebase/folds'
|
|
|
|
import Custom404 from '../../404'
|
|
|
|
import { SiteLink } from '../../../components/site-link'
|
2022-01-22 21:47:24 +00:00
|
|
|
import { toCamelCase } from '../../../lib/util/format'
|
2022-01-23 06:51:19 +00:00
|
|
|
import { useFold } from '../../../hooks/use-fold'
|
2022-01-21 23:21:46 +00:00
|
|
|
|
|
|
|
export async function getStaticProps(props: { params: { foldSlug: string } }) {
|
|
|
|
const { foldSlug } = props.params
|
|
|
|
|
|
|
|
const fold = await getFoldBySlug(foldSlug)
|
|
|
|
|
|
|
|
return {
|
|
|
|
props: { fold },
|
|
|
|
|
|
|
|
revalidate: 60, // regenerate after a minute
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getStaticPaths() {
|
|
|
|
return { paths: [], fallback: 'blocking' }
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function EditFoldPage(props: { fold: Fold | null }) {
|
2022-01-23 06:51:19 +00:00
|
|
|
const fold = useFold(props.fold?.id ?? '') ?? props.fold
|
2022-01-21 23:21:46 +00:00
|
|
|
const [name, setName] = useState(fold?.name ?? '')
|
2022-01-22 21:47:24 +00:00
|
|
|
|
|
|
|
const initialOtherTags =
|
|
|
|
fold?.tags.filter((tag) => tag !== toCamelCase(name)).join(', ') ?? ''
|
|
|
|
|
|
|
|
const [otherTags, setOtherTags] = useState(initialOtherTags)
|
2022-01-21 23:21:46 +00:00
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
|
|
|
|
|
|
|
if (!fold) return <Custom404 />
|
|
|
|
|
2022-01-22 21:47:24 +00:00
|
|
|
const tags = parseWordsAsTags(toCamelCase(name) + ' ' + otherTags)
|
|
|
|
|
2022-01-21 23:21:46 +00:00
|
|
|
const saveDisabled =
|
2022-01-22 21:47:24 +00:00
|
|
|
!name || (name === fold.name && _.isEqual(tags, fold.tags))
|
2022-01-21 23:21:46 +00:00
|
|
|
|
|
|
|
const onSubmit = async () => {
|
|
|
|
setIsSubmitting(true)
|
|
|
|
|
2022-01-22 21:47:24 +00:00
|
|
|
await updateFold(fold, {
|
|
|
|
name,
|
|
|
|
tags,
|
|
|
|
})
|
2022-01-21 23:21:46 +00:00
|
|
|
|
|
|
|
setIsSubmitting(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Page>
|
|
|
|
<Col className="items-center">
|
|
|
|
<Col className="max-w-2xl w-full px-2 sm:px-0">
|
|
|
|
<SiteLink href={foldPath(fold)}>
|
|
|
|
<ArrowCircleLeftIcon className="h-5 w-5 text-gray-500 inline mr-1" />{' '}
|
|
|
|
{fold.name}
|
|
|
|
</SiteLink>
|
|
|
|
|
|
|
|
<Spacer h={4} />
|
|
|
|
|
|
|
|
<div className="form-control w-full">
|
|
|
|
<label className="label">
|
|
|
|
<span className="mb-1">Fold name</span>
|
|
|
|
</label>
|
|
|
|
|
|
|
|
<input
|
|
|
|
placeholder="Your fold name"
|
|
|
|
className="input input-bordered resize-none"
|
|
|
|
disabled={isSubmitting}
|
|
|
|
value={name}
|
|
|
|
onChange={(e) => setName(e.target.value || '')}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<Spacer h={4} />
|
|
|
|
|
|
|
|
<div className="form-control w-full">
|
|
|
|
<label className="label">
|
|
|
|
<span className="mb-1">Tags</span>
|
|
|
|
</label>
|
|
|
|
|
|
|
|
<input
|
|
|
|
placeholder="Politics, Economics, Rationality"
|
|
|
|
className="input input-bordered resize-none"
|
|
|
|
disabled={isSubmitting}
|
2022-01-22 21:47:24 +00:00
|
|
|
value={otherTags}
|
|
|
|
onChange={(e) => setOtherTags(e.target.value || '')}
|
2022-01-21 23:21:46 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<Spacer h={4} />
|
2022-01-22 21:47:24 +00:00
|
|
|
<TagsList tags={tags.map((tag) => `#${tag}`)} noLink />
|
2022-01-21 23:21:46 +00:00
|
|
|
<Spacer h={4} />
|
|
|
|
|
|
|
|
<button
|
|
|
|
className={clsx(
|
|
|
|
'btn self-end',
|
|
|
|
saveDisabled ? 'btn-disabled' : 'btn-primary',
|
|
|
|
isSubmitting && 'loading'
|
|
|
|
)}
|
|
|
|
onClick={onSubmit}
|
|
|
|
>
|
|
|
|
Save
|
|
|
|
</button>
|
|
|
|
</Col>
|
|
|
|
</Col>
|
|
|
|
</Page>
|
|
|
|
)
|
|
|
|
}
|