Skip to content

Commit

Permalink
message @Publishing + hook for showing empty value
Browse files Browse the repository at this point in the history
  • Loading branch information
Ducica committed Nov 8, 2023
1 parent 0323f70 commit ea1d182
Showing 1 changed file with 38 additions and 12 deletions.
50 changes: 38 additions & 12 deletions oarepo_ui/theme/assets/semantic-ui/js/oarepo_ui/forms/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ import { useFormikContext, getIn } from "formik";
import _omit from "lodash/omit";
import _pick from "lodash/pick";
import _isEmpty from "lodash/isEmpty";
import _isObject from "lodash/isObject";
import { i18next } from "@translations/oarepo_ui/i18next";
import { relativeUrl } from "../util";

const extractFEErrorMessages = (obj) => {
const errorMessages = [];

const traverse = (obj) => {
for (const key in obj) {
if (typeof obj[key] === "string") {
errorMessages.push(obj[key]);
} else if (Array.isArray(obj[key])) {
obj[key].forEach((item) => traverse(item));
} else if (typeof obj[key] === "object") {
if (typeof obj === "string") {
errorMessages.push(obj);
} else if (Array.isArray(obj)) {
obj.forEach((item) => traverse(item));
} else if (typeof obj === "object") {
for (const key in obj) {
traverse(obj[key]);
}
}
Expand Down Expand Up @@ -65,11 +66,28 @@ export const useShowEmptyValue = (
React.useEffect(() => {
if (!showEmptyValue) return;
if (!_isEmpty(currentFieldValue)) return;
currentFieldValue.push({
__key: currentFieldValue.length,
...defaultNewValue,
});
setFieldValue(fieldPath, currentFieldValue);
if (defaultNewValue === undefined) {
console.error(
"Default value for new input must be provided. Field: ",
fieldPath
);
return;
}
if (!fieldPath) {
console.error("Fieldpath must be provided");
return;
}
// to be used with invenio array fields that always push objects and add the __key property
if (!_isEmpty(defaultNewValue) && _isObject(defaultNewValue)) {
currentFieldValue.push({
__key: currentFieldValue.length,
...defaultNewValue,
});
setFieldValue(fieldPath, currentFieldValue);
} else if (typeof defaultNewValue === "string") {
currentFieldValue.push(defaultNewValue);
setFieldValue(fieldPath, currentFieldValue);
}
}, [showEmptyValue, setFieldValue, fieldPath, defaultNewValue]);
};

Expand Down Expand Up @@ -172,7 +190,15 @@ export const useDepositApiClient = (
async function publish() {
// call save and if save returns false, exit
const saveResult = await save();
if (!saveResult) return;
if (!saveResult) {
setFieldValue(
"BEvalidationErrors.errorMessage",
i18next.t(
"Draft was saved but could not be published due to following validation errors"
)
);
return;
}
// imperative form validation, if fails exit
const FEvalidationErrors = await validateForm();
// show also front end validation errors grouped on the top similar to BE validation errors for consistency
Expand Down

0 comments on commit ea1d182

Please sign in to comment.