Hi, ui sdk question: How can I get a dateDataset’s...
# gooddata-ui
e
Hi, ui sdk question: How can I get a dateDataset’s id string or ref when the only thing I have is one of it’s corresponding displayform refs? Context: I have the
IInsight
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.
d
Hi Evan, this is currently a bit more involved, you need some extra data to determine the dateDataset for a given display form. I’ve put together a small example that hopefully does what you want (see the individual comments for more explanation):
Copy code
import 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!
e
thanks, i’ll take a crack at it and let you know if I have any questions!
👍 1
j
Is this the only way to get a dateDataset currently?
d
Currently, the catalog API is the only way of getting Date Datasets we provide in the GoodData.UI SDK (there is currently no API in the GoodData.UI to get a single Date Dataset for example). For some less complex use cases, you can also use the Export Catalog feature to make referencing the Date Datasets, Attributes and other items easier (especially in situations when you know ahead of time which item you want to use where). If these two options are not ergonomic for your use case, please let us know in our GoodData.UI Productboard