CF2-49: Changed 'Measures' table crud buttons

This commit is contained in:
KharchenkoVV 2021-10-07 17:23:01 +05:00
parent 11650fea46
commit 11bb5f4c18
3 changed files with 88 additions and 69 deletions

View File

@ -1,7 +1,12 @@
import { useState, useEffect } from 'react' import { useState, useEffect } from 'react'
import { Button, Form, Input, Timeline } from 'antd' import { Button, Form, Input, Popconfirm, Timeline } from 'antd'
import moment from 'moment' import moment from 'moment'
import { CheckSquareOutlined } from '@ant-design/icons' import { CheckSquareOutlined,
EditOutlined,
SaveOutlined,
PlusOutlined,
CloseCircleOutlined,
DeleteOutlined } from '@ant-design/icons'
import { View } from './View' import { View } from './View'
import LoaderPortal from '../../components/LoaderPortal' import LoaderPortal from '../../components/LoaderPortal'
import { MeasureService } from '../../services/api' import { MeasureService } from '../../services/api'
@ -13,7 +18,7 @@ const format='YYYY.MM.DD HH:mm'
export const MeasureTable = ({idWell, idCategory, title, columns, values, updateMeasuresFunc}) => { export const MeasureTable = ({idWell, idCategory, title, columns, values, updateMeasuresFunc}) => {
const [showLoader, setShowLoader] = useState(false); const [showLoader, setShowLoader] = useState(false);
const [selectedTimeLineId, setSelectedTimeLineId] = useState(0) const [selectedTimeLineId, setSelectedTimeLineId] = useState(undefined)
const [displayedValues, setDisplayedValues] = useState([]); const [displayedValues, setDisplayedValues] = useState([]);
const [editingColumns, setEditingColumns] = useState(columns); const [editingColumns, setEditingColumns] = useState(columns);
const [isTableEditing, setIsTableEditing] = useState(false); const [isTableEditing, setIsTableEditing] = useState(false);
@ -31,13 +36,12 @@ export const MeasureTable = ({idWell, idCategory, title, columns, values, update
useEffect(() => { useEffect(() => {
let valuesToDisplay = [] let valuesToDisplay = []
if(selectedTimeLineId === 0) { if(!selectedTimeLineId)
valuesToDisplay = values.length valuesToDisplay = values && values.length
? values[values.length-1] ? values[values.length-1]
: [] : []
} else { else
valuesToDisplay = values.find(el => el.id === selectedTimeLineId) valuesToDisplay = values.find(el => el.id === selectedTimeLineId)
}
setDisplayedValues(valuesToDisplay) setDisplayedValues(valuesToDisplay)
}, [selectedTimeLineId, values]) }, [selectedTimeLineId, values])
@ -45,77 +49,85 @@ export const MeasureTable = ({idWell, idCategory, title, columns, values, update
useEffect(() => { useEffect(() => {
let switchableColumns = [] let switchableColumns = []
isTableEditing && editingActionName !== 'Удалить' isTableEditing && editingActionName !== 'delete'
? switchableColumns = createEditingColumns(columns, () => <Input className='w-100' />) ? switchableColumns = createEditingColumns(columns, () => <Input className='w-100 measure-input' />)
: switchableColumns = createEditingColumns(columns, null) : switchableColumns = createEditingColumns(columns, null)
if(editingActionName === 'Редактировать') if(editingActionName === 'edit')
measuresForm.setFieldsValue(displayedValues.data); measuresForm.setFieldsValue(displayedValues.data);
else if(editingActionName === 'Добавить') else if(editingActionName === 'add')
measuresForm.resetFields() measuresForm.resetFields()
setEditingColumns(switchableColumns) setEditingColumns(switchableColumns)
}, [isTableEditing, columns, editingActionName, displayedValues.data, measuresForm]) }, [isTableEditing, columns, editingActionName, displayedValues?.data, measuresForm])
const buttonsConfig = [ const markMeasuresAsDeleted = async () => {
{name:'Добавить', key:'add'}, setShowLoader(true)
{name:'Редактировать', key:'edit'}, await MeasureService.markAsDelete(idWell, displayedValues.id)
{name:'Удалить', key:'delete'} updateMeasuresFunc()
] setShowLoader(false)
setIsTableEditing(false)
const createButtons = (config, onClickDelegate) => {
return (
<div className='w-300px mt-12px'>
{config.map(conf => {
return (
<Button
key={conf.key}
className='w-100'
onClick={onClickDelegate}
disabled={conf.key !== 'add' && displayedValues.isDefaultData
? true
: false}
>
{conf.name}
</Button>
)
})}
</div>
)
} }
const crudButtons = createButtons(buttonsConfig, (e) => { const checkIsDataDefault = () =>
setEditingActionName(e.target.innerText) displayedValues?.isDefaultData ? true : false
setIsTableEditing(true)
}) const crudButtons =
<div className='w-300px mt-8px'>
<Button
key='add'
className='w-33'
onClick={() => {
setEditingActionName('add')
setIsTableEditing(true)
}}
>
<PlusOutlined />
</Button>
<Button
key='edit'
className='w-33'
onClick={() => {
setEditingActionName('edit')
setIsTableEditing(true)
}}
disabled={checkIsDataDefault()}
>
<EditOutlined />
</Button>
<Popconfirm
title="Удалить данные?"
onConfirm={() => markMeasuresAsDeleted()}
disabled={checkIsDataDefault()}
>
<Button
key='delete'
onClick={() => {
setEditingActionName('delete')
}}
disabled={checkIsDataDefault()}
>
<DeleteOutlined style={{margin:'auto 28px'}}/>
</Button>
</Popconfirm>
</div>
const confirmButtons = const confirmButtons =
<div className='w-300px'> <div className='w-300px mt-8px'>
<h3 style={{textAlign: 'center'}}>{editingActionName} данные?</h3> <div className='d-flex'>
<div className='d-flex mt-20px'>
<Button <Button
key='confirm' key='confirm'
className='w-100' className='w-50'
onClick={async () => { onClick={() => { measuresForm.submit() }}
if(editingActionName === 'Удалить'){
setShowLoader(true)
await MeasureService.markAsDelete(idWell, displayedValues.id)
updateMeasuresFunc()
setShowLoader(false)
setIsTableEditing(false)
} else {
measuresForm.submit()
}
}}
> >
Да <SaveOutlined />
</Button> </Button>
<Button <Button
key='decline' key='decline'
className='w-100' className='w-50'
onClick={()=> setIsTableEditing(false)} onClick={()=> setIsTableEditing(false)}
> >
Нет <CloseCircleOutlined />
</Button> </Button>
</div> </div>
</div> </div>
@ -132,9 +144,9 @@ export const MeasureTable = ({idWell, idCategory, title, columns, values, update
setShowLoader(true) setShowLoader(true)
if(editingActionName === 'Добавить') { if(editingActionName === 'add') {
await MeasureService.insert(idWell, measureParams) await MeasureService.insert(idWell, measureParams)
} else if (editingActionName === 'Редактировать') { } else if (editingActionName === 'edit') {
measureParams.id = displayedValues.id measureParams.id = displayedValues.id
measureParams.timestamp = displayedValues.timestamp measureParams.timestamp = displayedValues.timestamp
await MeasureService.update(idWell, measureParams) await MeasureService.update(idWell, measureParams)
@ -147,11 +159,11 @@ export const MeasureTable = ({idWell, idCategory, title, columns, values, update
return <> return <>
&nbsp; &nbsp;
<h3>{title}</h3> <h2>{title}</h2>
&nbsp; &nbsp;
<div className='d-flex'> <div className='d-flex'>
<div className='flex-direction-column'> <div className='flex-direction-column'>
<div className='d-flex button-container'> <div className='measure-buttons-container'>
{isTableEditing {isTableEditing
? confirmButtons ? confirmButtons
: crudButtons : crudButtons
@ -184,7 +196,7 @@ export const MeasureTable = ({idWell, idCategory, title, columns, values, update
onFinish={handleSubmitMeasuresForm} onFinish={handleSubmitMeasuresForm}
> >
<View <View
item={displayedValues.data} item={displayedValues?.data ?? []}
columns={editingColumns} columns={editingColumns}
/> />
</Form> </Form>

View File

@ -27,6 +27,14 @@ body {
width: 15% width: 15%
} }
.w-33 {
width: 33%
}
.w-50 {
width: 50%
}
.w-100 { .w-100 {
width: 100% width: 100%
} }

View File

@ -1,4 +1,4 @@
input { input.measure-input {
font-weight:bold; font-weight:bold;
} }
@ -6,13 +6,12 @@ input {
width: 300px; width: 300px;
} }
.button-container { .measure-buttons-container {
height: 100px; height: 30px;
align-items: center;
} }
.measure-dates { .measure-dates {
height: calc(100% - 130px); height: 160px;
overflow-y: scroll; overflow-y: scroll;
} }