From 828b2e550a5aadc70c5cc1bc27478832e88ed404 Mon Sep 17 00:00:00 2001 From: rozetko Date: Tue, 27 Dec 2022 19:16:20 +0300 Subject: [PATCH] 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 + : +
+
+ + + + + } ); }