From 60b33503b14b3bf84217246b2b618f84506d4333 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D1=80=D0=BE=D0=BB=D0=BE=D0=B2?= Date: Fri, 13 Aug 2021 14:46:22 +0500 Subject: [PATCH] Add some useful factory functions --- src/components/factory.ts | 46 +++++++++++++++---- .../TelemetryView/ActiveMessagesOnline.jsx | 23 ++++------ src/pages/TelemetryView/index.jsx | 34 +++++--------- src/pages/WellStat.jsx | 26 ++--------- 4 files changed, 62 insertions(+), 67 deletions(-) diff --git a/src/components/factory.ts b/src/components/factory.ts index ecb011a..b6e7aa3 100644 --- a/src/components/factory.ts +++ b/src/components/factory.ts @@ -1,3 +1,6 @@ +import { Dispatch, SetStateAction } from "react" +import notify from "./notify" + export const RegExpIsFloat = /^[-+]?\d+\.?\d?$/ /* @@ -47,12 +50,37 @@ export const makeColumnsPlanFact = (title:string, key:string|string[], columsOth } } -// const makePaginationObject = (paginationContainer, ...other) =>{ -// let page = 1 + Math.floor(paginationContainer.skip/paginationContainer.take); -// return { -// ...other, -// pageSize: paginationContainer.take, -// total: paginationContainer.total, -// current: page, -// } -// } \ No newline at end of file +type PaginationContainer = { + skip?: number; + take?: number; + count?: number; + items?: any[] | null; +} + +type asyncFunction = (...args:any) => Promise; + +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>, 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) + } +} \ No newline at end of file diff --git a/src/pages/TelemetryView/ActiveMessagesOnline.jsx b/src/pages/TelemetryView/ActiveMessagesOnline.jsx index aff20eb..2504a30 100644 --- a/src/pages/TelemetryView/ActiveMessagesOnline.jsx +++ b/src/pages/TelemetryView/ActiveMessagesOnline.jsx @@ -3,7 +3,7 @@ import {Table} from "antd"; import moment from 'moment' import LoaderPortal from '../../components/LoaderPortal' -import notify from "../../components/notify" +import {updateFromWebApiWrapperAsync} from '../../components/factory' import {Subscribe} from '../../services/signalr' import {MessageService} from '../../services/api' @@ -54,21 +54,16 @@ export default function ActiveMessagesOnline({idWell}) { } } - useEffect(() => { - const update = async () => { - setLoader(true) - try { + useEffect(() => { + updateFromWebApiWrapperAsync( + async () => { const messages = await MessageService.getMessages(idWell, 0, 4) handleReceiveMessages(messages) - } - catch (ex) { - notify(`Не удалось загрузить сообщения по скважине "${idWell}"`, 'error') - console.log(ex) - } - setLoader(false) - return Subscribe('hubs/telemetry','ReceiveMessages', `well_${idWell}`, handleReceiveMessages) - } - update() + }, + setLoader, + `Не удалось загрузить сообщения по скважине "${idWell}"`, + ) + return Subscribe('hubs/telemetry','ReceiveMessages', `well_${idWell}`, handleReceiveMessages) }, [idWell]) return ( diff --git a/src/pages/TelemetryView/index.jsx b/src/pages/TelemetryView/index.jsx index 5b6b112..991bfad 100644 --- a/src/pages/TelemetryView/index.jsx +++ b/src/pages/TelemetryView/index.jsx @@ -10,7 +10,7 @@ import {UserOfWells} from './UserOfWells' import LoaderPortal from '../../components/LoaderPortal' import {Subscribe} from '../../services/signalr' import {DataService} from '../../services/api' -import notify from "../../components/notify" +import {updateFromWebApiWrapperAsync} from '../../components/factory' import '../../styles/message.css' @@ -138,7 +138,7 @@ const defaultChartInterval = '600' export default function TelemetryView({idWell}) { const [saubData, setSaubData] = useState([]) const [chartInterval, setChartInterval] = useState(defaultChartInterval) - const [loader, setLoader] = useState(false) + const [showLoader, setShowLoader] = useState(false) const options = timePeriodCollection.map((line) => ) @@ -149,32 +149,20 @@ export default function TelemetryView({idWell}) { } useEffect(() => { - const update = async () => { - setLoader(true) - try { - const data = await DataService.getData(idWell) + updateFromWebApiWrapperAsync( + async () => { + const data = await DataService.getData(idWell, null, chartInterval) handleReceiveDataSaub(data) - } catch (ex) { - notify(`Не удалось получить данные по скважине "${idWell}"`, 'error') - console.log(ex) - } - setLoader(false) - return Subscribe('hubs/telemetry', 'ReceiveDataSaub', `well_${idWell}`, handleReceiveDataSaub) - } - update() - }, [idWell]) - - useEffect(() => { - setLoader(true) - DataService.getData(idWell, null, chartInterval) - .then(handleReceiveDataSaub) - .catch(error => console.error(error)) - .finally(() => setLoader(false)) + }, + setShowLoader, + `Не удалось получить данные по скважине "${idWell}"`, + ) + return Subscribe('hubs/telemetry', 'ReceiveDataSaub', `well_${idWell}`, handleReceiveDataSaub) }, [idWell, chartInterval]) const colSpan = 24 / (paramsGroups.length) - return ( + return ( diff --git a/src/pages/WellStat.jsx b/src/pages/WellStat.jsx index 3558691..cfa5a32 100644 --- a/src/pages/WellStat.jsx +++ b/src/pages/WellStat.jsx @@ -2,7 +2,7 @@ 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 {updateFromWebApiWrapperAsync} from '../components/factory' import { EditableTable } from '../components/EditableTable'; import { Input } from 'antd' @@ -40,22 +40,6 @@ const columns = [ 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([]) @@ -66,7 +50,7 @@ export default function WellStat({idWell}){ } useEffect(() => { - runAsyncFunc( + updateFromWebApiWrapperAsync( async () => { const paginationContainer = await WellSectionService.getAll(idWell, 0, 1024) addKeysAndUpdateStateData(paginationContainer.items) @@ -76,7 +60,7 @@ export default function WellStat({idWell}){ } ,[idWell]) const onAdd = (item) => { - runAsyncFunc( + updateFromWebApiWrapperAsync( async () => { const updatedItems = await WellSectionService.insert(idWell, [item]) const newItems = [...items, ...updatedItems] @@ -87,7 +71,7 @@ export default function WellStat({idWell}){ } const onEdit = (item) => { - runAsyncFunc( + updateFromWebApiWrapperAsync( async () => { const updatedItem = await WellSectionService.update(idWell, item.id, item) const newItems = [...items] @@ -100,7 +84,7 @@ export default function WellStat({idWell}){ } const onDelete = (item) =>{ - runAsyncFunc( + updateFromWebApiWrapperAsync( async () => { await WellSectionService.delete(idWell, [item.id]) const newItems = [...items]