Skip to content

Commit

Permalink
doi: handle UI for optional DOI feature
Browse files Browse the repository at this point in the history
  • Loading branch information
anikachurilova committed Dec 5, 2024
1 parent 731ac68 commit bf5d9cf
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ class PublishButtonComponent extends Component {

handlePublish = (event, handleSubmit, publishWithoutCommunity) => {
const { setSubmitContext } = this.context;

// const { formik } = this.props;
// const noINeedOne = formik?.values?.noINeedOne;
// if (noINeedOne && Object.keys(formik?.values?.pids).length === 0) {
// formik.setFieldError("pids", "EROROROROROOR");
// }
setSubmitContext(
publishWithoutCommunity
? DepositFormSubmitActions.PUBLISH_WITHOUT_COMMUNITY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,13 @@ class ManagedUnmanagedSwitch extends Component {
handleChange = (e, { value }) => {
const { onManagedUnmanagedChange } = this.props;
const isManagedSelected = value === "managed";
onManagedUnmanagedChange(isManagedSelected);
const isNoNeedSelected = value === "notNeeded";
onManagedUnmanagedChange(isManagedSelected, isNoNeedSelected);
};

render() {
const { disabled, isManagedSelected, pidLabel } = this.props;
const { disabled, isManagedSelected, isNoNeedSelected, pidLabel, required } =
this.props;

return (
<Form.Group inline>
Expand All @@ -123,28 +125,41 @@ class ManagedUnmanagedSwitch extends Component {
pidLabel: pidLabel,
})}
</Form.Field>
<Form.Field width={2}>
<Form.Field width={4}>
<Radio
label={i18next.t("Yes")}
aria-label={i18next.t("Yes")}
label={i18next.t("Yes, I already have one")}
aria-label={i18next.t("Yes, I already have one")}
name="radioGroup"
value="unmanaged"
disabled={disabled}
checked={!isManagedSelected}
checked={!isManagedSelected && !isNoNeedSelected}
onChange={this.handleChange}
/>
</Form.Field>
<Form.Field width={2}>
<Form.Field width={3}>
<Radio
label={i18next.t("No")}
aria-label={i18next.t("No")}
label={i18next.t("No, I need one")}
aria-label={i18next.t("No, I need one")}
name="radioGroup"
value="managed"
disabled={disabled}
checked={isManagedSelected}
checked={isManagedSelected && !isNoNeedSelected}
onChange={this.handleChange}
/>
</Form.Field>
{!required && (
<Form.Field width={4}>
<Radio
label={i18next.t("No, I don't need one")}
aria-label={i18next.t("No, I don't need one")}
name="radioGroup"
value="notNeeded"
disabled={disabled}
checked={isNoNeedSelected}
onChange={this.handleChange}
/>
</Form.Field>
)}
</Form.Group>
);
}
Expand All @@ -153,8 +168,10 @@ class ManagedUnmanagedSwitch extends Component {
ManagedUnmanagedSwitch.propTypes = {
disabled: PropTypes.bool,
isManagedSelected: PropTypes.bool.isRequired,
isNoNeedSelected: PropTypes.bool.isRequired,
onManagedUnmanagedChange: PropTypes.func.isRequired,
pidLabel: PropTypes.string,
required: PropTypes.bool.isRequired,
};

ManagedUnmanagedSwitch.defaultProps = {
Expand Down Expand Up @@ -307,7 +324,7 @@ class UnmanagedIdentifierCmp extends Component {

render() {
const { localIdentifier } = this.state;
const { form, fieldPath, helpText, pidPlaceholder } = this.props;
const { form, fieldPath, helpText, pidPlaceholder, disabled } = this.props;
const fieldError = getFieldErrors(form, fieldPath);
return (
<>
Expand All @@ -318,6 +335,7 @@ class UnmanagedIdentifierCmp extends Component {
placeholder={pidPlaceholder}
width={16}
error={fieldError}
disabled={disabled}
/>
</Form.Field>
{helpText && <label className="helptext">{helpText}</label>}
Expand All @@ -333,10 +351,12 @@ UnmanagedIdentifierCmp.propTypes = {
identifier: PropTypes.string.isRequired,
onIdentifierChanged: PropTypes.func.isRequired,
pidPlaceholder: PropTypes.string.isRequired,
disabled: PropTypes.bool,
};

UnmanagedIdentifierCmp.defaultProps = {
helpText: null,
disabled: false,
};

/**
Expand All @@ -349,11 +369,17 @@ class CustomPIDField extends Component {
constructor(props) {
super(props);

const { canBeManaged, canBeUnmanaged } = this.props;
const { canBeManaged, canBeUnmanaged, record, field } = this.props;
this.canBeManagedAndUnmanaged = canBeManaged && canBeUnmanaged;

const value = field?.value;
const isDraft = record?.is_draft;
this.state = {
isManagedSelected: undefined,
isManagedSelected:
isDraft === true && value?.identifier && value?.provider !== PROVIDER_EXTERNAL
? true
: undefined,
isNoNeedSelected:
isDraft === true && value?.identifier === undefined ? true : undefined,
};
}

Expand All @@ -373,7 +399,7 @@ class CustomPIDField extends Component {
};

render() {
const { isManagedSelected } = this.state;
const { isManagedSelected, isNoNeedSelected } = this.state;
const {
btnLabelDiscardPID,
btnLabelGetPID,
Expand All @@ -392,6 +418,7 @@ class CustomPIDField extends Component {
pidType,
field,
record,
doiDefaultSelection,
} = this.props;

const value = field.value || {};
Expand All @@ -407,40 +434,64 @@ class CustomPIDField extends Component {
}

const hasManagedIdentifier = managedIdentifier !== "";
const hasUnmanagedIdentifier = unmanagedIdentifier !== "";

const isDraft = record.is_draft;
const _isUnmanagedSelected =
isManagedSelected === undefined
? hasUnmanagedIdentifier ||
(currentIdentifier === "" && doiDefaultSelection === "yes")
: !isManagedSelected;

const _isManagedSelected =
isManagedSelected === undefined
? hasManagedIdentifier || currentProvider === "" // i.e pids: {}
? hasManagedIdentifier ||
(currentIdentifier === "" && doiDefaultSelection === "no")
: isManagedSelected;

const _isNoNeedSelected =
isNoNeedSelected === undefined
? (!_isManagedSelected && !_isUnmanagedSelected) ||
(isDraft !== true &&
currentIdentifier === "" &&
doiDefaultSelection === "not_needed")
: isNoNeedSelected;

const doi = record?.pids?.doi?.identifier || "";
const hasDoi = doi !== "";
const isDoiCreated = currentIdentifier !== "";
const fieldError = getFieldErrors(form, fieldPath);
return (
<>
<Form.Field required={required} error={fieldError}>
<Form.Field required error={fieldError}>
<FieldLabel htmlFor={fieldPath} icon={pidIcon} label={fieldLabel} />
</Form.Field>

{this.canBeManagedAndUnmanaged && (
<ManagedUnmanagedSwitch
disabled={
(isEditingPublishedRecord || hasManagedIdentifier) &&
(hasDoi || isDoiCreated)
(hasDoi || isDoiCreated || _isNoNeedSelected)
}
isManagedSelected={_isManagedSelected}
onManagedUnmanagedChange={(userSelectedManaged) => {
isNoNeedSelected={_isNoNeedSelected}
onManagedUnmanagedChange={(userSelectedManaged, userSelectedNoNeed) => {
if (userSelectedManaged) {
form.setFieldValue("pids", {});
// form.setFieldValue("noINeedOne", true);
} else if (userSelectedNoNeed) {
// form.setFieldValue("noINeedOne", false);
} else {
this.onExternalIdentifierChanged("");
// form.setFieldValue("noINeedOne", false);
}
this.setState({
isManagedSelected: userSelectedManaged,
isNoNeedSelected: userSelectedNoNeed,
});
}}
pidLabel={pidLabel}
required={required}
/>
)}

Expand All @@ -458,7 +509,7 @@ class CustomPIDField extends Component {
/>
)}

{canBeUnmanaged && !_isManagedSelected && (
{canBeUnmanaged && (!_isManagedSelected || _isNoNeedSelected) && (
<UnmanagedIdentifierCmp
identifier={unmanagedIdentifier}
onIdentifierChanged={(identifier) => {
Expand All @@ -468,6 +519,7 @@ class CustomPIDField extends Component {
fieldPath={fieldPath}
pidPlaceholder={pidPlaceholder}
helpText={unmanagedHelpText}
disabled={_isNoNeedSelected || isEditingPublishedRecord}
/>
)}
</>
Expand All @@ -493,6 +545,7 @@ CustomPIDField.propTypes = {
required: PropTypes.bool.isRequired,
unmanagedHelpText: PropTypes.string,
record: PropTypes.object.isRequired,
doiDefaultSelection: PropTypes.object.isRequired,
};

CustomPIDField.defaultProps = {
Expand Down Expand Up @@ -542,6 +595,7 @@ PIDField.propTypes = {
required: PropTypes.bool,
unmanagedHelpText: PropTypes.string,
record: PropTypes.object.isRequired,
doiDefaultSelection: PropTypes.object.isRequired,
};

PIDField.defaultProps = {
Expand Down
1 change: 1 addition & 0 deletions invenio_rdm_records/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ def always_valid(identifier):
"validator": idutils.is_doi,
"normalizer": idutils.normalize_doi,
"is_enabled": providers.DataCitePIDProvider.is_enabled,
"ui": {"default_selected": "yes"}, # "yes", "no" or "not_needed"
},
"oai": {
"providers": ["oai"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ def get_related_identifiers(self, obj):
params={"_source_includes": "pids.doi"},
)
for version in record_versions:
version_doi = version["pids"]["doi"]
version_doi = version.get("pids", {}).get("doi")
id_scheme = get_scheme_datacite(
"doi",
"RDM_RECORDS_IDENTIFIERS_SCHEMES",
Expand Down
4 changes: 4 additions & 0 deletions invenio_rdm_records/services/components/pids.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ def publish(self, identity, draft=None, record=None):
current_schemes = set(current_pids.keys())
required_schemes = set(self.service.config.parent_pids_required)

# if parent DOI is not required in the config, but record DOI is created, we need to create parent DOI as well
if "doi" not in required_schemes and draft and draft.get("pids", {}).get("doi"):
required_schemes.add("doi")

conditional_schemes = self.service.config.parent_pids_conditional
for scheme in set(required_schemes):
condition_func = conditional_schemes.get(scheme)
Expand Down

0 comments on commit bf5d9cf

Please sign in to comment.