From fdd6e9f6249d5c8a87a2d7707f32b4c2f48c01d0 Mon Sep 17 00:00:00 2001 From: anikachurilova Date: Thu, 5 Oct 2023 16:24:06 +0200 Subject: [PATCH] deposit form: report invalid value errors on each draft save * closes https://github.com/zenodo/rdm-project/issues/244 --- .../src/deposit/state/actions/deposit.js | 16 ++++++++++++++++ .../src/deposit/state/reducers/deposit.js | 18 ++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/invenio_rdm_records/assets/semantic-ui/js/invenio_rdm_records/src/deposit/state/actions/deposit.js b/invenio_rdm_records/assets/semantic-ui/js/invenio_rdm_records/src/deposit/state/actions/deposit.js index e763e0343..53f11fa44 100644 --- a/invenio_rdm_records/assets/semantic-ui/js/invenio_rdm_records/src/deposit/state/actions/deposit.js +++ b/invenio_rdm_records/assets/semantic-ui/js/invenio_rdm_records/src/deposit/state/actions/deposit.js @@ -6,6 +6,7 @@ // under the terms of the MIT License; see LICENSE file for more details. import _isEmpty from "lodash/isEmpty"; +import _ from "lodash"; import { DISCARD_PID_FAILED, DISCARD_PID_STARTED, @@ -38,6 +39,21 @@ async function changeURLAfterCreation(draftURL) { export const saveDraftWithUrlUpdate = async (draft, draftsService) => { const hasAlreadyId = !!draft.id; const response = await draftsService.save(draft); + + const draftHasValidationErrors = !_isEmpty(response.errors); + + // In case of invalid values, on the second draft save, the form doesn't report the errors. This happens + // because the backend doesn't save invalid metadata. Here we are merging draft state with backend + // response in order not to lose those invalid values from the form state and have the errors reported. + if (draftHasValidationErrors) { + const mergingValues = { + metadata: draft.metadata, + custom_fields: draft.custom_fields, + }; + + response.data = _.merge(response.data, mergingValues); + } + if (!hasAlreadyId) { // draft was created, change URL to add the draft PID const draftURL = response.data.links.self_html; diff --git a/invenio_rdm_records/assets/semantic-ui/js/invenio_rdm_records/src/deposit/state/reducers/deposit.js b/invenio_rdm_records/assets/semantic-ui/js/invenio_rdm_records/src/deposit/state/reducers/deposit.js index dff21b6d7..d910ece74 100644 --- a/invenio_rdm_records/assets/semantic-ui/js/invenio_rdm_records/src/deposit/state/reducers/deposit.js +++ b/invenio_rdm_records/assets/semantic-ui/js/invenio_rdm_records/src/deposit/state/reducers/deposit.js @@ -253,8 +253,6 @@ const depositReducer = (state = {}, action) => { actionState: action.type, actionStateExtra: { pidType: action.payload.pidType }, }; - case DRAFT_FETCHED: - case DRAFT_SAVE_SUCCEEDED: case RESERVE_PID_SUCCEEDED: case DISCARD_PID_SUCCEEDED: return { @@ -271,6 +269,22 @@ const depositReducer = (state = {}, action) => { actionState: action.type, actionStateExtra: {}, }; + case DRAFT_FETCHED: + case DRAFT_SAVE_SUCCEEDED: + return { + ...state, + record: { + // populate record only with fresh backend response + ...action.payload.data, + }, + editorState: computeDepositState( + action.payload.data, + state.editorState.selectedCommunity + ), + errors: {}, + actionState: action.type, + actionStateExtra: {}, + }; case DRAFT_HAS_VALIDATION_ERRORS: case DRAFT_PUBLISH_FAILED_WITH_VALIDATION_ERRORS: case DRAFT_SUBMIT_REVIEW_FAILED_WITH_VALIDATION_ERRORS: