import { 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, useAsyncCallback } from "react-async-hook" import { TbDeviceFloppy, TbTrash } from "react-icons/tb" import { useParams } from "react-router-dom" export function CategoryDetailsPage() { const { id = Constants.categories.all.id } = useParams() const apiKey = useAppSelector(state => state.user.profile?.apiKey) const dispatch = useAppDispatch() const query = useAsync(() => client.category.getRoot(), []) const category = id === Constants.categories.starred.id ? Constants.categories.starred : query.result && flattenCategoryTree(query.result.data).find(c => c.id === id) const form = useForm() const { setValues } = form const modifyCategory = useAsyncCallback(client.category.modify, { onSuccess: () => { dispatch(reloadTree()) dispatch(redirectToSelectedSource()) }, }) const deleteCategory = useAsyncCallback(client.category.delete, { onSuccess: () => { dispatch(reloadTree()) dispatch(redirectToRootCategory()) }, }) const openDeleteCategoryModal = () => { const categoryName = category?.name return openConfirmModal({ title: Delete Category, children: ( Are you sure you want to delete category {categoryName}? ), labels: { confirm: Confirm, cancel: Cancel }, confirmProps: { color: "red" }, onConfirm: () => deleteCategory.execute({ id: +id }), }) } useEffect(() => { if (!category) return setValues({ id: +category.id, name: category.name, parentId: category.parentId, position: category.position, }) }, [setValues, category]) const editable = id !== Constants.categories.all.id && id !== Constants.categories.starred.id if (!category) return return ( {modifyCategory.error && ( )} {deleteCategory.error && ( )}
{category.name} Generated feed url}> {apiKey && ( Link )} {!apiKey && Generate an API key in your profile first.} {editable && ( <> Name} {...form.getInputProps("name")} required /> Parent Category} {...form.getInputProps("parentId")} clearable withoutCategoryIds={[id]} /> Position} {...form.getInputProps("position")} required min={0} /> )} {editable && ( <> )}
) }