import LoaderPortal from '../components/LoaderPortal' import { useState, useEffect } from "react"; import {makeColumn, makeColumnsPlanFact, RegExpIsFloat} from '../components/factory' import {WellSectionService} from '../services/api' import notify from '../components/notify' import { EditableTable } from '../components/EditableTable'; import { Input } from 'antd' const TypeSelector = const DataListSectionTypes = const numericColumnOptions = { editable: true, initialValue: 0, formItemRules: [ { required: true, message: `Введите число`, pattern: RegExpIsFloat, }, ], }; const columns = [ makeColumn('Конструкция секции', 'sectionType', {editable:true, input:TypeSelector}), makeColumnsPlanFact('Глубина, м', 'wellDepth', numericColumnOptions), makeColumnsPlanFact('Период, д', 'buildDays', numericColumnOptions), makeColumnsPlanFact('Механическая скорость проходки, м/час', 'rateOfPenetration', numericColumnOptions), makeColumnsPlanFact('Рейсовая скорость, м/час', 'routeSpeed', numericColumnOptions), makeColumnsPlanFact('Скорость подъема КНБК', 'bhaUpSpeed', numericColumnOptions), makeColumnsPlanFact('Скорость спуска КНБК', 'bhaDownSpeed', numericColumnOptions), makeColumnsPlanFact('Скорость спуска обсадной колонны', 'casingDownSpeed', numericColumnOptions), ] const runAsyncFunc = async (funcAsync, setShowLoader, errorNotifyText) => { if(setShowLoader) setShowLoader(true) try{ await funcAsync() } catch (ex) { if(process.env.NODE_ENV === 'development') console.log(ex) if(errorNotifyText) notify(errorNotifyText, 'error') } finally{ if(setShowLoader) setShowLoader(false) } } export default function WellStat({idWell}){ const [showLoader, setShowLoader] = useState(false) const [items, setItems] = useState([]) const addKeysAndUpdateStateData = (items) =>{ const keyedItems = items?.map(item => ({...item, key:item.id}))??[] setItems(keyedItems) } useEffect(() => { runAsyncFunc( async () => { const paginationContainer = await WellSectionService.getAll(idWell, 0, 1024) addKeysAndUpdateStateData(paginationContainer.items) }, setShowLoader, `Не удалось загрузить секции по скважине "${idWell}"`) } ,[idWell]) const onAdd = (item) => { runAsyncFunc( async () => { const updatedItems = await WellSectionService.insert(idWell, [item]) const newItems = [...items, ...updatedItems] addKeysAndUpdateStateData(newItems) }, setShowLoader, `Не удалось добавить секцию в скважину "${idWell}"`) } const onEdit = (item) => { runAsyncFunc( async () => { const updatedItem = await WellSectionService.update(idWell, item.id, item) const newItems = [...items] const index = newItems.findIndex((i) => item.key === i.key) newItems.splice(index, 1, updatedItem) addKeysAndUpdateStateData(newItems) }, setShowLoader, `Не удалось изменить секцию в скважине "${idWell}"`) } const onDelete = (item) =>{ runAsyncFunc( async () => { await WellSectionService.delete(idWell, [item.id]) const newItems = [...items] const index = newItems.findIndex((i) => item.key === i.key) newItems.splice(index, 1) addKeysAndUpdateStateData(newItems) }, setShowLoader, `Не удалось удалить секцию из скважины "${idWell}"`) } return( {DataListSectionTypes} ) }