diff --git a/src/components/Organization/index.jsx b/src/components/Organization/index.jsx index 5a116cb2..ff78568b 100644 --- a/src/components/Organization/index.jsx +++ b/src/components/Organization/index.jsx @@ -1,11 +1,35 @@ -import React from "react"; +import React, { useState, useEffect } from 'react'; import PropTypes from "prop-types"; import { Link } from "@reach/router"; +import PublisherDatasetCountByName from "../PublisherDatasetCountByName"; +import axios from 'axios'; function Organization(props) { - const { name, description, imageUrl, searchUrl, alignment } = props; + const { name, + description, + imageUrl, + searchUrl, + alignment, + organizationEndpoint} = props; + const image = {name; const link = searchUrl ? searchUrl : `search/?publisher__name=${name}`; + const [dataObj, setDataObj] = useState(); + + const fetchData = async () => { + const endpoint = organizationEndpoint ? organizationEndpoint.replace("api/1", "data.json") : null; + if (endpoint) { + axios.get(endpoint) + .then(res => (setDataObj(res.data))) + .catch(err => (console.log("Error, check URL/Cors.", err))); + } else { + console.log("No search endpoint defined for Organization/s, so no dataset info available."); + } + }; + + useEffect(() => { + fetchData(); + }, []); return (
@@ -20,10 +44,29 @@ function Organization(props) { {description}
)} + + {dataObj && dataObj.dataset !== 'undefined' ? + : + + } ); } +export const countDatasetsByName = (publisher, datasets) => { + const publishers = datasets.map((data, index, arr) => {return data.publisher; }); + const result = publishers.filter((p) => {return p.name === publisher;}); + if (typeof result !== 'undefined' && result.length) { + return result.length; + + } + return null; +}; + Organization.defaultProps = { alignment: "center", name: "", @@ -37,6 +80,7 @@ Organization.propTypes = { description: PropTypes.string, imageUrl: PropTypes.string, searchUrl: PropTypes.string, + organizationEndpoint: PropTypes.string, }; export default Organization; diff --git a/src/components/Organization/index.test.jsx b/src/components/Organization/index.test.jsx index 798c3bf5..fc0d5ce5 100644 --- a/src/components/Organization/index.test.jsx +++ b/src/components/Organization/index.test.jsx @@ -1,11 +1,37 @@ import React from 'react'; import { render, screen } from '@testing-library/react'; import '@testing-library/jest-dom/extend-expect'; -import Organization from './index'; +import Organization, {countDatasetsByName} from './index'; +import PublisherDatasetCountByName from '../PublisherDatasetCountByName'; + +const data = + [{"publisher": { + "@type": "org:Organization", + "name": "State Economic Council" + }}, + {"publisher": { + "@type": "org:Organization", + "name": "State Economic Council" + }}]; + describe('', () => { test('renders a heading', () => { render(); expect(screen.getByRole('heading', 'DKAN')).toBeInTheDocument(); }); + + test('Has a publisher name.', () => { + expect(data[0]['publisher']['name']).toEqual("State Economic Council"); + }); + + test('renders with a dataset link with no count', () => { + render(); + expect(screen.getByText('datasets')).toBeInTheDocument(); + }); + + test('Calculates a count from data',() => { + expect(countDatasetsByName("State Economic Council", data)).toEqual(2); + }); + }); diff --git a/src/components/PublisherDatasetCountByName/doc.mdx b/src/components/PublisherDatasetCountByName/doc.mdx new file mode 100644 index 00000000..ab90d1e8 --- /dev/null +++ b/src/components/PublisherDatasetCountByName/doc.mdx @@ -0,0 +1,38 @@ +--- +name: Publisher Dataset Count (by name). +menu: Components +route: /components/publisher-dataset-count-by-name +--- + +import { Playground, Props } from 'docz' +import PublisherDatasetCountByName from './index' + +# Publisher dataset count (by name). + +## Properties + + + +### With no datasetCount. + + + + +## Basic usage +### With a count of 1. + + + + +With a count of more than 1. + + + diff --git a/src/components/PublisherDatasetCountByName/index.jsx b/src/components/PublisherDatasetCountByName/index.jsx new file mode 100644 index 00000000..8626eae6 --- /dev/null +++ b/src/components/PublisherDatasetCountByName/index.jsx @@ -0,0 +1,27 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import { Link } from "@reach/router"; + +const PublisherDatasetCountByName = (props) => { + const { name, searchUrl, datasetCount} = props; + const link = searchUrl ? searchUrl : `search/?publisher__name=${name}`; + let str; + if (datasetCount) { + str = (datasetCount === 1) ? '1 dataset' : `${datasetCount}x datasets`; + } else { + str = 'datasets'; + } + return ( + + {str} + + ); +}; + +export default PublisherDatasetCountByName; + +PublisherDatasetCountByName.propTypes = { + name: PropTypes.string, + searchUrl: PropTypes.string, + datasetCount: PropTypes.string +}; diff --git a/src/components/PublisherDatasetCountByName/index.test.js b/src/components/PublisherDatasetCountByName/index.test.js new file mode 100644 index 00000000..79f10aef --- /dev/null +++ b/src/components/PublisherDatasetCountByName/index.test.js @@ -0,0 +1,22 @@ +import React from 'react'; +import { render, screen } from '@testing-library/react'; +import '@testing-library/jest-dom/extend-expect'; +import PublisherDatasetCountByName from './index'; + +describe('', () => { + + test('If no dataset renders, just a link to the page.', () => { + render(); + expect(screen.getByText('datasets')).toBeInTheDocument(); + }); + + test('If there is a publisher with datasets render the dataset count.',() => { + render(); + expect(screen.getByText('3x datasets')).toBeInTheDocument(); + }); + + test('Dataset count with just one item.',() => { + render(); + }); + +}); diff --git a/src/components/PublisherList/index.jsx b/src/components/PublisherList/index.jsx index c7b12146..0e2ae376 100644 --- a/src/components/PublisherList/index.jsx +++ b/src/components/PublisherList/index.jsx @@ -4,7 +4,7 @@ import Organization from '../Organization'; function PublisherList(props) { const { - items, className, + items, className, organizationEndpoint } = props; let content = (
); @@ -15,6 +15,7 @@ function PublisherList(props) { key={item.identifier} imageUrl={item.imageUrl} description={item.description} + organizationEndpoint={organizationEndpoint} searchUrl={item.searchUrl} alignment={item.alignment} /> @@ -40,6 +41,7 @@ PublisherList.propTypes = { identifier: PropTypes.string, imageUrl: PropTypes.string, searchUrl: PropTypes.string, + organizationEndpoint: PropTypes.string, })).isRequired, className: PropTypes.string, };