mirror of
https://github.com/strapi/strapi.git
synced 2025-08-08 00:37:38 +00:00
Init common fields
Signed-off-by: soupette <cyril.lpz@gmail.com>
This commit is contained in:
parent
523250fb3c
commit
ff8b407a3a
@ -10,6 +10,7 @@ import {
|
|||||||
} from 'strapi-helper-plugin';
|
} from 'strapi-helper-plugin';
|
||||||
import { useSelector, useDispatch } from 'react-redux';
|
import { useSelector, useDispatch } from 'react-redux';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
|
import isEqual from 'react-fast-compare';
|
||||||
import { createDefaultForm, getTrad, removePasswordFieldsFromData } from '../../utils';
|
import { createDefaultForm, getTrad, removePasswordFieldsFromData } from '../../utils';
|
||||||
import pluginId from '../../pluginId';
|
import pluginId from '../../pluginId';
|
||||||
import {
|
import {
|
||||||
@ -160,13 +161,13 @@ const CollectionTypeFormWrapper = ({ allLayoutData, children, from, slug, id, or
|
|||||||
if (requestURL) {
|
if (requestURL) {
|
||||||
fetchData(signal);
|
fetchData(signal);
|
||||||
} else {
|
} else {
|
||||||
dispatch(initForm());
|
dispatch(initForm(rawQuery));
|
||||||
}
|
}
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
abortController.abort();
|
abortController.abort();
|
||||||
};
|
};
|
||||||
}, [cleanClonedData, cleanReceivedData, push, requestURL, dispatch]);
|
}, [cleanClonedData, cleanReceivedData, push, requestURL, dispatch, rawQuery]);
|
||||||
|
|
||||||
const displayErrors = useCallback(err => {
|
const displayErrors = useCallback(err => {
|
||||||
const errorPayload = err.response.payload;
|
const errorPayload = err.response.payload;
|
||||||
@ -361,4 +362,4 @@ CollectionTypeFormWrapper.propTypes = {
|
|||||||
slug: PropTypes.string.isRequired,
|
slug: PropTypes.string.isRequired,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default memo(CollectionTypeFormWrapper);
|
export default memo(CollectionTypeFormWrapper, isEqual);
|
||||||
|
@ -19,8 +19,9 @@ export const getDataSucceeded = data => ({
|
|||||||
data,
|
data,
|
||||||
});
|
});
|
||||||
|
|
||||||
export const initForm = () => ({
|
export const initForm = rawQuery => ({
|
||||||
type: INIT_FORM,
|
type: INIT_FORM,
|
||||||
|
rawQuery,
|
||||||
});
|
});
|
||||||
|
|
||||||
export const resetProps = () => ({ type: RESET_PROPS });
|
export const resetProps = () => ({ type: RESET_PROPS });
|
||||||
|
@ -39,6 +39,13 @@ const crudReducer = (state = crudInitialState, action) =>
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case INIT_FORM: {
|
case INIT_FORM: {
|
||||||
|
if (action.data) {
|
||||||
|
draftState.isLoading = false;
|
||||||
|
draftState.data = action.data;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
draftState.isLoading = false;
|
draftState.isLoading = false;
|
||||||
draftState.data = state.contentTypeDataStructure;
|
draftState.data = state.contentTypeDataStructure;
|
||||||
break;
|
break;
|
||||||
|
@ -0,0 +1,59 @@
|
|||||||
|
import get from 'lodash/get';
|
||||||
|
import merge from 'lodash/merge';
|
||||||
|
import cloneDeep from 'lodash/cloneDeep';
|
||||||
|
import { parse } from 'qs';
|
||||||
|
import { request, formatComponentData } from 'strapi-helper-plugin';
|
||||||
|
import pluginId from '../pluginId';
|
||||||
|
|
||||||
|
const addCommonFieldsToInitialDataMiddleware = () => ({ getState, dispatch }) => next => action => {
|
||||||
|
if (action.type !== 'ContentManager/CrudReducer/INIT_FORM') {
|
||||||
|
return next(action);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!action.rawQuery) {
|
||||||
|
return next(action);
|
||||||
|
}
|
||||||
|
|
||||||
|
const search = action.rawQuery.substring(1);
|
||||||
|
const query = parse(search);
|
||||||
|
const relatedEntityId = get(query, 'plugins.i18n.relatedEntityId', null);
|
||||||
|
|
||||||
|
if (!relatedEntityId) {
|
||||||
|
return next(action);
|
||||||
|
}
|
||||||
|
|
||||||
|
const cmDataStore = getState().get('content-manager_editViewCrudReducer');
|
||||||
|
const cmLayoutStore = getState().get('content-manager_editViewLayoutManager');
|
||||||
|
const { contentTypeDataStructure } = cmDataStore;
|
||||||
|
const { currentLayout } = cmLayoutStore;
|
||||||
|
|
||||||
|
const getData = async () => {
|
||||||
|
dispatch({ type: 'ContentManager/CrudReducer/GET_DATA' });
|
||||||
|
|
||||||
|
try {
|
||||||
|
const requestURL = `${pluginId}/content-manager/actions/get-non-localized-fields`;
|
||||||
|
const body = { model: currentLayout.contentType.uid, id: relatedEntityId };
|
||||||
|
|
||||||
|
const { data } = await request(requestURL, { method: 'POST', body });
|
||||||
|
|
||||||
|
const { nonLocalizedFields, localizations } = data;
|
||||||
|
|
||||||
|
const merged = merge(cloneDeep(contentTypeDataStructure, nonLocalizedFields));
|
||||||
|
merged.localizations = localizations;
|
||||||
|
|
||||||
|
action.data = formatComponentData(
|
||||||
|
merged,
|
||||||
|
currentLayout.contentType,
|
||||||
|
currentLayout.components
|
||||||
|
);
|
||||||
|
} catch (err) {
|
||||||
|
// Silent
|
||||||
|
}
|
||||||
|
|
||||||
|
return next(action);
|
||||||
|
};
|
||||||
|
|
||||||
|
return getData();
|
||||||
|
};
|
||||||
|
|
||||||
|
export default addCommonFieldsToInitialDataMiddleware;
|
@ -1,3 +1,4 @@
|
|||||||
|
import addCommonFieldsToInitialDataMiddleware from './addCommonFieldsToInitialDataMiddleware';
|
||||||
import addLocaleToSingleTypesMiddleware from './addLocaleToSingleTypesMiddleware';
|
import addLocaleToSingleTypesMiddleware from './addLocaleToSingleTypesMiddleware';
|
||||||
import extendCMEditViewLayoutMiddleware from './extendCMEditViewLayoutMiddleware';
|
import extendCMEditViewLayoutMiddleware from './extendCMEditViewLayoutMiddleware';
|
||||||
import extendCTBInitialDataMiddleware from './extendCTBInitialDataMiddleware';
|
import extendCTBInitialDataMiddleware from './extendCTBInitialDataMiddleware';
|
||||||
@ -6,6 +7,7 @@ import localeQueryParamsMiddleware from './localeQueryParamsMiddleware';
|
|||||||
import localePermissionMiddleware from './localePermissionMiddleware';
|
import localePermissionMiddleware from './localePermissionMiddleware';
|
||||||
|
|
||||||
const middlewares = [
|
const middlewares = [
|
||||||
|
addCommonFieldsToInitialDataMiddleware,
|
||||||
addLocaleToSingleTypesMiddleware,
|
addLocaleToSingleTypesMiddleware,
|
||||||
extendCMEditViewLayoutMiddleware,
|
extendCMEditViewLayoutMiddleware,
|
||||||
extendCTBInitialDataMiddleware,
|
extendCTBInitialDataMiddleware,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user