From e11133c32e4ba7edb0f50fd3d81f2574ce6fabcd Mon Sep 17 00:00:00 2001 From: anuveyatsu Date: Mon, 22 Jun 2020 10:58:30 +0600 Subject: [PATCH] [resource page][m]: refactor to work with Apollo. --- components/resource/About.tsx | 39 +++++++++++- components/resource/DataExplorer.tsx | 38 +++++++++++- pages/[org]/[dataset]/r/[resource]/index.tsx | 62 ++++++++++++++------ 3 files changed, 119 insertions(+), 20 deletions(-) diff --git a/components/resource/About.tsx b/components/resource/About.tsx index e4a28423..c750e3e7 100644 --- a/components/resource/About.tsx +++ b/components/resource/About.tsx @@ -1,6 +1,41 @@ 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 About({ resource }) { +export const QUERY = gql` + query dataset($id: String!) { + dataset(id: $id) { + result { + resources { + name + id + url + format + title + created + last_modified + } + } + } + } +`; + +export default function About({ variables }) { + const { loading, error, data } = useQuery(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 ; + if (loading) return
Loading
; + + const { result } = data.dataset; + const resource = result.resources[0]; return ( <> @@ -26,7 +61,7 @@ export default function About({ resource }) {
{resource.created} {resource.last_modified || ''} - + {resource.format} diff --git a/components/resource/DataExplorer.tsx b/components/resource/DataExplorer.tsx index 6f2fbd8a..ee35308d 100644 --- a/components/resource/DataExplorer.tsx +++ b/components/resource/DataExplorer.tsx @@ -1,5 +1,41 @@ 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 QUERY = gql` + query dataset($id: String!) { + dataset(id: $id) { + result { + resources { + name + id + url + format + title + created + last_modified + } + } + } + } +`; + +export default function DataExplorer({ variables }) { + const { loading, error, data } = useQuery(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 ; + if (loading) return
Loading
; + + const { result } = data.dataset; + const resource = result.resources[0]; -export default function DataExplorer({ resource }) { return <>{JSON.stringify(resource)}; } diff --git a/pages/[org]/[dataset]/r/[resource]/index.tsx b/pages/[org]/[dataset]/r/[resource]/index.tsx index ee9dfdd5..351841e5 100644 --- a/pages/[org]/[dataset]/r/[resource]/index.tsx +++ b/pages/[org]/[dataset]/r/[resource]/index.tsx @@ -1,4 +1,7 @@ import { GetServerSideProps } from 'next'; +import gql from 'graphql-tag'; +import { useQuery } from '@apollo/react-hooks'; +import { initializeApollo } from '../../../../../lib/apolloClient'; import config from '../../../../../config'; import utils from '../../../../../utils'; import Head from 'next/head'; @@ -6,39 +9,64 @@ import Nav from '../../../../../components/home/Nav'; import About from '../../../../../components/resource/About'; import DataExplorer from '../../../../../components/resource/DataExplorer'; -function Resource({ resource }) { +const QUERY = gql` + query dataset($id: String!) { + dataset(id: $id) { + result { + resources { + name + title + } + } + } + } +`; + +function Resource({ variables }) { + const { + data: { + dataset: { result }, + }, + } = useQuery(QUERY, { variables }); + return ( <> - Portal | {resource.title || resource.name} + + Portal | {result.resources[0].title || result.resources[0].name} +