From 308f6386abbff739f4b04b7b3423459985147121 Mon Sep 17 00:00:00 2001 From: vargburz Date: Fri, 20 Jan 2023 13:41:25 +0300 Subject: [PATCH] fix error status chanhing --- .../components/Panel.tsx | 39 ++++++++----------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/src/panels/corpglory-dataexporter-panel/components/Panel.tsx b/src/panels/corpglory-dataexporter-panel/components/Panel.tsx index fea7ce7..614586f 100644 --- a/src/panels/corpglory-dataexporter-panel/components/Panel.tsx +++ b/src/panels/corpglory-dataexporter-panel/components/Panel.tsx @@ -59,14 +59,19 @@ export function Panel({ width, height, timeRange, eventBus, timeZone }: Props) { const [selectedTimeRange, setTimeRange] = useState(timeRange); const [panelStatus, setPanelStatus] = useState(PanelStatus.LOADING); - const [isStatusError, setStatusError] = useState(false); + function setPanelStatusWithValidate(status: PanelStatus): void { + if (panelStatus === PanelStatus.DATASOURCE_ERROR || panelStatus === PanelStatus.PERMISSION_ERROR) { + return; + } + return setPanelStatus(status); + } + const [errorMessage, setErrorMessage] = useState(null); const timeZoneName = convertTimeZoneTypeToName(timeZone); if (contextSrv.user.orgRole !== OrgRole.Admin) { - // TODO: it shouldn't be overriten - setPanelStatus(PanelStatus.PERMISSION_ERROR); + setPanelStatusWithValidate(PanelStatus.PERMISSION_ERROR); } useEffect(() => { @@ -123,17 +128,7 @@ export function Panel({ width, height, timeRange, eventBus, timeZone }: Props) { setTasksDataFrame(dataFrame); }, [tasks]); // eslint-disable-line react-hooks/exhaustive-deps - useEffect(() => { - if (isStatusError) { - return; - } - setPanelStatus(panelStatus); - if (panelStatus === PanelStatus.DATASOURCE_ERROR || panelStatus === PanelStatus.PERMISSION_ERROR) { - setStatusError(true); - } - }, [panelStatus]); // eslint-disable-line react-hooks/exhaustive-deps - - useEffect(refresh, []); // eslint-disable-line react-hooks/exhaustive-deps + useEffect(fetchTasks, []); // eslint-disable-line react-hooks/exhaustive-deps useEffect(() => { if (queries === null) { @@ -142,26 +137,26 @@ export function Panel({ width, height, timeRange, eventBus, timeZone }: Props) { setQueriesDataFrame(getDataFrameForQueriesTable(queries)); }, [queries]); // eslint-disable-line react-hooks/exhaustive-deps - function refresh(): void { + function fetchTasks(): void { getTasks() .then((tasks) => { setTasks(tasks); - setPanelStatus(PanelStatus.OK); + setPanelStatusWithValidate(PanelStatus.OK); for (let task of tasks) { if (task.progress?.status === ExportStatus.EXPORTING) { - setTimeout(refresh, 1000); + setTimeout(fetchTasks, 1000); return; } } }) .catch((err) => { - setPanelStatus(PanelStatus.DATASOURCE_ERROR); + setPanelStatusWithValidate(PanelStatus.DATASOURCE_ERROR); console.error('some error', err); setErrorMessage(`${err.name}: ${err.message}`); }); } - eventBus.subscribe(RefreshEvent, refresh); + eventBus.subscribe(RefreshEvent, fetchTasks); function getDatasourceByUid(uid?: string): DataSourceSettings | undefined { if (_.isNil(uid)) { @@ -204,7 +199,7 @@ export function Panel({ width, height, timeRange, eventBus, timeZone }: Props) { }, }); - refresh(); + fetchTasks(); onCloseModal(); unselectAllQueries(); @@ -431,8 +426,8 @@ export function Panel({ width, height, timeRange, eventBus, timeZone }: Props) { ); const permissionErrorDiv = (
-

Permission Error.

-
DataExporter panel availabel only for Admins
+

Permission Error.

+
DataExporter panel available only for Admins.
); const mainDiv = (