import invariant from "invariant"; import { observer } from "mobx-react"; import { useState } from "react"; import * as React from "react"; import { Trans, useTranslation } from "react-i18next"; import { toast } from "sonner"; import { CollectionValidation } from "@shared/validations"; import Button from "~/components/Button"; import Flex from "~/components/Flex"; import IconPicker from "~/components/IconPicker"; import Input from "~/components/Input"; import InputSelect from "~/components/InputSelect"; import Text from "~/components/Text"; import useStores from "~/hooks/useStores"; type Props = { collectionId: string; onSubmit: () => void; }; const CollectionEdit = ({ collectionId, onSubmit }: Props) => { const { collections } = useStores(); const collection = collections.get(collectionId); invariant(collection, "Collection not found"); const [name, setName] = useState(collection.name); const [icon, setIcon] = useState(collection.icon); const [color, setColor] = useState(collection.color || "#4E5C6E"); const [sort, setSort] = useState<{ field: string; direction: "asc" | "desc"; }>(collection.sort); const [isSaving, setIsSaving] = useState(false); const { t } = useTranslation(); const handleSubmit = React.useCallback( async (ev: React.SyntheticEvent) => { ev.preventDefault(); setIsSaving(true); try { await collection.save({ name, icon, color, sort, }); onSubmit(); toast.success(t("The collection was updated")); } catch (err) { toast.error(err.message); } finally { setIsSaving(false); } }, [collection, color, icon, name, onSubmit, sort, t] ); const handleSortChange = (value: string) => { const [field, direction] = value.split("."); if (direction === "asc" || direction === "desc") { setSort({ field, direction, }); } }; const handleNameChange = (ev: React.ChangeEvent) => { setName(ev.target.value); }; const handleChange = (color: string, icon: string) => { setColor(color); setIcon(icon); }; return (
You can edit the name and other details at any time, however doing so often might confuse your team mates.
); }; export default observer(CollectionEdit);