Skip to content

Instantly share code, notes, and snippets.

@williamtsoi1
Created July 7, 2023 03:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save williamtsoi1/a45a99b0e0e69e73922e35ebe767d7f0 to your computer and use it in GitHub Desktop.
Save williamtsoi1/a45a99b0e0e69e73922e35ebe767d7f0 to your computer and use it in GitHub Desktop.
Show how filters for a Looker embedded dashboard can be retrieved and used outside the immediate react component
import React, { useEffect, useRef } from "react";
import { LookerEmbedSDK } from "@looker/embed-sdk";
const LookerEmbedDashboard = ({callback}) => {
const [dashboardEmbedded, setDashboardEmbedded] = React.useState(false);
const dashboardRef = useRef(false);
useEffect(() => {
if (dashboardRef.current) return;
dashboardRef.current = true;
makeDashboard();
}, []);
let makeDashboard = async () => {
// LookerEmbedSDK.init(...) is already run at the page level
LookerEmbedSDK.createDashboardWithId(1)
.appendTo(document.getElementById("dashboard"))
.withClassName("container-fluid")
.withClassName("p-0")
.withClassName("h-100")
.on("dashboard:run:start", (e) => {
console.debug("Dashboard started");
callback(e.dashboard.dashboard_filters);
})
.on("dashboard:filters:changed", (e) => {
console.debug("Filters changed", e.dashboard.dashboard_filters);
})
.on("dashboard:run:complete", (e) => {
console.debug("Dashboard complete");
})
.build()
.connect()
.then((dashboard) => {
setDashboardEmbedded(true);
console.debug("LookerEmbedSDK.createDashboardWithId()::Connected!");
})
.catch((error) => {
console.error("An unexpected error occurred", error);
});
};
return (
<>
<div id="dashboard" className="h-100"></div>
</>
);
};
export default LookerEmbedDashboard;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment