From 7cccb268055e6719bcbe286416d7e82836cf84a8 Mon Sep 17 00:00:00 2001 From: Bryce Marshall Date: Tue, 9 Apr 2024 10:08:11 -0700 Subject: [PATCH] Feature/update doi on submit (#338) * Added call to update DOI when submitted * Added call to update DOI to submit button * Linting * Refactored DOI update logic to a util --------- Co-authored-by: Matthew Foster --- src/components/Pages/MetadataForm.jsx | 32 +++++++++++++++++++++++ src/components/Tabs/IdentificationTab.jsx | 27 +++++-------------- src/components/Tabs/SubmitTab.jsx | 29 ++++++++++++++++++-- src/utils/doiUpdate.js | 25 ++++++++++++++++++ src/utils/firebaseEnableDoiCreation.js | 4 +-- 5 files changed, 93 insertions(+), 24 deletions(-) create mode 100644 src/utils/doiUpdate.js diff --git a/src/components/Pages/MetadataForm.jsx b/src/components/Pages/MetadataForm.jsx index 8e558dea..7f5acf27 100644 --- a/src/components/Pages/MetadataForm.jsx +++ b/src/components/Pages/MetadataForm.jsx @@ -42,6 +42,7 @@ import { percentValid } from "../../utils/validate"; import tabs from "../../utils/tabs"; import { getBlankRecord } from "../../utils/blankRecord"; +import performUpdateDraftDoi from "../../utils/doiUpdate"; const LinearProgressWithLabel = ({ value }) => ( ({ right: theme.spacing(2), }, }); + + class MetadataForm extends FormClassTemplate { + + constructor(props) { super(props); @@ -117,6 +122,8 @@ class MetadataForm extends FormClassTemplate { editorInfo: { email: "", displayName: "" }, loggedInUserCanEditRecord: false, saveIncompleteRecordModalOpen: false, + doiUpdated: false, + doiError: false, }; } @@ -254,6 +261,27 @@ class MetadataForm extends FormClassTemplate { return contactsRef.push(contact).getKey(); } + async handleUpdateDraftDOI() { + const { match } = this.props; + const { region, language } = match.params; + const { record } = this.state; + + try { + const statusCode = await performUpdateDraftDoi(record, region, language); + + if (statusCode === 200) { + this.state.doiUpdated = true + } else { + this.state.doiError = true + } + } catch (err) { + // eslint-disable-next-line no-console + console.error('Error updating draft DOI: ', err); + this.state.doiError = true + throw err; + } + } + async handleSubmitRecord() { const { match } = this.props; const { region, userID } = match.params; @@ -265,6 +293,8 @@ class MetadataForm extends FormClassTemplate { const recordUserID = isNewRecord ? loggedInUserID : userID; const recordID = await this.handleSaveClick(); + await this.handleUpdateDraftDOI() + return submitRecord(region, recordUserID, recordID, "submitted", record); } @@ -534,6 +564,8 @@ class MetadataForm extends FormClassTemplate { this.handleSubmitRecord()} /> diff --git a/src/components/Tabs/IdentificationTab.jsx b/src/components/Tabs/IdentificationTab.jsx index 5d171371..7b291ae4 100644 --- a/src/components/Tabs/IdentificationTab.jsx +++ b/src/components/Tabs/IdentificationTab.jsx @@ -35,6 +35,7 @@ import { import regions from "../../regions"; import { UserContext } from "../../providers/UserProvider"; +import performUpdateDraftDoi from "../../utils/doiUpdate"; const IdentificationTab = ({ disabled, @@ -43,11 +44,11 @@ const IdentificationTab = ({ updateRecord, projects, }) => { - const { createDraftDoi, updateDraftDoi, deleteDraftDoi, getDoiStatus } = useContext(UserContext); + const { createDraftDoi, deleteDraftDoi, getDoiStatus } = useContext(UserContext); const { language, region, userID } = useParams(); const regionInfo = regions[region]; const doiIsValid =validateDOI(record.datasetIdentifier) - + const languageUpperCase = language.toUpperCase(); const [doiGenerated, setDoiGenerated] = useState(false); const [doiErrorFlag, setDoiErrorFlag] = useState(false); @@ -94,7 +95,7 @@ const IdentificationTab = ({ if(!loadingAuthHash && dataciteAuthHash) { await createDraftDoi({ - record: mappedDataCiteObject, + record: mappedDataCiteObject, authHash: dataciteAuthHash, }) .then((response) => { @@ -137,28 +138,14 @@ const IdentificationTab = ({ console.error("Error in handleGenerateDOI:", err); setDoiErrorFlag(true); throw err; - } + } } async function handleUpdateDraftDOI() { setLoadingDoiUpdate(true); try { - const mappedDataCiteObject = recordToDataCite(record, language, region, datacitePrefix); - delete mappedDataCiteObject.data.type; - delete mappedDataCiteObject.data.attributes.prefix; - - // Extract DOI from the full URL - const doi = record.datasetIdentifier.replace('https://doi.org/', ''); - - const dataObject = { - doi, - data: mappedDataCiteObject, - dataciteAuthHash, - } - - const response = await updateDraftDoi( dataObject ); - const statusCode = response.data.status; + const statusCode = await performUpdateDraftDoi(record, region, language) if (statusCode === 200) { setDoiUpdateFlag(true); @@ -838,7 +825,7 @@ const IdentificationTab = ({ )} - )} + )} {doiErrorFlag && ( diff --git a/src/components/Tabs/SubmitTab.jsx b/src/components/Tabs/SubmitTab.jsx index 275fe4de..6269ac48 100644 --- a/src/components/Tabs/SubmitTab.jsx +++ b/src/components/Tabs/SubmitTab.jsx @@ -26,7 +26,7 @@ import tabs from "../../utils/tabs"; import GetRegionInfo from "../FormComponents/Regions"; -const SubmitTab = ({ record, submitRecord }) => { +const SubmitTab = ({ record, submitRecord, doiUpdated, doiError }) => { const mounted = useRef(false); const [isSubmitting, setSubmitting] = useState(false); const [validationWarnings, setValidationWarnings] = useState(false); @@ -132,6 +132,31 @@ const SubmitTab = ({ record, submitRecord }) => { + {doiUpdated && ( + + + + DOI has successfully been updated with the submitted changes. + + + DOI a été mis à jour avec succès avec les modifications soumises. + + + + )} + {doiError && ( + + + + + Error occurred when updating DOI. + + + Une erreur s'est produite lors de la mise à jour du DOI + + + + )} ) : ( <> @@ -233,7 +258,7 @@ const SubmitTab = ({ record, submitRecord }) => { - + diff --git a/src/utils/doiUpdate.js b/src/utils/doiUpdate.js new file mode 100644 index 00000000..0a49dd5e --- /dev/null +++ b/src/utils/doiUpdate.js @@ -0,0 +1,25 @@ +import recordToDataCite from "./recordToDataCite"; +import firebase from "../firebase"; +import {getAuthHash} from "./firebaseEnableDoiCreation"; + +async function performUpdateDraftDoi(record, region, language) { + const dataciteAuthHash = await getAuthHash(region); + + const mappedDataCiteObject = recordToDataCite(record, language, region); + delete mappedDataCiteObject.data.type; + delete mappedDataCiteObject.data.attributes.prefix; + + // Extract DOI from the full URL + const doi = record.datasetIdentifier.replace('https://doi.org/', ''); + + const dataObject = { + doi, + data: mappedDataCiteObject, + dataciteAuthHash, + } + + const response = await firebase.functions().httpsCallable("updateDraftDoi")(dataObject); + return response.data.status; + } + +export default performUpdateDraftDoi; diff --git a/src/utils/firebaseEnableDoiCreation.js b/src/utils/firebaseEnableDoiCreation.js index db3877f6..6ccd8e0a 100644 --- a/src/utils/firebaseEnableDoiCreation.js +++ b/src/utils/firebaseEnableDoiCreation.js @@ -6,7 +6,7 @@ export async function newDataciteAccount(region, prefix, authHash) { .ref("admin") .child(region) .child("dataciteCredentials"); - + // Overwriting prefix and authHash directly under dataciteCredentials await dataciteRef.set({ prefix, @@ -52,7 +52,7 @@ export async function getAuthHash(region) { } catch (error) { console.error(`Error fetching Datacite Auth Hash for region ${region}:`, error); return null; -} +} } export async function getCredentialsStored(region) {