import { t, Trans } from "@lingui/macro" import { Anchor, Box, Button, Code, Container, Divider, Group, Input, NumberInput, Stack, Text, TextInput, Title } from "@mantine/core" import { useForm } from "@mantine/form" import { openConfirmModal } from "@mantine/modals" import { client, errorToStrings } from "app/client" import { Constants } from "app/constants" import { redirectToRootCategory, redirectToSelectedSource } from "app/slices/redirect" import { reloadTree } from "app/slices/tree" import { useAppDispatch, useAppSelector } from "app/store" import { CategoryModificationRequest } from "app/types" import { flattenCategoryTree } from "app/utils" import { Alert } from "components/Alert" import { CategorySelect } from "components/content/add/CategorySelect" import { Loader } from "components/Loader" import { useEffect } from "react" import { useAsync } from "react-async-hook" import { TbDeviceFloppy, TbTrash } from "react-icons/tb" import { useParams } from "react-router-dom" import useMutation from "use-mutation" export function CategoryDetailsPage() { const { id = Constants.categoryIds.all } = useParams() const apiKey = useAppSelector(state => state.user.profile?.apiKey) const dispatch = useAppDispatch() const query = useAsync(() => client.category.getRoot(), []) const category = query.result && flattenCategoryTree(query.result.data).find(c => c.id === id) const form = useForm() const { setValues } = form const [modify, modifyResult] = useMutation(client.category.modify, { onSuccess: () => { dispatch(reloadTree()) dispatch(redirectToSelectedSource()) }, }) const [deleteCategory, deleteCategoryResult] = useMutation(client.category.delete, { onSuccess: () => { dispatch(reloadTree()) dispatch(redirectToRootCategory()) }, }) const errors = [...errorToStrings(modifyResult.error), ...errorToStrings(deleteCategoryResult.error)] const openDeleteCategoryModal = () => { const categoryName = category?.name return openConfirmModal({ title: t`Delete Category`, children: ( Are you sure you want to delete category {categoryName}? ), labels: { confirm: t`Confirm`, cancel: t`Cancel` }, confirmProps: { color: "red" }, onConfirm: () => deleteCategory({ id: +id }), }) } useEffect(() => { if (!category) return setValues({ id: +category.id, name: category.name, parentId: category.parentId, position: category.position, }) }, [setValues, category]) if (!category) return return ( {errors.length > 0 && ( )}
{category.name} {apiKey && ( Link )} {!apiKey && Generate an API key in your profile first.}
) }