import { useState } from 'react' import _ from 'lodash' import clsx from 'clsx' import { PencilIcon } from '@heroicons/react/outline' import { Fold } from '../../common/fold' import { parseWordsAsTags } from '../../common/util/parse' import { deleteFold, updateFold } from '../lib/firebase/folds' import { toCamelCase } from '../../common/util/format' import { Spacer } from './layout/spacer' import { TagsList } from './tags-list' import { useRouter } from 'next/router' export function EditFoldButton(props: { fold: Fold; className?: string }) { const { fold, className } = props const router = useRouter() const [name, setName] = useState(fold.name) const [about, setAbout] = useState(fold.about ?? '') const initialOtherTags = fold?.tags.filter((tag) => tag !== toCamelCase(name)).join(', ') ?? '' const [otherTags, setOtherTags] = useState(initialOtherTags) const [isSubmitting, setIsSubmitting] = useState(false) const tags = parseWordsAsTags(toCamelCase(name) + ' ' + otherTags) const lowercaseTags = tags.map((tag) => tag.toLowerCase()) const saveDisabled = name === fold.name && _.isEqual(tags, fold.tags) && about === (fold.about ?? '') const onSubmit = async () => { setIsSubmitting(true) await updateFold(fold, { name, about, tags, lowercaseTags, }) setIsSubmitting(false) } return (
setName(e.target.value || '')} />
setAbout(e.target.value || '')} />
setOtherTags(e.target.value || '')} />
) }