Evan Shen
01/25/2022, 12:40 AMIInsight
data returned from <InsightView>’s onInsightLoaded
and was able to find that displayform in the attributes bucket, but I need to create a date filter and newAbsoluteDateFilter
requires the dateDataset id/ref, unlike newPositiveAttributeFilter
or newMeasureFilter
which accept displayFormIds and localIds that I can get out of the buckets data.Dan Homola
01/25/2022, 12:37 PMimport React, { useEffect, useMemo, useState } from "react";
import { areObjRefsEqual, newAbsoluteDateFilter } from "@gooddata/sdk-model";
import { InsightView } from "@gooddata/sdk-ui-ext";
import { IAttribute, insightAttributes } from "@gooddata/sdk-model";
import { useBackendStrict, useWorkspaceStrict } from "@gooddata/sdk-ui";
import { ICatalogDateDataset } from "@gooddata/sdk-backend-spi";
export const DateDatasetExample = () => {
const backend = useBackendStrict();
const workspace = useWorkspaceStrict();
const [dateAttribute, setDateAttribute] = useState<IAttribute | undefined>();
const [dateDataset, setDateDataset] = useState<ICatalogDateDataset | undefined>();
const filter = useMemo(() => {
return dateDataset
? newAbsoluteDateFilter(dateDataset.dataSet.ref, "2015-01-01", "2022-01-01")
: undefined;
}, [dateDataset]);
useEffect(() => {
const body = async () => {
if (!dateAttribute) {
return;
}
// load the catalog via the Catalog API, only for the dateDataset objects
const catalog = await backend.workspace(workspace).catalog().forTypes(["dateDataset"]).load();
// find the date dataset that contains the date attribute
const matchingDateDataset = catalog.dateDatasets().find((dataset) =>
dataset.dateAttributes.some((candidateAttribute) =>
// find the attribute that has our target display form as its default display form
areObjRefsEqual(
candidateAttribute.defaultDisplayForm,
dateAttribute.attribute.displayForm,
),
),
);
setDateDataset(matchingDateDataset);
};
body();
}, [dateAttribute]);
return (
<>
<pre>{filter ? JSON.stringify(filter, null, 2) : "Loading..."}</pre>
<div style={{ height: 400 }}>
<InsightView
insight="<insight-identifier>"
onInsightLoaded={(insight) => {
const attributes = insightAttributes(insight);
// or some more complex logic to find the relevant attribute
const dateAttribute = attributes[0];
setDateAttribute(dateAttribute);
}}
/>
</div>
</>
);
};
I hope this helps!Evan Shen
01/25/2022, 3:45 PMJustin Unverricht
01/27/2022, 10:16 PMDan Homola
01/28/2022, 9:17 AM