MSP-3174 add SecurityLogPage

This commit is contained in:
Anastasiia committed 2024-04-19 14:32:41 +03:00
1 parent 97d878fafb
commit bdb2805844
2 files changed
+162

No files matched your search

+126
View File
@@ -0,0 +1,126 @@
import { Pagination, Spin, Table } from 'antd'
import { ColumnsType } from 'antd/es/table'
import { useEffect, useState } from 'react'
import { useNavigate } from 'react-router-dom'
import { getUserFullName } from '@utils/userUtils/getFullNameUtil'
import useRole from '@hooks/useRole'
import securityLogServiceApi from '@services/securityLogService'
import userServiceApi from '@services/userService'
import { routePaths } from '@routes/routePaths.ts'
import { LogType } from '@type/log.type'
import { PermissionKeysType } from '@type/roles.type'
import './security-log-page.scss'
const limit = 10
const SecurityLogPage = () => {
const [getAllLogs, { data, isLoading: isLoadingLogs }] =
securityLogServiceApi.useGetAllLogsMutation()
const { data: allUsers = [], isLoading: isUsersLoading } =
userServiceApi.useGetAllUsersQuery()
const { role, hasPermission } = useRole()
const navigate = useNavigate()
const [currentPage, setCurrentPage] = useState(1)
const isLoading = isUsersLoading || isLoadingLogs
const isPageAvailable = hasPermission(PermissionKeysType.read)
useEffect(() => {
getAllLogs({ limit, offset: 0 })
}, [])
useEffect(() => {
if (!isPageAvailable) {
navigate(routePaths.home)
}
}, [isPageAvailable])
useEffect(() => {
if (!role) {
navigate(routePaths.error)
}
}, [role])
const handleChangePagination = (page: number) => {
setCurrentPage(page)
getAllLogs({ limit, offset: (page - 1) * limit })
}
if (isLoading || !data) {
return <Spin />
}
const columns: ColumnsType<LogType> = [
{
title: 'Пользователь',
dataIndex: 'userId',
key: 'userId',
render: (value) => {
const user = allUsers.find((el) => el.id === value)
if (user) {
return getUserFullName(user)
}
}
},
{
title: 'Время события',
dataIndex: 'createdAt',
key: 'createdAt',
render: (value) => {
const date = new Date(value)
return `${date.toLocaleDateString()} ${date.toLocaleTimeString()}`
}
},
{
title: 'Действие',
dataIndex: 'message',
key: 'message',
render: (value: string) =>
value.split('\n').map((line) => <p key={line}>{line}</p>)
}
]
const { totalCount, items } = data
return (
<section className="security-log-page">
<header className="security-log-page__header">
<span
data-cy="security-log-page__header__title"
className="security-log-page__header__title"
>
Просмотр журналов ИБ
</span>
</header>
<section className="security-log-page__content">
<div className="security-log-page__content__wrap-table">
<Table
rowKey={(record) => record.id}
pagination={false}
dataSource={items}
columns={columns}
/>
</div>
<div className="security-log-page__content__pagination">
<Pagination
showSizeChanger={false}
current={currentPage}
total={totalCount}
pageSize={limit}
onChange={handleChangePagination}
/>
</div>
</section>
</section>
)
}
export default SecurityLogPage
@@ -0,0 +1,36 @@
@import "src/constants/style/styleConst";
.security-log-page {
height: 100%;
background: #fff;
border: 1px solid $border-color;
&__header {
border-bottom: 1px solid $border-color;
padding: 0 3 * $basic-padding;
&__title {
padding: 3 * $basic-padding 0;
display: inline-block;
font-weight: 500;
font-size: 20px;
line-height: 28px;
}
}
&__content {
padding: 2 * $basic-padding 3 * $basic-padding;
&__wrap-table {
overflow-x: scroll;
height: calc(100vh - 200px);
}
&__pagination {
margin-top: 20px;
display: flex;
justify-content: flex-end;
}
}
}