2021-08-13 14:46:22 +05:00
|
|
|
|
import { Dispatch, SetStateAction } from "react"
|
|
|
|
|
import notify from "./notify"
|
|
|
|
|
|
2021-07-30 16:13:26 +05:00
|
|
|
|
export const RegExpIsFloat = /^[-+]?\d+\.?\d?$/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
other - объект с дополнительными свойствами колонки
|
|
|
|
|
поддерживаются все базовые свойства из описания https://ant.design/components/table/#Column
|
|
|
|
|
плю дополнительные для колонок EditableTable:
|
|
|
|
|
editable - редактируемая колонка, bool
|
2021-08-12 15:43:03 +05:00
|
|
|
|
input - react компонента редактора (<Input/>, <InputNumber/>, <DatePicker/>...)
|
2021-07-30 16:13:26 +05:00
|
|
|
|
isRequired - значение может быть пустым,
|
|
|
|
|
formItemClass - css класс для <FormItem/>, если требуется
|
|
|
|
|
formItemRules - массив правил валидации значений https://ant.design/components/form/#Rule,
|
2021-08-12 15:43:03 +05:00
|
|
|
|
initialValue - дефолтное значение при добавлении новой строки
|
2021-07-30 16:13:26 +05:00
|
|
|
|
*/
|
2021-07-29 11:38:09 +05:00
|
|
|
|
export const makeColumn = (title:string, key:string, other?:any) => ({
|
2021-07-29 11:22:25 +05:00
|
|
|
|
title: title,
|
|
|
|
|
key: key,
|
|
|
|
|
dataIndex: key,
|
|
|
|
|
...other,
|
2021-07-29 15:15:09 +05:00
|
|
|
|
})
|
2021-07-29 11:22:25 +05:00
|
|
|
|
|
2021-07-30 16:13:26 +05:00
|
|
|
|
export const makeColumnsPlanFact = (title:string, key:string|string[], columsOther?:any|any[], gruopOther?:any) =>
|
2021-07-29 11:22:25 +05:00
|
|
|
|
{
|
2021-07-30 15:13:15 +05:00
|
|
|
|
let keyPlanLocal = key
|
|
|
|
|
let keyFactLocal = key
|
2021-07-29 11:22:25 +05:00
|
|
|
|
|
2021-07-30 15:13:15 +05:00
|
|
|
|
if(key instanceof Array){
|
|
|
|
|
keyPlanLocal = key[0]
|
|
|
|
|
keyFactLocal = key[1]
|
|
|
|
|
}else{
|
|
|
|
|
keyPlanLocal = key + 'Plan'
|
|
|
|
|
keyFactLocal = key + 'Fact'
|
2021-07-29 11:22:25 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 15:13:15 +05:00
|
|
|
|
let columsOtherLoacl :any[2]
|
|
|
|
|
if(columsOther instanceof Array)
|
|
|
|
|
columsOtherLoacl = [columsOther[0], columsOther[1]]
|
|
|
|
|
else
|
|
|
|
|
columsOtherLoacl = [columsOther, columsOther]
|
|
|
|
|
|
2021-07-29 11:22:25 +05:00
|
|
|
|
return {
|
|
|
|
|
title: title,
|
2021-07-30 15:13:15 +05:00
|
|
|
|
...gruopOther,
|
|
|
|
|
children: [
|
|
|
|
|
makeColumn('план', keyPlanLocal, columsOtherLoacl[0]),
|
|
|
|
|
makeColumn('факт', keyFactLocal, columsOtherLoacl[1]),
|
2021-07-29 11:22:25 +05:00
|
|
|
|
]
|
|
|
|
|
}
|
2021-08-12 15:43:03 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-13 14:46:22 +05:00
|
|
|
|
type PaginationContainer = {
|
|
|
|
|
skip?: number;
|
|
|
|
|
take?: number;
|
|
|
|
|
count?: number;
|
|
|
|
|
items?: any[] | null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type asyncFunction = (...args:any) => Promise<any|void>;
|
|
|
|
|
|
|
|
|
|
export const makePaginationObject = (paginationContainer:PaginationContainer, ...other:any) => {
|
|
|
|
|
let page = 1 + Math.floor((paginationContainer.skip??0) /(paginationContainer.take??1));
|
|
|
|
|
return {
|
|
|
|
|
...other,
|
|
|
|
|
pageSize: paginationContainer.take,
|
|
|
|
|
total: paginationContainer.count ?? paginationContainer.items?.length ?? 0,
|
|
|
|
|
current: page,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const updateFromWebApiWrapperAsync = async (funcAsync: asyncFunction, setShowLoader: Dispatch<SetStateAction<boolean>>, errorNotifyText: string) => {
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|