Skip to content

Commit

Permalink
Feature/update doi on submit (#338)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
BryceMarshall and fostermh authored Apr 9, 2024
1 parent 28335e7 commit 7cccb26
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 24 deletions.
32 changes: 32 additions & 0 deletions src/components/Pages/MetadataForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => (
<Tooltip
Expand Down Expand Up @@ -93,7 +94,11 @@ const styles = (theme) => ({
right: theme.spacing(2),
},
});


class MetadataForm extends FormClassTemplate {


constructor(props) {
super(props);

Expand All @@ -117,6 +122,8 @@ class MetadataForm extends FormClassTemplate {
editorInfo: { email: "", displayName: "" },
loggedInUserCanEditRecord: false,
saveIncompleteRecordModalOpen: false,
doiUpdated: false,
doiError: false,
};
}

Expand Down Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down Expand Up @@ -534,6 +564,8 @@ class MetadataForm extends FormClassTemplate {
<TabPanel value={tabIndex} index="submit">
<SubmitTab
{...tabProps}
doiUpdated={this.state.doiUpdated}
doiError={this.state.doiError}
submitRecord={() => this.handleSubmitRecord()}
/>
</TabPanel>
Expand Down
27 changes: 7 additions & 20 deletions src/components/Tabs/IdentificationTab.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {

import regions from "../../regions";
import { UserContext } from "../../providers/UserProvider";
import performUpdateDraftDoi from "../../utils/doiUpdate";

const IdentificationTab = ({
disabled,
Expand All @@ -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);
Expand Down Expand Up @@ -94,7 +95,7 @@ const IdentificationTab = ({
if(!loadingAuthHash && dataciteAuthHash) {

await createDraftDoi({
record: mappedDataCiteObject,
record: mappedDataCiteObject,
authHash: dataciteAuthHash,
})
.then((response) => {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -838,7 +825,7 @@ const IdentificationTab = ({
)}
</div>
</Button>
)}
)}

{doiErrorFlag && (
<span>
Expand Down
29 changes: 27 additions & 2 deletions src/components/Tabs/SubmitTab.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -132,6 +132,31 @@ const SubmitTab = ({ record, submitRecord }) => {
</Fr>
</I18n>
</Typography>
{doiUpdated && (<Typography>
<b>
<I18n>
<En>
DOI has successfully been updated with the submitted changes.
</En>
<Fr>
DOI a été mis à jour avec succès avec les modifications soumises.
</Fr>
</I18n>
</b>
</Typography>)}
{doiError && (
<Typography>
<b>
<I18n>
<En>
Error occurred when updating DOI.
</En>
<Fr>
Une erreur s'est produite lors de la mise à jour du DOI
</Fr>
</I18n>
</b>
</Typography>)}
</Grid>
) : (
<>
Expand Down Expand Up @@ -233,7 +258,7 @@ const SubmitTab = ({ record, submitRecord }) => {
</I18n>
</Typography>
</Grid>

<Grid item xs>
<Typography>
<I18n>
Expand Down
25 changes: 25 additions & 0 deletions src/utils/doiUpdate.js
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 2 additions & 2 deletions src/utils/firebaseEnableDoiCreation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 7cccb26

Please sign in to comment.