asb_cloud_front/src/pages/Messages.jsx

152 lines
4.5 KiB
React
Raw Normal View History

import { Table, Select, DatePicker, Input } from 'antd'
import { useState, useEffect } from 'react'
2021-08-13 10:30:26 +05:00
import moment from 'moment'
import { MessageService } from '../services/api'
import LoaderPortal from '../components/LoaderPortal'
import { notify } from '../components/factory'
2021-08-13 10:30:26 +05:00
import '../styles/message.css'
const { Option } = Select
const pageSize = 26
const { RangePicker } = DatePicker
const { Search } = Input
2021-05-28 12:04:38 +05:00
// Словарь категорий для строк таблицы
const categoryDictionary = {
1: {title: 'Авария'},
2: {title: 'Предупреждение'},
3: {title: 'Информация'},
}
const columns = [
{
title: 'Дата',
key: 'date',
dataIndex: 'date',
render: item => moment(item).format('DD MMM YYYY, HH:mm:ss'),
}, {
title: 'Глубина',
key: 'wellDepth',
dataIndex: 'wellDepth',
render: depth => <span>{depth.toFixed(2)} м.</span>,
}, {
title: 'Категория',
key: 'categoryId',
dataIndex: 'categoryId',
render: (_, item) => categoryDictionary[item.categoryId].title,
style: (_, item) => categoryDictionary[item.categoryId].style,
ellipsis: true,
}, {
title: 'Сообщение',
key: 'message',
dataIndex: 'message',
}, {
title: 'Пользователь',
key: 'user',
dataIndex: 'user',
},
]
const filterOptions = [
{label: 'Важное', value: 1},
{label: 'Предупреждение', value: 2},
{label: 'Информация', value: 3},
]
// Данные для таблицы
2021-08-13 10:30:26 +05:00
export default function Messages({idWell}) {
const [messages, setMessages] = useState([])
const [pagination, setPagination] = useState(null)
const [page, setPage] = useState(1)
const [range, setRange] = useState([])
const [categories, setCategories] = useState([])
const [searchString, setSearchString] = useState('')
2021-08-18 13:50:44 +05:00
const [showLoader, setShowLoader] = useState(false)
2021-05-28 12:04:38 +05:00
const children = filterOptions.map((line) => <Option key={line.value}>{line.label}</Option>)
const onChangeRange = (range) => setRange(range)
const onChangeSearchString = (message) => setSearchString(message.length > 2 ? message : '')
2021-08-09 16:49:14 +05:00
useEffect(() => {
const GetMessages = async () => {
2021-08-18 13:50:44 +05:00
setShowLoader(true)
try {
const [begin, end] = range?.length > 1 ? [range[0].toISOString(), range[1].toISOString()] : [null, null]
2021-08-13 10:30:26 +05:00
let paginatedMessages = await MessageService.getMessages(idWell,
(page - 1) * pageSize,
pageSize,
categories,
begin,
end,
searchString)
if (paginatedMessages === null){
2021-08-13 10:30:26 +05:00
notify(`Данных по скважине "${idWell}" нет`, 'warning')
2021-08-18 13:50:44 +05:00
setShowLoader(false)
return
}
setMessages(paginatedMessages.items.map(m => ({
key: m.id,
categoryids: categoryDictionary[m.categoryId],
begin: m.date,
...m
})))
setPagination({
total: paginatedMessages.count,
current: Math.floor(paginatedMessages.skip / pageSize),
})
} catch (ex) {
console.log(ex)
2021-08-13 10:30:26 +05:00
notify(`Не удалось загрузить сообщения по скважине "${idWell}"`, 'error')
}
2021-08-18 13:50:44 +05:00
setShowLoader(false)
}
GetMessages()
2021-08-13 10:30:26 +05:00
}, [idWell, page, categories, range, searchString])
return (
<>
<div className='filter-group'>
<h3 className='filter-group-heading'>Фильтр сообщений</h3>
<Select
mode={'multiple'}
allowClear
placeholder={'Фильтр сообщений'}
className={'filter-selector'}
value={categories}
onChange={setCategories}>
{children}
</Select>
<RangePicker
showTime
onChange={onChangeRange}
/>
<Search
className={'filter-selector'}
placeholder={'Фильтр сообщений по тексту'}
onSearch={onChangeSearchString}
/>
</div>
2021-08-18 13:50:44 +05:00
<LoaderPortal show={showLoader}>
<Table
columns={columns}
dataSource={messages}
rowClassName={(record) => `event_message_${record.categoryId} event_message`}
size={'small'}
pagination={{
pageSize: pageSize,
showSizeChanger: false,
total: pagination?.total,
current: page,
onChange: (page) => setPage(page)
}}
rowKey={(record) => record.id}
/>
</LoaderPortal>
</>
)
2021-05-28 12:04:38 +05:00
}