Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publisher dataset count by name #140

Merged
merged 5 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions src/components/Organization/index.jsx
Original file line number Diff line number Diff line change
@@ -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 = <img alt={name || 'Organization Image'} src={imageUrl} />;
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 (
<div className="dc-org-block" style={{ textAlign: alignment }}>
Expand All @@ -20,10 +44,29 @@ function Organization(props) {
{description}
</div>
)}

{dataObj && dataObj.dataset !== 'undefined' ?
<PublisherDatasetCountByName
name={name}
datasetCount={
countDatasetsByName(name, dataObj.dataset)
} /> :
<PublisherDatasetCountByName name={name} />
}
</div>
);
}

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: "",
Expand All @@ -37,6 +80,7 @@ Organization.propTypes = {
description: PropTypes.string,
imageUrl: PropTypes.string,
searchUrl: PropTypes.string,
organizationEndpoint: PropTypes.string,
};

export default Organization;
28 changes: 27 additions & 1 deletion src/components/Organization/index.test.jsx
Original file line number Diff line number Diff line change
@@ -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('<Organization />', () => {
test('renders a heading', () => {
render(<Organization name="DKAN" />);
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(<Organization name="DKAN" />);
expect(screen.getByText('datasets')).toBeInTheDocument();
});

test('Calculates a count from data',() => {
expect(countDatasetsByName("State Economic Council", data)).toEqual(2);
});

});
38 changes: 38 additions & 0 deletions src/components/PublisherDatasetCountByName/doc.mdx
Original file line number Diff line number Diff line change
@@ -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

<Props of={PublisherDatasetCountByName} />

### With no datasetCount.
<Playground>
<PublisherDatasetCountByName
name="State Economic Council"
/>
</Playground>

## Basic usage
### With a count of 1.
<Playground>
<PublisherDatasetCountByName
datasetCount="1"
name="State Economic Council"
/>
</Playground>

With a count of more than 1.
<Playground>
<PublisherDatasetCountByName
datasetCount="3"
name="State Economic Council"
/>
</Playground>
27 changes: 27 additions & 0 deletions src/components/PublisherDatasetCountByName/index.jsx
Original file line number Diff line number Diff line change
@@ -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 (
<Link to={link} className="publisher-datasets-link" alt="Publisher datasets">
{str}
</Link>
);
};

export default PublisherDatasetCountByName;

PublisherDatasetCountByName.propTypes = {
name: PropTypes.string,
searchUrl: PropTypes.string,
datasetCount: PropTypes.string
};
22 changes: 22 additions & 0 deletions src/components/PublisherDatasetCountByName/index.test.js
Original file line number Diff line number Diff line change
@@ -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('<PublisherDatasetCountByName />', () => {

test('If no dataset renders, just a link to the page.', () => {
render(<PublisherDatasetCountByName name="Non matching organization." />);
expect(screen.getByText('datasets')).toBeInTheDocument();
});

test('If there is a publisher with datasets render the dataset count.',() => {
render(<PublisherDatasetCountByName name="State Economic Council" datasetCount="3" />);
expect(screen.getByText('3x datasets')).toBeInTheDocument();
});

test('Dataset count with just one item.',() => {
render(<PublisherDatasetCountByName name="State Economic Council" datasetCount="1" />);
});

});
4 changes: 3 additions & 1 deletion src/components/PublisherList/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Organization from '../Organization';

function PublisherList(props) {
const {
items, className,
items, className, organizationEndpoint
} = props;
let content = (<div />);

Expand All @@ -15,6 +15,7 @@ function PublisherList(props) {
key={item.identifier}
imageUrl={item.imageUrl}
description={item.description}
organizationEndpoint={organizationEndpoint}
searchUrl={item.searchUrl}
alignment={item.alignment}
/>
Expand All @@ -40,6 +41,7 @@ PublisherList.propTypes = {
identifier: PropTypes.string,
imageUrl: PropTypes.string,
searchUrl: PropTypes.string,
organizationEndpoint: PropTypes.string,
})).isRequired,
className: PropTypes.string,
};
Expand Down