From 5a94d9d36887d020baa4f76184b1dc50d42545a9 Mon Sep 17 00:00:00 2001 From: anuveyatsu Date: Fri, 12 Jun 2020 14:49:12 +0600 Subject: [PATCH] [search][xl]: initial search page with essential components. - No pagination at the moment. - Sorting isn't functional yet. - Search works - we only have mocks for searching 'gdp' param. --- components/search/Input.tsx | 33 +++++++++++++++++++++++++++++++++ components/search/Item.tsx | 21 +++++++++++++++++++++ components/search/List.tsx | 9 +++++++++ components/search/Sort.tsx | 14 ++++++++++++++ components/search/Total.tsx | 3 +++ pages/search.tsx | 34 ++++++++++++++++++++++++++++++++++ 6 files changed, 114 insertions(+) create mode 100644 components/search/Input.tsx create mode 100644 components/search/Item.tsx create mode 100644 components/search/List.tsx create mode 100644 components/search/Sort.tsx create mode 100644 components/search/Total.tsx create mode 100644 pages/search.tsx diff --git a/components/search/Input.tsx b/components/search/Input.tsx new file mode 100644 index 00000000..a2406e79 --- /dev/null +++ b/components/search/Input.tsx @@ -0,0 +1,33 @@ +import { useState } from 'react' +import { useRouter } from 'next/router' +import Link from 'next/link' + +export default function Input({ query }) { + const router = useRouter() + const [q, setQ] = useState(query.q) + + const handleChange = (event) => { + setQ(event.target.value) + } + + const handleSubmit = (event) => { + event.preventDefault() + router.push({ + pathname: '/search', + query: { q }, + }) + } + + return ( +
+ +
+ ) +} diff --git a/components/search/Item.tsx b/components/search/Item.tsx new file mode 100644 index 00000000..4658621d --- /dev/null +++ b/components/search/Item.tsx @@ -0,0 +1,21 @@ +import Link from 'next/link' + +export default function Item({ datapackage }) { + return ( +
+

+ + { datapackage.title || datapackage.name } + +

+ + + { datapackage.organization ? datapackage.organization.title : 'dataset' } + + +
+ { datapackage.description } +
+
+ ) +} diff --git a/components/search/List.tsx b/components/search/List.tsx new file mode 100644 index 00000000..918bd343 --- /dev/null +++ b/components/search/List.tsx @@ -0,0 +1,9 @@ +import Item from './Item' + +export default function List({ datapackages }) { + return ( + + ) +} diff --git a/components/search/Sort.tsx b/components/search/Sort.tsx new file mode 100644 index 00000000..578b6776 --- /dev/null +++ b/components/search/Sort.tsx @@ -0,0 +1,14 @@ +export default function Sort({ selected }) { + return ( +
+ + +
+ ) +} diff --git a/components/search/Total.tsx b/components/search/Total.tsx new file mode 100644 index 00000000..e26a3178 --- /dev/null +++ b/components/search/Total.tsx @@ -0,0 +1,3 @@ +export default function Total({ total }) { + return

{ total } results found

+} diff --git a/pages/search.tsx b/pages/search.tsx new file mode 100644 index 00000000..d57b8397 --- /dev/null +++ b/pages/search.tsx @@ -0,0 +1,34 @@ +import { GetServerSideProps } from 'next' +import querystring from 'querystring' +import config from '../config' +import utils from '../utils' +import Input from '../components/search/Input' +import Total from '../components/search/Total' +import Sort from '../components/search/Sort' +import List from '../components/search/List' + +function Search({ ckanResult, datapackages, query }) { + return ( +
+ + + + +
+ ) +} + +export const getServerSideProps: GetServerSideProps = async (context) => { + const query = context.query || {} + const ckanQuery = querystring.stringify( + utils.convertToCkanSearchQuery(query) + ) + console.log(ckanQuery) + const res = await fetch(`${config.get('DMS')}/api/3/action/package_search?${ckanQuery}`) + const ckanResult = (await res.json()).result + const datapackages = ckanResult.results.map(item => utils.ckanToDataPackage(item)) + + return { props: { ckanResult, datapackages, query } } +} + +export default Search