From 61a1ec09642549a042d6b4ec3241fdc33778c026 Mon Sep 17 00:00:00 2001 From: veselovnk Date: Sat, 27 Apr 2024 16:31:29 +0300 Subject: [PATCH] MSP-3203 create ActiveConfigurationsTable --- .../active-configurations-table.scss | 36 +++ .../active-configurations-table.type.ts | 8 + .../ActiveConfigurationsTable/index.tsx | 207 ++++++++++++++++++ 3 files changed, 251 insertions(+) create mode 100644 src/components/ActiveConfigurationsTable/active-configurations-table.scss create mode 100644 src/components/ActiveConfigurationsTable/active-configurations-table.type.ts create mode 100644 src/components/ActiveConfigurationsTable/index.tsx diff --git a/src/components/ActiveConfigurationsTable/active-configurations-table.scss b/src/components/ActiveConfigurationsTable/active-configurations-table.scss new file mode 100644 index 0000000..23af0f7 --- /dev/null +++ b/src/components/ActiveConfigurationsTable/active-configurations-table.scss @@ -0,0 +1,36 @@ +@import "src/constants/style/styleConst"; + + +.active-configurations-table { + &__title { + margin-bottom: $basic-margin*2; + font-size: 20px; + } + + &__table { + &__name-field { + input { + width: 100%; + } + + display: flex; + justify-content: space-between; + gap: $basic-margin; + + &__actions { + display: flex; + gap: $basic-margin + } + } + } + + .active-config { + tr { + background: $blue-light; + } + + .ant-table-cell-row-hover { + background: $blue-color-2 !important; + } + } +} diff --git a/src/components/ActiveConfigurationsTable/active-configurations-table.type.ts b/src/components/ActiveConfigurationsTable/active-configurations-table.type.ts new file mode 100644 index 0000000..bc6fd95 --- /dev/null +++ b/src/components/ActiveConfigurationsTable/active-configurations-table.type.ts @@ -0,0 +1,8 @@ +import { ConfigType } from '@pages/ModulesPage/module.type.ts' + +export interface ActiveConfigurationsTablePropsType { + isActiveTable?: boolean + data: ConfigType[] + handleShowConfig: (config: ConfigType) => void + handleShowCompareModal: (config: ConfigType) => void +} diff --git a/src/components/ActiveConfigurationsTable/index.tsx b/src/components/ActiveConfigurationsTable/index.tsx new file mode 100644 index 0000000..906cdf5 --- /dev/null +++ b/src/components/ActiveConfigurationsTable/index.tsx @@ -0,0 +1,207 @@ +import { CheckOutlined, CloseOutlined, EditOutlined } from '@ant-design/icons' +import { Button, message, Table, Tag, Tooltip } from 'antd' +import { FC, useEffect, useRef, useState } from 'react' + +import ActiveTableActionButtons from '@widgets/ActiveTableActionButtons' + +import { + ActiveConfigurationsTablePropsType +} from '@components/ActiveConfigurationsTable/active-configurations-table.type.ts' + +import configApi from '@services/configService.ts' + +import { ConfigType } from '@pages/ModulesPage/module.type.ts' +import { ColumnsType } from 'antd/es/table' + +import './active-configurations-table.scss' +import dayjs from 'dayjs' +import CanEdit from '@components/CanEdit' + + +const ActiveConfigurationsTable: FC = + ({ + isActiveTable = false, + handleShowConfig, + handleShowCompareModal, + data + }) => { + const [editKeyConfig, setEditKeyConfig] = useState('') + const [tempEditValues, setTempEditValues] = useState() + const inputRefs = useRef>({}) + const [createUpdateConfig] = configApi.useCreateUpdateConfigMutation() + const [markConfigAsActive] = configApi.useMarkConfigAsActiveMutation() + const [deleteConfig] = configApi.useDeleteConfigMutation() + + + useEffect(() => { + if (inputRefs.current[editKeyConfig]) { + inputRefs.current[editKeyConfig]?.focus() + } + }, [editKeyConfig]) + const handleEditStart = (record: ConfigType) => { + setEditKeyConfig(record.id) + setTempEditValues({ + ...tempEditValues, + [record.id]: record.name + } as ConfigType) + } + + const handleCancelEdit = (record: ConfigType) => { + setEditKeyConfig('') + setTempEditValues({ + ...tempEditValues, + [record.id]: record.name + } as ConfigType) + } + + const handleSaveEdit = (record: ConfigType) => { + const sendData = { ...record, name: tempEditValues[record.id] } + createUpdateConfig(sendData) + .unwrap() + .then(() => { + message.success('Элемент успешно сохранен') + setEditKeyConfig('') + }) + .catch(() => { + message.error('Не удалось сохранить элемент') + }) + } + const handleDeleteConfig = (record: ConfigType) => { + deleteConfig(record.id) + .unwrap() + .then(() => { + message.success('Элемент успешно удален') + setEditKeyConfig('') + }) + .catch(() => { + message.error('Не удалось удалить элемент') + }) + } + + const handleMarkConfigActive = (record: ConfigType) => { + markConfigAsActive(record.id) + .unwrap() + .then(() => { + message.success('Элемент успешно обновлен') + setEditKeyConfig('') + }) + .catch(() => { + message.error('Не удалось обновить элемент') + }) + } + + const renderNameField = (value: string, record: ConfigType) => { + const canEdit = editKeyConfig === record.id + const inputValue = canEdit ? tempEditValues[record.id] : value + + return ( +
+ (inputRefs.current[record.id] = inputRef)} + onChange={(event) => { + const { value } = event.target + setTempEditValues({ ...tempEditValues, [record.id]: value }) + }} + disabled={!canEdit} + value={inputValue} + /> + {canEdit ? ( +
+ +
+ ) : ( + + +
+ ) + } + + const columns: ColumnsType = [ + { + title: 'Наименование', + dataIndex: 'name', + key: 'name', + render: renderNameField, + align: 'center' + }, + { + title: 'Версия', + dataIndex: 'version', + key: 'version', + align: 'center' + }, + { + title: 'Статус конфигурации', + dataIndex: 'valid', + key: 'version', + align: 'center', + render: (value: boolean) => { + if (value) { + return Корректная + } + return Некорректная + } + }, + { + title: 'Дата обновления', + dataIndex: 'updatedAt', + key: 'updatedAt', + align: 'center', + render: (value: string) => { + return dayjs(value).format('HH:mm:ss DD.MM.YYYY') + } + }, + { + title: 'Действия', + dataIndex: 'actions', + key: 'actions', + align: 'center', + render: (_: unknown, record: ConfigType) => ( + + ) + } + ] + const className = `active-configurations-table__table ${isActiveTable ? 'active-config' : ''}` + return ( +
+

+ {isActiveTable ? 'Активная конфигурация' : 'Другие конфигурации'} +

+ record.id} + pagination={false} + columns={columns} + dataSource={data} + /> + + ) + } + +export default ActiveConfigurationsTable