From ec9ee8acba5355c02868890ba1b2f0bfb255f6fd Mon Sep 17 00:00:00 2001 From: Om Bhardwaj <115864495+ombhardwajj@users.noreply.github.com> Date: Sun, 15 Sep 2024 23:53:24 +0530 Subject: [PATCH] avniproject#1337 | Added a switch to enable metabase setup , a button to check sync status and a button to refresh tables --- src/reports/SelfServiceReports.js | 227 ++++++++++++++++++++++-------- 1 file changed, 168 insertions(+), 59 deletions(-) diff --git a/src/reports/SelfServiceReports.js b/src/reports/SelfServiceReports.js index ac13f0122..c190a7384 100644 --- a/src/reports/SelfServiceReports.js +++ b/src/reports/SelfServiceReports.js @@ -1,7 +1,6 @@ -import React from "react"; +import React, { useState, useEffect } from "react"; import ScreenWithAppBar from "../common/components/ScreenWithAppBar"; import { reportSideBarOptions } from "./Common"; - import Card from "@material-ui/core/Card"; import CardContent from "@material-ui/core/CardContent"; import Typography from "@material-ui/core/Typography"; @@ -11,81 +10,191 @@ import { makeStyles } from "@material-ui/core"; import Grid from "@material-ui/core/Grid"; import MetabaseSVG from "./Metabase_icon.svg"; import Button from "@material-ui/core/Button"; +import Switch from "@material-ui/core/Switch"; +import CircularProgress from "@material-ui/core/CircularProgress"; const useStyles = makeStyles({ root: { - maxWidth: 500, + maxWidth: 600, backgroundColor: "#FFF" }, - details: { - display: "flex" + topRightButtons: { + display: "flex", + justifyContent: "flex-end", + gap: "10px", + marginTop: "10px" + }, + actionAreaContainer: { + display: "flex", + justifyContent: "space-between", + alignItems: "center" }, - cover: { - width: 151 + buttonSpacing: { + marginLeft: "10px" }, - content: { - flex: "1 0 auto" + message: { + color: "green", + marginTop: "10px" + }, + errorMessage: { + color: "red", + marginTop: "10px" } }); const SelfServiceReports = () => { const classes = useStyles(); + const [setupEnabled, setSetupEnabled] = useState(false); + const [loadingSetup, setLoadingSetup] = useState(false); + const [loadingRefresh, setLoadingRefresh] = useState(false); + const [syncStatus, setSyncStatus] = useState(""); + const [errorMessage, setErrorMessage] = useState(""); + + useEffect(() => { + if (errorMessage) { + const timer = setTimeout(() => { + setErrorMessage(""); + }, 10000); + return () => clearTimeout(timer); + } + }, [errorMessage]); + + const handleSetupToggle = async () => { + setLoadingSetup(true); + try { + const response = await fetch("/api/metabase/setup-toggle?enabled=" + !setupEnabled, { + method: "POST" + }); + const data = await response.json(); + if (response.ok) { + setSetupEnabled(!setupEnabled); + setErrorMessage(data.message.includes("could not be created") ? data.message : ""); + } else { + console.error("Failed to toggle setup."); + } + } catch (error) { + console.error("Error during setup toggle:", error); + } + setLoadingSetup(false); + }; + + const handleRefreshTables = async () => { + setLoadingRefresh(true); + try { + const response = await fetch("/api/metabase/create-questions", { + method: "POST" + }); + if (!response.ok) { + console.error("Failed to refresh tables."); + setErrorMessage("Failed to refresh tables. Please try again."); + } + } catch (error) { + console.error("Error during refresh:", error); + setErrorMessage("Failed to refresh tables. Please try again."); + } + setLoadingRefresh(false); + }; + + const handleCheckSyncStatus = async () => { + try { + const response = await fetch("/api/metabase/sync-status", { + method: "GET" + }); + if (response.ok) { + const status = await response.json(); + setSyncStatus(status); + } else { + setSyncStatus("Error retrieving sync status"); + } + } catch (error) { + console.error("Error fetching sync status:", error); + setSyncStatus("Error fetching sync status"); + } + }; + return ( - + - + - - - - - - {"Metabase - - - - Metabase - - - Metabase provides a graphical interface to create business intelligence and - analytics graphs in minutes. Avni integrates with Metabase to support ad hoc - and self serviced reports. - - + + + + + {"Metabase + + + + Metabase + + + Metabase provides a graphical interface to create business intelligence and analytics graphs in minutes. Avni + integrates with Metabase to support ad hoc and self serviced reports. + - - - - - + + + + +
+ + {loadingSetup && } + + +
+ +
+ + +
+ + {errorMessage && ( + + {errorMessage} + + )} + + {syncStatus && ( + + Sync Status: {syncStatus} + + )}