Skip to content

Commit

Permalink
feat: Add link to opendata.swiss
Browse files Browse the repository at this point in the history
fix #135
  • Loading branch information
ptbrowne committed May 31, 2022
1 parent d73cd94 commit cc7149d
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 4 deletions.
44 changes: 41 additions & 3 deletions app/components/chart-footnotes.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Trans } from "@lingui/macro";
import { Box, Button, Typography } from "@mui/material";
import { useEffect, useState } from "react";
import { Box, Button, Link, Typography } from "@mui/material";
import { useEffect, useState, useMemo } from "react";

import { useChartTablePreview } from "@/components/chart-table-preview";
import { DataDownloadMenu, RunSparqlQuery } from "@/components/data-download";
import { ChartConfig } from "@/configurator";
import {
DataCubeMetadataWithComponentValuesQuery,
useDataCubeMetadataWithComponentValuesQuery,
useDataCubeObservationsQuery,
} from "@/graphql/query-hooks";
Expand All @@ -14,6 +15,23 @@ import { useLocale } from "@/locales/use-locale";

import { useQueryFilters } from "../charts/shared/chart-helpers";

const makeOpenDataLink = (
lang: string,
cube: DataCubeMetadataWithComponentValuesQuery["dataCubeByIri"]
) => {
const identifier = cube.identifier;
const creatorIri = cube.creator?.iri;
if (!identifier || !creatorIri) {
return;
}
return `https://opendata.swiss/${lang}/perma/${encodeURIComponent(
`${identifier}@${creatorIri.replace(
"https://register.ld.admin.ch/opendataswiss/org/",
""
)}`
)}`;
};

export const ChartFootnotes = ({
dataSetIri,
chartConfig,
Expand Down Expand Up @@ -47,13 +65,33 @@ export const ChartFootnotes = ({
const sparqlEditorUrl =
visibleData?.dataCubeByIri?.observations.sparqlEditorUrl;

const cubeLink = useMemo(() => {
if (data?.dataCubeByIri) {
return makeOpenDataLink(locale, data?.dataCubeByIri);
} else {
return undefined;
}
}, [locale, data?.dataCubeByIri]);

if (data?.dataCubeByIri) {
const { dataCubeByIri } = data;

return (
<Box sx={{ mt: 2 }}>
<Typography component="div" variant="caption" color="grey.600">
<Trans id="metadata.dataset">Dataset</Trans>: {dataCubeByIri.title}
<Trans id="metadata.dataset">Dataset</Trans>:{" "}
{cubeLink ? (
<Link target="_blank" href={cubeLink} rel="noreferrer">
{dataCubeByIri.title}{" "}
<Icon
name="linkExternal"
size={12}
style={{ display: "inline" }}
/>
</Link>
) : (
dataCubeByIri.title
)}
</Typography>

<Typography component="div" variant="caption" color="grey.600">
Expand Down
4 changes: 4 additions & 0 deletions app/graphql/queries/data-cubes.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ query DataCubeMetadataWithComponentValues(
iri
title
publisher
identifier
creator {
iri
}
dimensions {
...dimensionMetaData
}
Expand Down
7 changes: 6 additions & 1 deletion app/graphql/query-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type Scalars = {
export type DataCube = {
__typename: 'DataCube';
iri: Scalars['String'];
identifier?: Maybe<Scalars['String']>;
title: Scalars['String'];
version?: Maybe<Scalars['String']>;
contactName?: Maybe<Scalars['String']>;
Expand Down Expand Up @@ -407,7 +408,7 @@ export type DataCubeMetadataWithComponentValuesQueryVariables = Exact<{
}>;


export type DataCubeMetadataWithComponentValuesQuery = { __typename: 'Query', dataCubeByIri?: Maybe<{ __typename: 'DataCube', iri: string, title: string, publisher?: Maybe<string>, dimensions: Array<(
export type DataCubeMetadataWithComponentValuesQuery = { __typename: 'Query', dataCubeByIri?: Maybe<{ __typename: 'DataCube', iri: string, title: string, publisher?: Maybe<string>, identifier?: Maybe<string>, creator?: Maybe<{ __typename: 'DataCubeOrganization', iri: string }>, dimensions: Array<(
{ __typename: 'GeoCoordinatesDimension' }
& DimensionMetaData_GeoCoordinatesDimension_Fragment
) | (
Expand Down Expand Up @@ -686,6 +687,10 @@ export const DataCubeMetadataWithComponentValuesDocument = gql`
iri
title
publisher
identifier
creator {
iri
}
dimensions {
...dimensionMetaData
}
Expand Down
2 changes: 2 additions & 0 deletions app/graphql/resolver-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type Scalars = {
export type DataCube = {
__typename?: 'DataCube';
iri: Scalars['String'];
identifier?: Maybe<Scalars['String']>;
title: Scalars['String'];
version?: Maybe<Scalars['String']>;
contactName?: Maybe<Scalars['String']>;
Expand Down Expand Up @@ -462,6 +463,7 @@ export type ResolversParentTypes = ResolversObject<{

export type DataCubeResolvers<ContextType = any, ParentType extends ResolversParentTypes['DataCube'] = ResolversParentTypes['DataCube']> = ResolversObject<{
iri?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
identifier?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
title?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
version?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
contactName?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
Expand Down
1 change: 1 addition & 0 deletions app/graphql/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ const DataCube: DataCubeResolvers = {
iri: ({ data: { iri } }) => iri,
title: ({ data: { title } }) => title,
version: ({ data: { version } }) => version ?? null,
identifier: ({ data: { identifier } }) => identifier ?? null,
publisher: ({ data: { publisher } }) => publisher ?? null,
contactName: ({ data: { contactPoint } }) => contactPoint?.name ?? null,
contactEmail: ({ data: { contactPoint } }) => contactPoint?.email ?? null,
Expand Down
1 change: 1 addition & 0 deletions app/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ enum DataCubePublicationStatus {

type DataCube {
iri: String!
identifier: String
title: String!
version: String
contactName: String
Expand Down

0 comments on commit cc7149d

Please sign in to comment.