import { useLanguageProfiles } from "@/apis/hooks"; import { MultiSelector, Selector } from "@/components/inputs"; import { useModals, withModal } from "@/modules/modals"; import { GetItemId, useSelectorOptions } from "@/utilities"; import { Button, Divider, Group, LoadingOverlay, Stack } from "@mantine/core"; import { useForm } from "@mantine/hooks"; import { FunctionComponent, useMemo } from "react"; import { UseMutationResult } from "react-query"; interface Props { mutation: UseMutationResult; item: Item.Base | null; onComplete?: () => void; onCancel?: () => void; } const ItemEditForm: FunctionComponent = ({ mutation, item, onComplete, onCancel, }) => { const { data, isFetching } = useLanguageProfiles(); const { isLoading, mutate } = mutation; const modals = useModals(); const profileOptions = useSelectorOptions( data ?? [], (v) => v.name ?? "Unknown", (v) => v.profileId.toString() ?? "-1" ); const profile = useMemo( () => data?.find((v) => v.profileId === item?.profileId) ?? null, [data, item?.profileId] ); const form = useForm({ initialValues: { profile: profile ?? null, }, }); const options = useSelectorOptions( item?.audio_language ?? [], (v) => v.name, (v) => v.code2 ); const isOverlayVisible = isLoading || isFetching || item === null; return (
{ if (item) { const itemId = GetItemId(item); if (itemId) { mutate({ id: [itemId], profileid: [profile?.profileId ?? null] }); onComplete?.(); modals.closeSelf(); return; } } form.setErrors({ profile: "Invalid profile" }); })} >
); }; export const ItemEditModal = withModal(ItemEditForm, "item-editor", { title: "Editor", size: "md", }); export default ItemEditForm;