From bc1decdbfc55023b4060cc6191ab81cf1632a8dd Mon Sep 17 00:00:00 2001 From: jahooma Date: Sat, 22 Jan 2022 15:47:24 -0600 Subject: [PATCH] Initialize folds with one primary tag --- web/lib/util/format.ts | 11 ++++++++++ web/pages/fold/[foldSlug]/edit.tsx | 27 +++++++++++++++---------- web/pages/folds.tsx | 32 ++++++++++++++++++++---------- 3 files changed, 49 insertions(+), 21 deletions(-) diff --git a/web/lib/util/format.ts b/web/lib/util/format.ts index 9d0496b2..741200c9 100644 --- a/web/lib/util/format.ts +++ b/web/lib/util/format.ts @@ -16,3 +16,14 @@ export function formatWithCommas(amount: number) { export function formatPercent(zeroToOne: number) { return Math.round(zeroToOne * 100) + '%' } + +export function toCamelCase(words: string) { + return words + .split(' ') + .map((word) => word.trim()) + .filter((word) => word) + .map((word, index) => + index === 0 ? word : word[0].toLocaleUpperCase() + word.substring(1) + ) + .join('') +} diff --git a/web/pages/fold/[foldSlug]/edit.tsx b/web/pages/fold/[foldSlug]/edit.tsx index 30fed0ae..2a093dfd 100644 --- a/web/pages/fold/[foldSlug]/edit.tsx +++ b/web/pages/fold/[foldSlug]/edit.tsx @@ -15,6 +15,7 @@ import { } from '../../../lib/firebase/folds' import Custom404 from '../../404' import { SiteLink } from '../../../components/site-link' +import { toCamelCase } from '../../../lib/util/format' export async function getStaticProps(props: { params: { foldSlug: string } }) { const { foldSlug } = props.params @@ -36,20 +37,27 @@ export default function EditFoldPage(props: { fold: Fold | null }) { const { fold } = props const [name, setName] = useState(fold?.name ?? '') - const [tags, setTags] = useState(fold?.tags.join(', ') ?? '') + + const initialOtherTags = + fold?.tags.filter((tag) => tag !== toCamelCase(name)).join(', ') ?? '' + + const [otherTags, setOtherTags] = useState(initialOtherTags) const [isSubmitting, setIsSubmitting] = useState(false) if (!fold) return + const tags = parseWordsAsTags(toCamelCase(name) + ' ' + otherTags) + const saveDisabled = - !name || - !tags || - (name === fold.name && _.isEqual(parseWordsAsTags(tags), fold.tags)) + !name || (name === fold.name && _.isEqual(tags, fold.tags)) const onSubmit = async () => { setIsSubmitting(true) - await updateFold(fold, { name, tags: parseWordsAsTags(tags) }) + await updateFold(fold, { + name, + tags, + }) setIsSubmitting(false) } @@ -90,16 +98,13 @@ export default function EditFoldPage(props: { fold: Fold | null }) { placeholder="Politics, Economics, Rationality" className="input input-bordered resize-none" disabled={isSubmitting} - value={tags} - onChange={(e) => setTags(e.target.value || '')} + value={otherTags} + onChange={(e) => setOtherTags(e.target.value || '')} /> - `#${tag}`)} - noLink - /> + `#${tag}`)} noLink />