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

components: add affiliations suggestions display component #255

Merged
merged 5 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion src/lib/elements/contrib/invenioRDM/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
* // under the terms of the MIT License; see LICENSE file for more details.
*/

export * from "./users";
export * from "./groups";
export * from "./records";
export * from "./users";
125 changes: 125 additions & 0 deletions src/lib/elements/contrib/invenioRDM/records/AffiliationsSuggestions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* // This file is part of React-Invenio-Forms
* // Copyright (C) 2024 CERN.
* //
* // React-Invenio-Forms is free software; you can redistribute it and/or modify it
* // under the terms of the MIT License; see LICENSE file for more details.
*/

import React from "react";
import { Image } from "../../../Image";
import { Header } from "semantic-ui-react";
import { Trans } from "react-i18next";

const makeIdEntry = (identifier) => {
let icon, link;

switch (identifier.scheme) {
case "orcid":
icon = "/static/images/orcid.svg";
link = `https://orcid.org/${identifier.identifier}`;
break;
case "gnd":
icon = "/static/images/gnd-icon.svg";
link = `https://d-nb.info/gnd/${identifier.identifier}`;
break;
case "ror": // ROR doesn't recommend displaying ROR IDs
case "isni":
case "grid":
return; // Skip these schemes
default:
return (
<>
{identifier.scheme}: {identifier.identifier}
</>
);
}

return (
<span key={identifier.identifier}>
<a href={link} target="_blank" rel="noopener noreferrer">
<Image src={icon} className="inline-id-icon mr-5" verticalAlign="middle" />
{identifier.scheme === "orcid" && identifier.identifier}
</a>
</span>
);
};

const makeSubheader = (creatibutor, isOrganization) => {
if (isOrganization) {
const locationPart = [creatibutor?.location_name, creatibutor?.country_name]
?.filter(Boolean)
?.join(", ");

const typesPart = creatibutor?.types
?.map((type) => type.charAt(0).toUpperCase() + type.slice(1))
?.join(", ");

return `${locationPart}${
slint marked this conversation as resolved.
Show resolved Hide resolved
locationPart && typesPart ? " — " : ""
}${typesPart}`.trim();
} else {
return (
creatibutor?.affiliations?.map((affiliation) => affiliation.name)?.join(", ") ||
""
);
}
};

export const AffiliationsSuggestions = (
creatibutors,
isOrganization,
showManualEntry
) => {
const results = creatibutors.map((creatibutor) => {
// ensure `affiliations` and `identifiers` are present
creatibutor.affiliations = creatibutor.affiliations || [];
creatibutor.identifiers = creatibutor.identifiers || [];

const subheader = makeSubheader(creatibutor, isOrganization);
let name = creatibutor.name;
if (creatibutor.acronym) name += ` (${creatibutor.acronym})`;

const idString = [];
creatibutor.identifiers?.forEach((i) => {
const idEntry = makeIdEntry(i);
if (idEntry) idString.push(idEntry);
});

return {
text: creatibutor.name,
slint marked this conversation as resolved.
Show resolved Hide resolved
value: creatibutor.name,
extra: creatibutor,
key: creatibutor.name,
id: creatibutor.id,
content: (
<Header>
{name} {idString.length > 0 && <>({idString})</>}
<Header.Subheader>{subheader}</Header.Subheader>
</Header>
),
};
});

if (showManualEntry) {
results.push({
text: "Manual entry",
carlinmack marked this conversation as resolved.
Show resolved Hide resolved
value: "Manual entry",
extra: "Manual entry",
key: "manual-entry",
content: (
<Header textAlign="center">
<Header.Content>
<p>
carlinmack marked this conversation as resolved.
Show resolved Hide resolved
<Trans>
{/* eslint-disable-next-line jsx-a11y/anchor-is-valid*/}
Couldn't find your person? You can <a>create a new entry</a>.
</Trans>
Copy link
Contributor Author

@carlinmack carlinmack Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to fix the tests we need to sort the import of react-i18next. In my opinion I think it would be better to shelve introducing i18n to react-invenio-forms and remove it from this PR, rather than introducing the translation mechanism (like so) in this PR.

This is the only translation that we would be supporting by importing react-i18next

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed IRL, we could introduce a prop with a default value to the text without translation. Then, in the places that this component is used we can pass a translatable text as a property.

Copy link
Contributor Author

@carlinmack carlinmack Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did attempt to change this into a component (as this is currently just a function which returns a list of dictionaries) so we could do this, however it would require changing how it is used which is not simple (as it is expecting a list of dictionaries not a react component)

I need to work on other things so I'm removing this translation and shelving this #6, apologies

</p>
</Header.Content>
</Header>
),
});
}
return results;
};
9 changes: 9 additions & 0 deletions src/lib/elements/contrib/invenioRDM/records/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* // This file is part of React-Invenio-Forms
* // Copyright (C) 2024 CERN.
* //
* // React-Invenio-Forms is free software; you can redistribute it and/or modify it
* // under the terms of the MIT License; see LICENSE file for more details.
*/

export { AffiliationsSuggestions } from "./AffiliationsSuggestions";
Loading