mirror of
https://github.com/strapi/strapi.git
synced 2025-08-07 16:29:18 +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';
|
||||
import { useSelector, useDispatch } from 'react-redux';
|
||||
import PropTypes from 'prop-types';
|
||||
import isEqual from 'react-fast-compare';
|
||||
import { createDefaultForm, getTrad, removePasswordFieldsFromData } from '../../utils';
|
||||
import pluginId from '../../pluginId';
|
||||
import {
|
||||
@ -160,13 +161,13 @@ const CollectionTypeFormWrapper = ({ allLayoutData, children, from, slug, id, or
|
||||
if (requestURL) {
|
||||
fetchData(signal);
|
||||
} else {
|
||||
dispatch(initForm());
|
||||
dispatch(initForm(rawQuery));
|
||||
}
|
||||
|
||||
return () => {
|
||||
abortController.abort();
|
||||
};
|
||||
}, [cleanClonedData, cleanReceivedData, push, requestURL, dispatch]);
|
||||
}, [cleanClonedData, cleanReceivedData, push, requestURL, dispatch, rawQuery]);
|
||||
|
||||
const displayErrors = useCallback(err => {
|
||||
const errorPayload = err.response.payload;
|
||||
@ -361,4 +362,4 @@ CollectionTypeFormWrapper.propTypes = {
|
||||
slug: PropTypes.string.isRequired,
|
||||
};
|
||||
|
||||
export default memo(CollectionTypeFormWrapper);
|
||||
export default memo(CollectionTypeFormWrapper, isEqual);
|
||||
|
@ -19,8 +19,9 @@ export const getDataSucceeded = data => ({
|
||||
data,
|
||||
});
|
||||
|
||||
export const initForm = () => ({
|
||||
export const initForm = rawQuery => ({
|
||||
type: INIT_FORM,
|
||||
rawQuery,
|
||||
});
|
||||
|
||||
export const resetProps = () => ({ type: RESET_PROPS });
|
||||
|
@ -39,6 +39,13 @@ const crudReducer = (state = crudInitialState, action) =>
|
||||
break;
|
||||
}
|
||||
case INIT_FORM: {
|
||||
if (action.data) {
|
||||
draftState.isLoading = false;
|
||||
draftState.data = action.data;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
draftState.isLoading = false;
|
||||
draftState.data = state.contentTypeDataStructure;
|
||||
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 extendCMEditViewLayoutMiddleware from './extendCMEditViewLayoutMiddleware';
|
||||
import extendCTBInitialDataMiddleware from './extendCTBInitialDataMiddleware';
|
||||
@ -6,6 +7,7 @@ import localeQueryParamsMiddleware from './localeQueryParamsMiddleware';
|
||||
import localePermissionMiddleware from './localePermissionMiddleware';
|
||||
|
||||
const middlewares = [
|
||||
addCommonFieldsToInitialDataMiddleware,
|
||||
addLocaleToSingleTypesMiddleware,
|
||||
extendCMEditViewLayoutMiddleware,
|
||||
extendCTBInitialDataMiddleware,
|
||||
|
Loading…
x
Reference in New Issue
Block a user