EditViewDataManagerProvider: Reuse getYupInnerErrors from helper-plugin

This commit is contained in:
Gustav Hansen 2022-03-28 19:28:56 +02:00
parent 538c1f1ac4
commit 3177ddda35
4 changed files with 12 additions and 32 deletions

View File

@ -14,11 +14,12 @@ import {
useNotification,
useOverlayBlocker,
useTracking,
getYupInnerErrors,
} from '@strapi/helper-plugin';
import { getTrad, removeKeyInObject } from '../../utils';
import reducer, { initialState } from './reducer';
import { cleanData, createYupSchema, getYupInnerErrors } from './utils';
import { cleanData, createYupSchema } from './utils';
import { getAPIInnerError } from './utils/getAPIInnerError';
const EditViewDataManagerProvider = ({
@ -268,7 +269,7 @@ const EditViewDataManagerProvider = ({
);
const createFormData = useCallback(
data => {
(data) => {
// First we need to remove the added keys needed for the dnd
const preparedData = removeKeyInObject(cloneDeep(data), '__temp_key__');
// Then we need to apply our helper
@ -292,7 +293,7 @@ const EditViewDataManagerProvider = ({
}, [hasDraftAndPublish, shouldNotRunValidations]);
const handleSubmit = useCallback(
async e => {
async (e) => {
e.preventDefault();
let errors = {};
@ -362,8 +363,8 @@ const EditViewDataManagerProvider = ({
}, [allLayoutData, currentContentTypeLayout, isCreatingEntry, modifiedData, onPublish]);
const shouldCheckDZErrors = useCallback(
dzName => {
const doesDZHaveError = Object.keys(formErrors).some(key => key.split('.')[0] === dzName);
(dzName) => {
const doesDZHaveError = Object.keys(formErrors).some((key) => key.split('.')[0] === dzName);
const shouldCheckErrors = !isEmpty(formErrors) && doesDZHaveError;
return shouldCheckErrors;
@ -417,7 +418,7 @@ const EditViewDataManagerProvider = ({
});
}, []);
const onRemoveRelation = useCallback(keys => {
const onRemoveRelation = useCallback((keys) => {
dispatch({
type: 'REMOVE_RELATION',
keys,

View File

@ -1,17 +0,0 @@
import { get } from 'lodash';
const getYupInnerErrors = error => {
return get(error, 'inner', []).reduce((acc, curr) => {
acc[
curr.path
.split('[')
.join('.')
.split(']')
.join('')
] = { id: curr.message };
return acc;
}, {});
};
export default getYupInnerErrors;

View File

@ -1,4 +1,3 @@
export { default as moveFields } from './moveFields';
export { default as cleanData } from './cleanData';
export { default as getYupInnerErrors } from './getYupInnerErrors';
export { default as createYupSchema } from './schema';

View File

@ -1,14 +1,11 @@
import { get } from 'lodash';
const getYupInnerErrors = error => {
const getYupInnerErrors = (error) => {
return get(error, 'inner', []).reduce((acc, curr) => {
acc[
curr.path
.split('[')
.join('.')
.split(']')
.join('')
] = { id: curr.message };
acc[curr.path.split('[').join('.').split(']').join('')] = {
id: curr.message,
defaultMessage: curr.message,
};
return acc;
}, {});