MSP-3173 add SessionsPage

This commit is contained in:
Anastasiia committed 2024-04-19 14:32:41 +03:00
1 parent ddf4388250
commit 1916bac98b
1 file changed
+172
+172
View File
@@ -0,0 +1,172 @@
import { LogoutOutlined } from '@ant-design/icons'
import { Button, Popconfirm, Spin, Table, message, Pagination } from 'antd'
import { ColumnsType } from 'antd/es/table'
import { useEffect, useState } from 'react'
import { useNavigate } from 'react-router-dom'
import { SessionStatusKeysType, sessionStatuses } from '@constants/statuses'
import { getUserFullName } from '@utils/userUtils/getFullNameUtil'
import useRole from '@hooks/useRole'
import sessionServiceApi from '@services/sessionService'
import userServiceApi from '@services/userService'
import { routePaths } from '@routes/routePaths.ts'
import { PermissionKeysType } from '@type/roles.type.ts'
import { SessionType } from '@type/session.type'
import './sessions-page.scss'
const limit = 10
const SessionsPage = () => {
const [offset, setOffset] = useState<number>(0)
const { data, isLoading: isLoadingSession } =
sessionServiceApi.useGetAllSessionsQuery({ limit, offset })
const [revokeSession] = sessionServiceApi.useRevokeSessionMutation()
const { data: allUsers = [], isLoading: isUsersLoading } =
userServiceApi.useGetAllUsersQuery()
const { role, hasPermission } = useRole()
const navigate = useNavigate()
const [currentPage, setCurrentPage] = useState(1)
const isLoading = isUsersLoading || isLoadingSession
const isPageAvailable = hasPermission(PermissionKeysType.read)
const hasRevokePermission = hasPermission(PermissionKeysType.write)
useEffect(() => {
if (!isPageAvailable) {
navigate(routePaths.home)
}
}, [isPageAvailable])
useEffect(() => {
if (!role) {
navigate(routePaths.error)
}
}, [role])
const handleChangePagination = (page: number) => {
setCurrentPage(page)
const currentOffset = (page - 1) * limit
setOffset(currentOffset)
}
const handleRevokeSessions = (id: number) => {
revokeSession(id)
.unwrap()
.then(() => message.success('Сессия успешно завершена'))
.catch(() => message.info('Не удалось завершить сессию'))
}
if (isLoading || !data) {
return <Spin />
}
const columns: ColumnsType<SessionType> = [
{
title: 'Пользователь',
dataIndex: 'userId',
key: 'userId',
render: (value) => {
const user = allUsers.find((el) => el.id === value)
return user && getUserFullName(user)
}
},
{
title: 'Идентификатор',
dataIndex: 'id',
key: 'id'
},
{
title: 'Дата начала сессии',
dataIndex: 'createdAt',
key: 'createdAt',
render: (value) => {
const date = new Date(value)
return `${date.toLocaleDateString()} ${date.toLocaleTimeString()}`
}
},
{
title: 'Дата окончания сессии',
dataIndex: 'expiredAt',
key: 'expiredAt',
render: (value) => {
const date = new Date(value)
return `${date.toLocaleDateString()} ${date.toLocaleTimeString()}`
}
},
{
title: 'Статус',
dataIndex: 'status',
key: 'status',
render: (value: SessionStatusKeysType) => sessionStatuses[value]
},
{
dataIndex: 'actions',
key: 'actions',
render: (_, record) => {
if (record.status !== SessionStatusKeysType.allowed) {
return null
}
return (
<>
{hasRevokePermission && (
<Popconfirm
title="Закончить эту сессию?"
onConfirm={() => handleRevokeSessions(record.id)}
>
<Button
data-cy="sessions-page__content__table__logout-btn"
type="primary"
className="sessions-page__content__table__logout-btn"
icon={<LogoutOutlined />}
/>
</Popconfirm>
)}
</>
)
}
}
]
const { totalCount, items } = data
return (
<section className="sessions-page">
<header className="sessions-page__header">
<span
data-cy="sessions-page__header__title"
className="sessions-page__header__title"
>
Пользовательские сессии
</span>
</header>
<section className="sessions-page__content">
<div className="sessions-page__content__wrap-table">
<Table
className="sessions-page__content__table"
rowKey={(record) => record.id}
pagination={false}
dataSource={items}
columns={columns}
/>
</div>
<div className="sessions-page__content__pagination">
<Pagination
showSizeChanger={false}
current={currentPage}
total={totalCount}
pageSize={limit}
onChange={handleChangePagination}
/>
</div>
</section>
</section>
)
}
export default SessionsPage