asb_cloud_front/src/pages/WellOperations/WellOperationsEditor.jsx

110 lines
4.2 KiB
React
Raw Normal View History

2021-08-18 18:01:46 +05:00
import { useState, useEffect } from 'react'
2021-08-20 12:31:24 +05:00
import { Input } from 'antd'
2021-08-19 15:07:24 +05:00
import moment from 'moment'
2021-08-20 12:31:24 +05:00
import { EditableTable, DatePickerWrapper, SelectFromDictionary, numericColumnOptions, makeColumn } from "../../components/Table"
2021-08-19 17:51:56 +05:00
import LoaderPortal from '../../components/LoaderPortal'
import { invokeWebApiWrapperAsync } from '../../components/factory'
2021-08-19 17:51:56 +05:00
import { WellOperationService} from '../../services/api'
2021-08-20 12:31:24 +05:00
import { dictionarySectionType, getByKeyOrReturnKey } from './dictionary'
2021-08-19 15:07:24 +05:00
const { TextArea } = Input;
const basePageSize = 160;
const format='YYYY.MM.DD HH:mm'
2021-08-18 18:01:46 +05:00
2021-08-19 17:51:56 +05:00
export const WellOperationsEditor = ({idWell, idType}) => {
2021-08-18 18:01:46 +05:00
const [pageNumAndPageSize, setPageNumAndPageSize] = useState({current:1, pageSize:basePageSize})
const [paginationTotal, setPaginationTotal] = useState(0)
2021-08-20 12:31:24 +05:00
const [dictionaryOperationCategory, setDictionaryOperationCategory] = useState(new Map())
2021-08-18 18:01:46 +05:00
const [operations, setOperations] = useState([])
const [showLoader, setShowLoader] = useState(false)
2021-08-19 15:07:24 +05:00
useEffect(() => invokeWebApiWrapperAsync(
async () => {
2021-08-20 12:31:24 +05:00
const categories = await WellOperationService.getCategories(idWell)
const dictCategories = new Map()
categories?.forEach((item) => dictCategories.set(item.id, item.name))
setDictionaryOperationCategory(dictCategories)
2021-08-19 15:07:24 +05:00
}),[idWell])
2021-08-18 18:01:46 +05:00
const updateOperations = () => invokeWebApiWrapperAsync(
async () => {
const skip = ((pageNumAndPageSize.current - 1) * pageNumAndPageSize.pageSize) || 0
const take = pageNumAndPageSize.pageSize
2021-08-19 15:07:24 +05:00
const paginatedOperations = await WellOperationService.getOperations(idWell, idType, undefined, undefined, undefined, undefined, skip, take )
const operations = paginatedOperations?.items ?? []
setOperations(operations)
2021-08-18 18:01:46 +05:00
const total = paginatedOperations.count?? paginatedOperations.items?.length ?? 0
setPaginationTotal(total)
},
setShowLoader,
'Не удалось загрузить список операций по скважине'
)
2021-08-19 15:07:24 +05:00
useEffect(updateOperations, [idWell, idType, pageNumAndPageSize])
2021-08-18 18:01:46 +05:00
2021-08-19 15:07:24 +05:00
const columns = [
makeColumn('Конструкция секции','idWellSectionType', {
editable:true,
2021-08-20 12:31:24 +05:00
input:<SelectFromDictionary dictionary={dictionarySectionType}/>,
width:110,
render:(_, record)=>getByKeyOrReturnKey(dictionarySectionType, record.idWellSectionType)
2021-08-19 15:07:24 +05:00
}),
makeColumn('Операция','idCategory', {
editable:true,
2021-08-20 12:31:24 +05:00
input:<SelectFromDictionary dictionary={dictionaryOperationCategory}/>,
2021-08-19 15:07:24 +05:00
width:200,
2021-08-20 12:31:24 +05:00
render:(_, record)=>getByKeyOrReturnKey(dictionaryOperationCategory, record.idCategory)
2021-08-19 15:07:24 +05:00
}),
makeColumn('Доп. инфо','categoryInfo', {editable:true, width:300, input:<TextArea/>}),
makeColumn('Глубина забоя','wellDepth', numericColumnOptions),
2021-08-19 15:07:24 +05:00
makeColumn('Время начала','startDate', {
editable:true,
width:200,
input:<DatePickerWrapper/>,
render:(_, record) => moment.utc(record.startDate).local().format(format)
}),
makeColumn('Часы','durationHours', numericColumnOptions),
2021-08-19 15:07:24 +05:00
makeColumn('Комментарий','comment', {editable:true, input:<TextArea/>}),
]
2021-08-18 18:01:46 +05:00
2021-08-19 15:07:24 +05:00
const onAdd = async (operation) => {
operation.idType = idType
await WellOperationService.insertRange(idWell, [operation])
updateOperations()
2021-08-18 18:01:46 +05:00
}
2021-08-19 15:07:24 +05:00
const onEdit= async (operation) => {
if(!operation.id)
return
operation.idType = idType
await WellOperationService.update(idWell, operation.id, operation)
updateOperations()
}
2021-08-18 18:01:46 +05:00
2021-08-19 15:07:24 +05:00
const onDelete= async (operation) => {
if(!operation.id)
return
await WellOperationService.delete(idWell, operation.id)
updateOperations()
2021-08-18 18:01:46 +05:00
}
return <LoaderPortal show={showLoader}>
<EditableTable
2021-08-19 15:07:24 +05:00
size='small'
2021-08-19 17:55:08 +05:00
bordered
2021-08-18 18:01:46 +05:00
columns={columns}
dataSource={operations}
onRowAdd={onAdd}
onRowEdit={onEdit}
onRowDelete={onDelete}
pagination={{
current: pageNumAndPageSize.current,
pageSize: pageNumAndPageSize.pageSize,
showSizeChanger: false,
total: paginationTotal,
onChange: (page, pageSize) => setPageNumAndPageSize({current: page, pageSize: pageSize})
}}
/>
</LoaderPortal>
}