[dataset page][xl]: refactor to work with Apollo.
This commit is contained in:
@@ -1,4 +1,39 @@
|
|||||||
export default function About({ datapackage }) {
|
import ErrorMessage from '../Error';
|
||||||
|
import { NetworkStatus } from 'apollo-client';
|
||||||
|
import { useQuery } from '@apollo/react-hooks';
|
||||||
|
import gql from 'graphql-tag';
|
||||||
|
|
||||||
|
export const GET_DATAPACKAGE_QUERY = gql`
|
||||||
|
query dataset($id: String!) {
|
||||||
|
dataset(id: $id) {
|
||||||
|
result {
|
||||||
|
name
|
||||||
|
metadata_created
|
||||||
|
metadata_modified
|
||||||
|
resources {
|
||||||
|
name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
export default function About({ variables }) {
|
||||||
|
const { loading, error, data, fetchMore, networkStatus } = useQuery(
|
||||||
|
GET_DATAPACKAGE_QUERY,
|
||||||
|
{
|
||||||
|
variables,
|
||||||
|
// Setting this value to true will make the component rerender when
|
||||||
|
// the "networkStatus" changes, so we are able to know if it is fetching
|
||||||
|
// more data
|
||||||
|
notifyOnNetworkStatusChange: true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (error) return <ErrorMessage message="Error loading dataset." />;
|
||||||
|
if (loading) return <div>Loading</div>;
|
||||||
|
|
||||||
|
const { result } = data.dataset;
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<table className="table-auto w-full text-sm text-left my-6">
|
<table className="table-auto w-full text-sm text-left my-6">
|
||||||
@@ -15,11 +50,11 @@ export default function About({ datapackage }) {
|
|||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr>
|
<tr>
|
||||||
<td className="px-4 py-2">{datapackage.resources.length}</td>
|
<td className="px-4 py-2">{result.resources.length}</td>
|
||||||
<td className="px-4 py-2">{datapackage.size || 'NA'}</td>
|
<td className="px-4 py-2">{result.size || 'NA'}</td>
|
||||||
<td className="px-4 py-2"></td>
|
<td className="px-4 py-2"></td>
|
||||||
<td className="px-4 py-2">{datapackage.created}</td>
|
<td className="px-4 py-2">{result.metadata_created}</td>
|
||||||
<td className="px-4 py-2">{datapackage.modified}</td>
|
<td className="px-4 py-2">{result.metadata_modified}</td>
|
||||||
<td className="px-4 py-2"></td>
|
<td className="px-4 py-2"></td>
|
||||||
<td className="px-4 py-2"></td>
|
<td className="px-4 py-2"></td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|||||||
@@ -1,20 +1,53 @@
|
|||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
|
import ErrorMessage from '../Error';
|
||||||
|
import { NetworkStatus } from 'apollo-client';
|
||||||
|
import { useQuery } from '@apollo/react-hooks';
|
||||||
|
import gql from 'graphql-tag';
|
||||||
|
|
||||||
export default function Org({ org }) {
|
export const GET_ORG_QUERY = gql`
|
||||||
|
query dataset($id: String!) {
|
||||||
|
dataset(id: $id) {
|
||||||
|
result {
|
||||||
|
organization {
|
||||||
|
name
|
||||||
|
title
|
||||||
|
image_url
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
export default function Org({ variables }) {
|
||||||
|
const { loading, error, data, fetchMore, networkStatus } = useQuery(
|
||||||
|
GET_ORG_QUERY,
|
||||||
|
{
|
||||||
|
variables,
|
||||||
|
// Setting this value to true will make the component rerender when
|
||||||
|
// the "networkStatus" changes, so we are able to know if it is fetching
|
||||||
|
// more data
|
||||||
|
notifyOnNetworkStatusChange: true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (error) return <ErrorMessage message="Error loading dataset." />;
|
||||||
|
if (loading) return <div>Loading</div>;
|
||||||
|
|
||||||
|
const { organization } = data.dataset.result;
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{org ? (
|
{organization ? (
|
||||||
<>
|
<>
|
||||||
<img
|
<img
|
||||||
src={
|
src={
|
||||||
org.image_url ||
|
organization.image_url ||
|
||||||
'https://datahub.io/static/img/datahub-cube-edited.svg'
|
'https://datahub.io/static/img/datahub-cube-edited.svg'
|
||||||
}
|
}
|
||||||
className="h-5 w-5 mr-2 inline-block"
|
className="h-5 w-5 mr-2 inline-block"
|
||||||
/>
|
/>
|
||||||
<Link href={`/@${org.name}`}>
|
<Link href={`/@${organization.name}`}>
|
||||||
<a className="font-semibold text-primary underline">
|
<a className="font-semibold text-primary underline">
|
||||||
{org.title || org.name}
|
{organization.title || organization.name}
|
||||||
</a>
|
</a>
|
||||||
</Link>
|
</Link>
|
||||||
</>
|
</>
|
||||||
|
|||||||
@@ -1,6 +1,43 @@
|
|||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
|
import ErrorMessage from '../Error';
|
||||||
|
import { NetworkStatus } from 'apollo-client';
|
||||||
|
import { useQuery } from '@apollo/react-hooks';
|
||||||
|
import gql from 'graphql-tag';
|
||||||
|
|
||||||
|
export const DEFAULT_DATASET_QUERY = gql`
|
||||||
|
query dataset($id: String!) {
|
||||||
|
dataset(id: $id) {
|
||||||
|
result {
|
||||||
|
name
|
||||||
|
resources {
|
||||||
|
name
|
||||||
|
title
|
||||||
|
format
|
||||||
|
created
|
||||||
|
last_modified
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
export default function Resources({ variables }) {
|
||||||
|
const { loading, error, data, fetchMore, networkStatus } = useQuery(
|
||||||
|
DEFAULT_DATASET_QUERY,
|
||||||
|
{
|
||||||
|
variables,
|
||||||
|
// Setting this value to true will make the component rerender when
|
||||||
|
// the "networkStatus" changes, so we are able to know if it is fetching
|
||||||
|
// more data
|
||||||
|
notifyOnNetworkStatusChange: true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (error) return <ErrorMessage message="Error loading dataset." />;
|
||||||
|
if (loading) return <div>Loading</div>;
|
||||||
|
|
||||||
|
const { result } = data.dataset;
|
||||||
|
|
||||||
export default function Resources({ datapackage }) {
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h3 className="text-xl font-semibold">Data Files</h3>
|
<h3 className="text-xl font-semibold">Data Files</h3>
|
||||||
@@ -15,10 +52,10 @@ export default function Resources({ datapackage }) {
|
|||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{datapackage.resources.map((resource, index) => (
|
{result.resources.map((resource, index) => (
|
||||||
<tr key={index}>
|
<tr key={index}>
|
||||||
<td className="px-4 py-2">
|
<td className="px-4 py-2">
|
||||||
<Link href={`${datapackage.name}/r/${resource.name}`}>
|
<Link href={`${result.name}/r/${resource.name}`}>
|
||||||
<a className="underline">{resource.title || resource.name}</a>
|
<a className="underline">{resource.title || resource.name}</a>
|
||||||
</Link>
|
</Link>
|
||||||
</td>
|
</td>
|
||||||
@@ -26,7 +63,7 @@ export default function Resources({ datapackage }) {
|
|||||||
<td className="px-4 py-2">{resource.created}</td>
|
<td className="px-4 py-2">{resource.created}</td>
|
||||||
<td className="px-4 py-2">{resource.last_modified}</td>
|
<td className="px-4 py-2">{resource.last_modified}</td>
|
||||||
<td className="px-4 py-2">
|
<td className="px-4 py-2">
|
||||||
<Link href={`${datapackage.name}/r/${resource.name}`}>
|
<Link href={`${result.name}/r/${resource.name}`}>
|
||||||
<a className="underline">Preview</a>
|
<a className="underline">Preview</a>
|
||||||
</Link>
|
</Link>
|
||||||
</td>
|
</td>
|
||||||
|
|||||||
@@ -1,39 +1,59 @@
|
|||||||
import { GetServerSideProps } from 'next';
|
import { GetServerSideProps } from 'next';
|
||||||
|
import { useQuery } from '@apollo/react-hooks';
|
||||||
|
import { initializeApollo } from '../../../lib/apolloClient';
|
||||||
import config from '../../../config';
|
import config from '../../../config';
|
||||||
import utils from '../../../utils';
|
import utils from '../../../utils';
|
||||||
import Head from 'next/head';
|
import Head from 'next/head';
|
||||||
import Nav from '../../../components/home/Nav';
|
import Nav from '../../../components/home/Nav';
|
||||||
import About from '../../../components/dataset/About';
|
import About from '../../../components/dataset/About';
|
||||||
import Org from '../../../components/dataset/Org';
|
import Org from '../../../components/dataset/Org';
|
||||||
import Resources from '../../../components/dataset/Resources';
|
import Resources, {
|
||||||
|
DEFAULT_DATASET_QUERY,
|
||||||
|
} from '../../../components/dataset/Resources';
|
||||||
|
|
||||||
|
function Dataset({ variables }) {
|
||||||
|
const {
|
||||||
|
data: {
|
||||||
|
dataset: { result },
|
||||||
|
},
|
||||||
|
} = useQuery(DEFAULT_DATASET_QUERY, { variables });
|
||||||
|
|
||||||
function Dataset({ datapackage }) {
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Head>
|
<Head>
|
||||||
<title>Portal | {datapackage.title || datapackage.name}</title>
|
<title>Portal | {result.title || result.name}</title>
|
||||||
<link rel="icon" href="/favicon.ico" />
|
<link rel="icon" href="/favicon.ico" />
|
||||||
</Head>
|
</Head>
|
||||||
<Nav />
|
<Nav />
|
||||||
<main className="p-6">
|
<main className="p-6">
|
||||||
<h1 className="text-3xl font-semibold text-primary mb-2">
|
<h1 className="text-3xl font-semibold text-primary mb-2">
|
||||||
{datapackage.title || datapackage.name}
|
{result.title || result.name}
|
||||||
</h1>
|
</h1>
|
||||||
<Org org={datapackage.organization} />
|
<Org variables={variables} />
|
||||||
<About datapackage={datapackage} />
|
<About variables={variables} />
|
||||||
<Resources datapackage={datapackage} />
|
<Resources variables={variables} />
|
||||||
</main>
|
</main>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getServerSideProps: GetServerSideProps = async (context) => {
|
export const getServerSideProps: GetServerSideProps = async (context) => {
|
||||||
const res = await fetch(
|
const apolloClient = initializeApollo();
|
||||||
`${config.get('DMS')}/api/3/action/package_show?id=${context.query.dataset}`
|
const variables = {
|
||||||
);
|
id: context.query.dataset,
|
||||||
const ckanResult = (await res.json()).result;
|
};
|
||||||
const datapackage = utils.ckanToDataPackage(ckanResult);
|
|
||||||
return { props: { datapackage } };
|
const apolloResponse = await apolloClient.query({
|
||||||
|
query: DEFAULT_DATASET_QUERY,
|
||||||
|
variables,
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
props: {
|
||||||
|
initialApolloState: apolloClient.cache.extract(),
|
||||||
|
variables,
|
||||||
|
},
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export default Dataset;
|
export default Dataset;
|
||||||
|
|||||||
Reference in New Issue
Block a user