import { CopyOutlined, DeleteOutlined } from '@ant-design/icons' import { Button, message, Popconfirm, Spin, Table } from 'antd' import { ColumnsType } from 'antd/es/table' import ClipboardJS from 'clipboard' import dayjs from 'dayjs' import 'dayjs/locale/ru' import duration from 'dayjs/plugin/duration' import relativeTime from 'dayjs/plugin/relativeTime' import { Layout, FormComponents } from 'isp-ui-kit' import { useEffect, useState } from 'react' import { useForm } from 'react-hook-form' import { ValidationRules } from '@constants/form/validationRules.ts' import Modal from '@widgets/Modal' // import CanEdit from '@components/CanEdit' import { ApplicationTokenType, NewApplicationTokenType } from '@pages/ApplicationsPage/applications.type.ts' import tokensApi from '@services/tokensService.ts' import './tokens.scss' import CanEdit from '@components/CanEdit' const { FormSelect } = FormComponents const { EmptyData } = Layout interface TokenPropTypes { id: number } // eslint-disable-next-line import/no-named-as-default-member dayjs.locale('ru') dayjs.extend(duration) dayjs.extend(relativeTime) const TokenContent = ({ id }: TokenPropTypes) => { const [showApplicationsModal, setShowApplicationsModal] = useState(false) const { handleSubmit: handleSubmitTokens, control: controlTokens, reset } = useForm({ mode: 'onChange' }) const { data, isLoading } = tokensApi.useGetTokensByAppIdQuery({ id }) const [createToken] = tokensApi.useCreateTokenMutation() const [revokeToken] = tokensApi.useRevokeTokensMutation() useEffect(() => { const clipboard = new ClipboardJS('.copy-btn') clipboard.on('success', () => { message.success('Токен скопирован в буфер обмена') }) clipboard.on('error', () => { message.error('Ошибка копирования') }) return () => { clipboard.destroy() } }, []) const handleCreateToken = (data: any) => { if (id) { const newToken: NewApplicationTokenType = { expireTimeMs: data.expireTime, appId: Number(id) } createToken(newToken) .then(() => { setShowApplicationsModal(false) reset() message.info('Элемент добавлен') }) .catch(() => { message.error('Ошибка добавления элемента') }) } } function shortenString(longString: string) { if (longString.length <= 6) { return {longString} } else { return ( {longString.slice(0, 3)}...{longString.slice(-3)} ) } } const handleRevokeToken = (elem: ApplicationTokenType) => { const currentRemoveToken = { appId: id, tokens: [elem.token] } revokeToken(currentRemoveToken) .unwrap() .then(() => message.success('Элемент удален')) .catch(() => message.info('Ошибка удаления элемента')) } const columns: ColumnsType = [ { title: 'Токен', dataIndex: 'token', key: 'token', render: (record) =>
{shortenString(record)}
}, { title: 'Дата/Время выпуска', dataIndex: 'createdAt', key: 'createdAt', render: (record) => { const FULL_DATE_FORMAT = 'DD-MM-YYYY/HH:mm' const formatTime = dayjs(record.createdAt).format(FULL_DATE_FORMAT) return
{formatTime}
} }, { title: 'Срок действия', dataIndex: 'expireTime', key: 'expireTime', render: (value) => { const time = dayjs.duration(value).humanize() if (value > 0) { return
{time}
} return
Отсутствует
} }, { title: 'Истекает', dataIndex: 'expireTime', key: 'expireTime', render: (value, record) => { const FULL_DATE_FORMAT = 'DD-MM-YYYY, HH:mm' const formatTime = dayjs(record.createdAt) .add(record.expireTime) .format(FULL_DATE_FORMAT) return value > 0 ? {formatTime} : Никогда } }, { title: 'Скопировать', dataIndex: 'token', key: 'copyToken', align: 'center', render: (record) => { return ( ) } }, { title: 'Отозвать', dataIndex: 'appId', key: 'recall', align: 'center', render: (_, record) => { return ( handleRevokeToken(record)} > ) } } ] const tokensOptions = [ { value: -1, label: 'Бессрочно' }, { value: 3600000, label: 'Один час' }, { value: 86400000, label: 'Один день' }, { value: 2592000000, label: '30 дней' }, { value: 31536000000, label: 'Один год' } ] if (!id) { return } if (isLoading) { return } const handleShowAddModalToken = () => { setShowApplicationsModal(true) } return (
record.createdAt} pagination={false} columns={columns} dataSource={data} /> setShowApplicationsModal(false)} >
) } export default TokenContent