Skip to content

Commit

Permalink
components: add affiliations suggestions display component
Browse files Browse the repository at this point in the history
  • Loading branch information
carlinmack committed Sep 2, 2024
1 parent 735495a commit c893900
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 1 deletion.
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";
123 changes: 123 additions & 0 deletions src/lib/elements/contrib/invenioRDM/records/AffiliationsSuggestions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* // 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 = null;
let link = null;

if (identifier.scheme === "orcid") {
icon = "/static/images/orcid.svg";
link = "https://orcid.org/" + identifier.identifier;
} else if (identifier.scheme === "gnd") {
icon = "/static/images/gnd-icon.svg";
link = "https://d-nb.info/gnd/" + identifier.identifier;
} else if (identifier.scheme === "ror") {
return; // ROR doesn't recommend displaying ROR IDs
} else if (identifier.scheme === "isni" || identifier.scheme === "grid") {
return;
} else {
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 : null}
</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}${
locationPart && typesPart ? " — " : ""
}${typesPart}`.trim();
} else {
return (
creatibutor?.affiliations?.map((affiliation) => affiliation.name)?.join(", ") ||
""
);
}
};

export const AffiliationsSuggestions = (
creatibutors,
isOrganization,
showManualEntry,

Check failure on line 70 in src/lib/elements/contrib/invenioRDM/records/AffiliationsSuggestions.js

View workflow job for this annotation

GitHub Actions / Tests (18.x)

Delete `,`

Check failure on line 70 in src/lib/elements/contrib/invenioRDM/records/AffiliationsSuggestions.js

View workflow job for this annotation

GitHub Actions / Tests (16.x)

Delete `,`
) => {
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,
value: creatibutor.id,
extra: creatibutor,
key: creatibutor.id,
id: creatibutor.id,
content: (
<Header>
{name} {idString.length ? <>({idString})</> : null}
<Header.Subheader>{subheader}</Header.Subheader>
</Header>
),
};
});

if (showManualEntry) {
results.push({
text: "Manual entry",
value: "Manual entry",
extra: "Manual entry",
key: "manual-entry",
content: (
<Header textAlign="center">
<Header.Content>
<p>
<Trans>
{/* eslint-disable-next-line jsx-a11y/anchor-is-valid*/}
Couldn't find your person? You can <a>create a new entry</a>.
</Trans>
</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";

0 comments on commit c893900

Please sign in to comment.