[catalog/resource page][l]: implemented resource page with core Table component.

This commit is contained in:
anuveyatsu 2021-08-25 13:31:14 +06:00
parent f2629cb195
commit 9543cdf998
5 changed files with 128 additions and 16 deletions

View File

@ -1,8 +1,8 @@
/* eslint-disable react/display-name */
import { useQuery } from '@apollo/react-hooks';
import * as timeago from 'timeago.js';
import { ErrorMessage } from '../_shared';
import { GET_DATASET_QUERY } from '../../graphql/queries';
import { ResourceInfo } from 'portal';
const About: React.FC<{ variables: any }> = ({ variables }) => {
const { loading, error, data } = useQuery(GET_DATASET_QUERY, {
@ -21,7 +21,58 @@ const About: React.FC<{ variables: any }> = ({ variables }) => {
(item) => item.name === variables.resource
);
return <ResourceInfo resources={[resource]} />;
const stats = [
{ name: 'File', stat: resource.title || resource.name },
{ name: 'Description', stat: resource.description || 'N/A' },
{ name: 'Size', stat: resource.size || 'N/A' },
{
name: 'Created',
stat: resource.created && timeago.format(resource.created),
},
{
name: 'Updated',
stat: resource.updated && timeago.format(resource.updated),
},
{ name: 'Download', stat: resource.path, link: true },
];
return (
<>
<div className="pb-5 border-b border-gray-200">
<h1 className="text-3xl leading-6 font-medium text-gray-900">
{result.title || result.name}
</h1>
</div>
<div className="mb-5">
<dl className="mt-5 grid grid-cols-1 gap-5 sm:grid-cols-3">
{stats.map((item) => (
<div
key={item.name}
className="px-4 py-5 bg-white shadow rounded-lg overflow-hidden sm:p-6"
>
<dt className="text-sm font-medium text-gray-500 truncate">
{item.name}
</dt>
<dd className="mt-1 text-3xl font-semibold text-gray-900">
{item.link ? (
<a
href={item.stat}
className="underline"
rel="noreferrer"
target="_blank"
>
{resource.format || 'Click'}
</a>
) : (
item.stat
)}
</dd>
</div>
))}
</dl>
</div>
</>
);
};
export default About;

View File

@ -0,0 +1,32 @@
import { useQuery } from '@apollo/react-hooks';
import { PlotlyChart, Table } from 'portal';
import { ErrorMessage } from '../_shared';
import { GET_DATASTORE_DATA } from '../../graphql/queries';
const Preview: React.FC<{ view: any }> = ({ view }) => {
const variables = {
resource_id: view.resources,
};
const { loading, error, data } = useQuery(GET_DATASTORE_DATA, {
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.datastore;
// Assuming for now it is always a table view
const columns = result.fields.map((field) => ({
field: field.id,
headerName: field.id,
}));
return <Table columns={columns} data={result.records} />;
};
export default Preview;

View File

@ -1,9 +1,10 @@
import { useQuery } from '@apollo/react-hooks';
import Preview from './Preview';
import { ErrorMessage } from '../_shared';
import { GET_DATASET_QUERY } from '../../graphql/queries';
import { GET_RESOURCE_VIEWS } from '../../graphql/queries';
const DataExplorer: React.FC<{ variables: any }> = ({ variables }) => {
const { loading, error, data } = useQuery(GET_DATASET_QUERY, {
const View: React.FC<{ variables: any }> = ({ variables }) => {
const { loading, error, data } = useQuery(GET_RESOURCE_VIEWS, {
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
@ -14,12 +15,10 @@ const DataExplorer: React.FC<{ variables: any }> = ({ variables }) => {
if (error) return <ErrorMessage message="Error loading dataset." />;
if (loading) return <div>Loading</div>;
const { result } = data.dataset;
const resource = result.resources.find(
(item) => item.name === variables.resource
);
const { result } = data.views;
const previews = result.map((view) => <Preview view={view} key={view.id} />);
return <>{JSON.stringify(resource)}</>;
return <>{previews}</>;
};
export default DataExplorer;
export default View;

View File

@ -1,5 +1,35 @@
import gql from 'graphql-tag';
export const GET_RESOURCE_VIEWS = gql`
query views($id: String) {
views(id: $id) @rest(type: "Response", path: "resource_view_list?{args}") {
result @type(name: "View") {
id
title
description
resources: resource_id
viewType: view_type
series
group
type: graph_type
}
}
}
`;
export const GET_DATASTORE_DATA = gql`
query datastore($resource_id: String) {
datastore(resource_id: $resource_id)
@rest(type: "Response", path: "datastore_search?{args}") {
result {
count: total
fields
records
}
}
}
`;
export const GET_ORG_QUERY = gql`
query org($id: String) {
org(id: $id) @rest(type: "Response", path: "organization_show?{args}") {
@ -27,6 +57,7 @@ export const GET_DATASET_QUERY = gql`
created: metadata_created
updated: metadata_modified
resources {
id
name
title
description

View File

@ -4,7 +4,8 @@ import Head from 'next/head';
import { initializeApollo } from '../../../../../lib/apolloClient';
import Nav from '../../../../../components/home/Nav';
import About from '../../../../../components/resource/About';
import DataExplorer from '../../../../../components/resource/DataExplorer';
import View from '../../../../../components/resource/View';
import Footer from '../../../../../components/home/Footer';
import { GET_DATASET_QUERY } from '../../../../../graphql/queries';
const Resource: React.FC<{ variables: any }> = ({ variables }) => {
@ -24,11 +25,9 @@ const Resource: React.FC<{ variables: any }> = ({ variables }) => {
</Head>
<Nav />
<main className="p-6">
<h1 className="text-3xl font-semibold text-primary mb-2">
{resource.title || resource.name}
</h1>
<About variables={variables} />
<DataExplorer variables={variables} />
<View variables={{ id: resource.id }} />
<Footer />
</main>
</>
);