From 828b2e550a5aadc70c5cc1bc27478832e88ed404 Mon Sep 17 00:00:00 2001 From: rozetko Date: Tue, 27 Dec 2022 19:16:20 +0300 Subject: [PATCH 1/2] panel: display tasks from the api --- .../components/Panel.tsx | 64 ++++++++++++++----- 1 file changed, 47 insertions(+), 17 deletions(-) diff --git a/src/panels/corpglory-dataexporter-panel/components/Panel.tsx b/src/panels/corpglory-dataexporter-panel/components/Panel.tsx index 8ebaa20..0663e08 100644 --- a/src/panels/corpglory-dataexporter-panel/components/Panel.tsx +++ b/src/panels/corpglory-dataexporter-panel/components/Panel.tsx @@ -1,7 +1,8 @@ import { PanelOptions, TableRowConfig } from '../types'; -import { Table, Button, HorizontalGroup } from '@grafana/ui'; +import { makeRequest } from 'services/network_service'; +import { Table, Button, HorizontalGroup, LoadingPlaceholder } from '@grafana/ui'; import { PanelProps, toDataFrame, @@ -12,36 +13,65 @@ import { DataLinkClickEvent, } from '@grafana/data'; -import React, { useState } from 'react'; +import React, { useState, useEffect } from 'react'; import * as _ from 'lodash'; interface Props extends PanelProps {} export function Panel({ options, data, width, height, timeRange, onChangeTimeRange }: Props) { console.log('panel', data); - const configs: TableRowConfig[] = []; - const [tasks, setTasks] = useState(configs); - const dataFrame = getDataFrameForTable(tasks, setTasks); + + const [tasks, setTasks] = useState(null); + const [dataFrame, setDataFrame] = useState(null); + + useEffect(() => { + if(tasks === null) { + return; + } + const dataFrame = getDataFrameForTable(tasks, setTasks); + setDataFrame(dataFrame) + }, [tasks]); + + useEffect(() => { + // TODO: move this function to Network Service + async function getTasks(): Promise { + return makeRequest('/tasks', {}); + } + + setTimeout(() => { + + getTasks().then(tasks => setTasks(tasks)).catch(err => console.error(err)); + }, 2000); + }, []); function onAddTaskClick(): void { + if(tasks === null) { + return; + } const configs = [...tasks, createConfigItem()]; setTasks(configs); } return (
- - - - + {dataFrame === null ? + // TODO: if datasource responds with error, display the error + : +
+
+ + + + + } ); } From d5da3d32c858d0331a8d26d3f9150e174d20699f Mon Sep 17 00:00:00 2001 From: rozetko Date: Tue, 27 Dec 2022 19:21:14 +0300 Subject: [PATCH 2/2] rm timeout --- .../corpglory-dataexporter-panel/components/Panel.tsx | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/panels/corpglory-dataexporter-panel/components/Panel.tsx b/src/panels/corpglory-dataexporter-panel/components/Panel.tsx index 0663e08..5ebbd74 100644 --- a/src/panels/corpglory-dataexporter-panel/components/Panel.tsx +++ b/src/panels/corpglory-dataexporter-panel/components/Panel.tsx @@ -38,10 +38,7 @@ export function Panel({ options, data, width, height, timeRange, onChangeTimeRan return makeRequest('/tasks', {}); } - setTimeout(() => { - - getTasks().then(tasks => setTasks(tasks)).catch(err => console.error(err)); - }, 2000); + getTasks().then(tasks => setTasks(tasks)).catch(err => console.error(err)); }, []); function onAddTaskClick(): void { @@ -54,7 +51,7 @@ export function Panel({ options, data, width, height, timeRange, onChangeTimeRan return (
- {dataFrame === null ? + {dataFrame === null ? // TODO: if datasource responds with error, display the error :