import { useIntl } from 'react-intl'; import Checkbox from '../../components/Common/Checkbox'; import SubmitButton from '../../components/Common/SubmitButton'; import TextArea from '../../components/Common/TextArea'; import TextInput from '../../components/Common/TextInput'; import { useRSForm } from '../../context/RSFormContext'; import { useCallback, useEffect, useRef, useState } from 'react'; import Button from '../../components/Common/Button'; import { CrownIcon, DownloadIcon, DumpBinIcon, ShareIcon } from '../../components/Icons'; import { useUsers } from '../../context/UsersContext'; import { useNavigate } from 'react-router-dom'; import { toast } from 'react-toastify'; import fileDownload from 'js-file-download'; import { AxiosResponse } from 'axios'; function RSFormCard() { const navigate = useNavigate(); const intl = useIntl(); const { getUserLabel } = useUsers(); const { schema, update, download, reload, isEditable, isClaimable, processing, destroy, claim } = useRSForm(); const [title, setTitle] = useState(''); const [alias, setAlias] = useState(''); const [comment, setComment] = useState(''); const [common, setCommon] = useState(false); useEffect(() => { setTitle(schema!.title) setAlias(schema!.alias) setComment(schema!.comment) setCommon(schema!.is_common) }, [schema]); const handleSubmit = (event: React.FormEvent) => { event.preventDefault(); const data = { 'title': title, 'alias': alias, 'comment': comment, 'is_common': common, }; update(data, () => { toast.success('Изменения сохранены'); reload(); }); }; const handleDelete = useCallback(() => { if (window.confirm('Вы уверены, что хотите удалить данную схему?')) { destroy(() => { toast.success('Схема удалена'); navigate('/rsforms?filter=personal'); }); } }, [destroy, navigate]); const handleClaimOwner = useCallback(() => { if (window.confirm('Вы уверены, что хотите стать владельцем данной схемы?')) { claim(() => { toast.success('Вы стали владельцем схемы'); reload(); }); } }, [claim, reload]); const handleDownload = useCallback(() => { download((response: AxiosResponse) => { try { const fileName = (schema?.alias || 'Schema') + '.trs'; fileDownload(response.data, fileName); } catch (error: any) { toast.error(error.message); } }); }, [download, schema?.alias]); const handleShare = useCallback(() => { const url = window.location.href + '&share'; navigator.clipboard.writeText(url); toast.success(`Ссылка скопирована: ${url}`); }, []); return (
setTitle(event.target.value)} /> setAlias(event.target.value)} />