Skip to content

Commit

Permalink
deposit form: report invalid value errors on each draft save
Browse files Browse the repository at this point in the history
  • Loading branch information
anikachurilova authored and zzacharo committed Oct 6, 2023
1 parent 8754b5c commit fdd6e9f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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:
Expand Down

0 comments on commit fdd6e9f

Please sign in to comment.