Skip to content

Commit

Permalink
optional-doi: fix validation check when user needs a DOI
Browse files Browse the repository at this point in the history
  • Loading branch information
zzacharo committed Dec 16, 2024
1 parent 22f49d7 commit ade7093
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ class PublishButtonComponent extends Component {
handlePublish = (event, handleSubmit, publishWithoutCommunity) => {
const { setSubmitContext } = this.context;
const { formik, raiseDOINeededButNotReserved, isDOIRequired } = this.props;
const noINeedOne = formik?.values?.noINeedOne;
const noINeedDOI = formik?.values?.noINeedDOI;
// Check for explicit DOI reservation via the "GET DOI button" only when DOI is
// optional in the instance's settings. If it is required, backend will automatically
// mint one even if it was not explicitly reserved
const shouldCheckForExplicitDOIReservation =
isDOIRequired !== undefined && // isDOIRequired is undefined when no value was provided from Invenio-app-rdm
!isDOIRequired &&
noINeedOne &&
noINeedDOI &&
Object.keys(formik?.values?.pids).length === 0;
if (shouldCheckForExplicitDOIReservation) {
const errors = {
pids: {
doi: i18next.t("DOI is needed. Please click on the button to reserve it."),
doi: i18next.t("DOI is needed. You need to reserve a DOI before publishing."),
},
};
formik.setErrors(errors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,14 +497,14 @@ class CustomPIDField extends Component {
form.setFieldValue("pids", {});
if (!required) {
// We set the
form.setFieldValue("noINeedOne", true);
form.setFieldValue("noINeedDOI", true);
}
} else if (userSelectedNoNeed) {
form.setFieldValue("pids", {});
form.setFieldValue("noINeedOne", false);
form.setFieldValue("noINeedDOI", false);
} else {
this.onExternalIdentifierChanged("");
form.setFieldValue("noINeedOne", false);
form.setFieldValue("noINeedDOI", false);
}
form.setFieldError(fieldPath, false);
this.setState({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
RESERVE_PID_STARTED,
RESERVE_PID_SUCCEEDED,
SET_COMMUNITY,
SET_DOI_NEEDED,
} from "../types";

async function changeURLAfterCreation(draftURL) {
Expand Down Expand Up @@ -152,6 +153,16 @@ export const save = (draft) => {
type: DRAFT_SAVE_SUCCEEDED,
payload: { data: response.data },
});

if (draft.noINeedDOI) {
// Save the choice that user selected that DOI is needed. This is used to validate
// if user has reserved a DOI before clicking publish. This check is valid when
// DOI is optional
dispatch({
type: SET_DOI_NEEDED,
payload: { noINeedDOI: draft.noINeedDOI },
});
}
};
};

Expand Down Expand Up @@ -344,3 +355,12 @@ export const changeSelectedCommunity = (community) => {
});
};
};

export const setDOINeeded = (value) => {
return async (dispatch) => {
dispatch({
type: SET_DOI_NEEDED,
payload: { noINeedDOI: value },
});
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
RESERVE_PID_STARTED,
RESERVE_PID_SUCCEEDED,
SET_COMMUNITY,
SET_DOI_NEEDED,
} from "../types";

export class DepositStatus {
Expand Down Expand Up @@ -332,13 +333,21 @@ const depositReducer = (state = {}, action) => {
},
};
}

return {
...state,
record: recordCopy,
editorState: computeDepositState(recordCopy, action.payload.community),
};
}
case SET_DOI_NEEDED: {
const recordCopy = {
...state.record,
};
return {
...state,
record: { ...recordCopy, ...action.payload },
};
}
default:
return state;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export const DISCARD_PID_STARTED = "DISCARD_PID_STARTED";
export const DISCARD_PID_SUCCEEDED = "DISCARD_PID_SUCCEEDED";
export const DISCARD_PID_FAILED = "DISCARD_PID_FAILED";

export const SET_DOI_NEEDED = "SET_DOI_NEEDED";

// Files
export const FILE_UPLOAD_ADDED = "FILE_UPLOAD_ADDED";
export const FILE_UPLOAD_IN_PROGRESS = "FILE_UPLOAD_IN_PROGRESS";
Expand Down
12 changes: 6 additions & 6 deletions invenio_rdm_records/services/components/pids.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ def update_draft(self, identity, data=None, record=None, errors=None):

# if DOI is not required in an instance check validate allowed providers
# for each record version
if "doi" not in required_schemes and not self.service.check_permission(
identity, "pid_manage"
):
doi_required = "doi" in required_schemes
can_manage_dois = self.service.check_permission(identity, "pid_manage")
if not doi_required and not can_manage_dois:
previous_published = self.service.record_cls.get_latest_published_by_parent(
record.parent
)
Expand Down Expand Up @@ -151,9 +151,9 @@ def publish(self, identity, draft=None, record=None):

# if DOI is not required in an instance check validate allowed providers
# for each record version
if "doi" not in required_schemes and not self.service.check_permission(
identity, "pid_manage"
):
doi_required = "doi" in required_schemes
can_manage_dois = self.service.check_permission(identity, "pid_manage")
if not doi_required and not can_manage_dois:
# if a doi was ever minted for the parent record then we always require one
# for any version of the record that will be published
if draft.parent.get("pids", {}).get("doi"):
Expand Down
1 change: 0 additions & 1 deletion tests/services/test_rdm_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from copy import deepcopy

import pytest

from invenio_access.permissions import system_identity

from invenio_rdm_records.proxies import current_rdm_records
Expand Down

0 comments on commit ade7093

Please sign in to comment.