2021-05-28 12:04:38 +05:00
|
|
|
|
import {Button, Table, Select} from 'antd';
|
2021-05-27 12:53:42 +05:00
|
|
|
|
import {MessageService} from '../services/api'
|
|
|
|
|
import {useState, useEffect} from 'react'
|
|
|
|
|
import {useParams} from 'react-router-dom'
|
|
|
|
|
import {Subscribe} from '../services/signalr'
|
2021-05-19 16:05:01 +05:00
|
|
|
|
import Loader from '../components/Loader'
|
2021-05-28 12:04:38 +05:00
|
|
|
|
import moment from 'moment'
|
2021-05-19 16:05:01 +05:00
|
|
|
|
import '../styles/message.css'
|
2021-05-27 12:53:42 +05:00
|
|
|
|
import Notification from '../components/Notification'
|
2021-05-19 16:05:01 +05:00
|
|
|
|
|
2021-05-28 12:04:38 +05:00
|
|
|
|
const {Option} = Select
|
|
|
|
|
|
2021-05-19 16:05:01 +05:00
|
|
|
|
// Словарь категорий для строк таблицы
|
|
|
|
|
const categoryDictionary = {
|
2021-05-27 12:53:42 +05:00
|
|
|
|
1: {title: 'Авария'},
|
|
|
|
|
2: {title: 'Предупреждение'},
|
|
|
|
|
3: {title: 'Информация'},
|
2021-05-19 16:05:01 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Данные для таблицы
|
2021-05-27 12:53:42 +05:00
|
|
|
|
export default function Messages() {
|
|
|
|
|
let {id} = useParams()
|
2021-05-19 16:05:01 +05:00
|
|
|
|
const [messages, setMessages] = useState([])
|
2021-05-31 09:34:22 +05:00
|
|
|
|
const [filteredInfo, setFilteredInfo] = useState({})
|
2021-05-19 16:05:01 +05:00
|
|
|
|
const [loader] = useState(false)
|
2021-05-28 12:04:38 +05:00
|
|
|
|
const filterOptions = [
|
2021-05-31 09:34:22 +05:00
|
|
|
|
{label: 'Авария', value: '1'},
|
|
|
|
|
{label: 'Предупреждение', value: '2'},
|
|
|
|
|
{label: 'Информация', value: '3'},
|
2021-05-28 12:04:38 +05:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
const children = filterOptions.map((line) => (<Option key={line.value}>{line.label}</Option>))
|
|
|
|
|
|
2021-05-31 09:34:22 +05:00
|
|
|
|
const handleChange = (filters) => {
|
|
|
|
|
setFilteredInfo({
|
|
|
|
|
filteredInfo: filters
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-19 16:05:01 +05:00
|
|
|
|
const handleReceiveMessages = (messages) => {
|
|
|
|
|
if (messages) {
|
|
|
|
|
setMessages(messages.items)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-05-28 12:04:38 +05:00
|
|
|
|
MessageService.getMessage(id)
|
|
|
|
|
.then(handleReceiveMessages)
|
|
|
|
|
.catch((ex) => {
|
|
|
|
|
Notification(`Не удалось загрузить сообщения по скважине "${id}"`, 'error')
|
|
|
|
|
console.log(ex)
|
|
|
|
|
})
|
2021-05-27 12:53:42 +05:00
|
|
|
|
|
2021-05-19 16:05:01 +05:00
|
|
|
|
let unSubscribeMessagesHub = Subscribe('ReceiveMessages', `well_${id}`, handleReceiveMessages)
|
|
|
|
|
return () => {
|
|
|
|
|
unSubscribeMessagesHub()
|
|
|
|
|
}
|
2021-05-28 12:04:38 +05:00
|
|
|
|
}, [id])
|
2021-05-19 16:05:01 +05:00
|
|
|
|
|
2021-05-31 09:34:22 +05:00
|
|
|
|
const columns = [
|
|
|
|
|
{
|
|
|
|
|
title: 'Дата',
|
|
|
|
|
key: 'date',
|
|
|
|
|
dataIndex: 'date',
|
|
|
|
|
render: (item) => moment(item).format('DD MMM YYYY, HH:MM:ss'),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: 'Категория',
|
|
|
|
|
key: 'categoryId',
|
|
|
|
|
dataIndex: 'categoryId',
|
|
|
|
|
render: (_, item) => categoryDictionary[item.categoryId].title,
|
|
|
|
|
style: (_, item) => categoryDictionary[item.categoryId].style,
|
|
|
|
|
filters: [
|
|
|
|
|
{text: 'Авария', value: '1'},
|
|
|
|
|
{text: 'Предупреждение', value: '2'},
|
|
|
|
|
{text: 'Информация', value: '3'},
|
|
|
|
|
],
|
|
|
|
|
filteredValue: filteredInfo.categoryId,
|
|
|
|
|
onFilter: (value, record) => record.categoryId.toString() === value,
|
|
|
|
|
ellipsis: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: 'Сообщение',
|
|
|
|
|
key: 'message',
|
|
|
|
|
dataIndex: 'message',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: 'Пользователь',
|
|
|
|
|
key: 'user',
|
|
|
|
|
dataIndex: 'user',
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2021-05-19 16:05:01 +05:00
|
|
|
|
return (
|
2021-05-27 12:53:42 +05:00
|
|
|
|
<>
|
|
|
|
|
<h2>Сообщения</h2>
|
|
|
|
|
<hr/>
|
2021-05-28 12:04:38 +05:00
|
|
|
|
<h3>Фильтр сообщений</h3>
|
|
|
|
|
<Select
|
|
|
|
|
mode="multiple"
|
|
|
|
|
allowClear
|
|
|
|
|
placeholder="Фильтр сообщений"
|
|
|
|
|
className="filter-selector"
|
2021-05-31 09:34:22 +05:00
|
|
|
|
onChange={handleChange}
|
2021-05-28 12:04:38 +05:00
|
|
|
|
>
|
|
|
|
|
{children}
|
|
|
|
|
</Select>
|
|
|
|
|
<Button
|
|
|
|
|
type="primary"
|
|
|
|
|
className="submit-button"
|
|
|
|
|
>
|
|
|
|
|
Применть
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
className="submit-button"
|
|
|
|
|
>
|
|
|
|
|
Обновить
|
|
|
|
|
</Button>
|
2021-05-27 12:53:42 +05:00
|
|
|
|
<Table
|
|
|
|
|
columns={columns}
|
|
|
|
|
dataSource={messages}
|
|
|
|
|
rowClassName={(record) => `event_message_${record.categoryId} event_message`}
|
|
|
|
|
size={'small'}
|
2021-05-28 12:04:38 +05:00
|
|
|
|
pagination={{pageSize: 26}}
|
2021-05-27 12:53:42 +05:00
|
|
|
|
rowKey={(record) => record.id}
|
|
|
|
|
/>
|
|
|
|
|
{loader && <Loader/>}
|
|
|
|
|
</>
|
2021-05-19 16:05:01 +05:00
|
|
|
|
)
|
2021-05-28 12:04:38 +05:00
|
|
|
|
}
|