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 '../lib/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 saveDisabled = name === fold.name && _.isEqual(tags, fold.tags) && about === (fold.about ?? '') const onSubmit = async () => { setIsSubmitting(true) await updateFold(fold, { name, about, tags, }) setIsSubmitting(false) } return (