From f07dcf3f7f9cd3eb0d48fb013e75923132dcdcd5 Mon Sep 17 00:00:00 2001 From: HichamELBSI Date: Wed, 15 Apr 2020 11:41:08 +0200 Subject: [PATCH 1/2] Fix single types navigation Signed-off-by: HichamELBSI --- .../src/components/FieldComponent/index.js | 9 ++------- .../admin/src/components/InputUID/index.js | 9 +++++---- .../admin/src/components/Inputs/index.js | 1 + .../src/components/RepeatableComponent/index.js | 17 ++++------------- .../admin/src/containers/EditView/index.js | 10 +++++++--- .../EditViewDataManagerProvider/index.js | 4 +++- .../EditViewDataManagerProvider/reducer.js | 12 +++++++++++- .../utils/createDefaultForm.js | 9 +-------- 8 files changed, 34 insertions(+), 37 deletions(-) diff --git a/packages/strapi-plugin-content-manager/admin/src/components/FieldComponent/index.js b/packages/strapi-plugin-content-manager/admin/src/components/FieldComponent/index.js index 1dbc61c02a..1480ac0ae6 100644 --- a/packages/strapi-plugin-content-manager/admin/src/components/FieldComponent/index.js +++ b/packages/strapi-plugin-content-manager/admin/src/components/FieldComponent/index.js @@ -32,13 +32,8 @@ const FieldComponent = ({ const componentValue = get(modifiedData, name, null); const componentValueLength = size(componentValue); const isInitialized = componentValue !== null || isFromDynamicZone; - const showResetComponent = - !isRepeatable && isInitialized && !isFromDynamicZone; - const currentComponentSchema = get( - allLayoutData, - ['components', componentUid], - {} - ); + const showResetComponent = !isRepeatable && isInitialized && !isFromDynamicZone; + const currentComponentSchema = get(allLayoutData, ['components', componentUid], {}); const displayedFields = get(currentComponentSchema, ['layouts', 'edit'], []); diff --git a/packages/strapi-plugin-content-manager/admin/src/components/InputUID/index.js b/packages/strapi-plugin-content-manager/admin/src/components/InputUID/index.js index 8df1fed034..b0f55a2457 100644 --- a/packages/strapi-plugin-content-manager/admin/src/components/InputUID/index.js +++ b/packages/strapi-plugin-content-manager/admin/src/components/InputUID/index.js @@ -61,7 +61,7 @@ const InputUID = ({ const initialValue = initialData[name]; const isCreation = isEmpty(initialData); - generateUid.current = async () => { + generateUid.current = async (changeInitialData = false) => { setIsLoading(true); const requestURL = getRequestUrl('explorer/uid/generate'); try { @@ -73,7 +73,8 @@ const InputUID = ({ data: modifiedData, }, }); - onChange({ target: { name, value: data, type: 'text' } }); + + onChange({ target: { name, value: data, type: 'text' } }, changeInitialData); setIsLoading(false); } catch (err) { console.error({ err }); @@ -107,7 +108,7 @@ const InputUID = ({ useEffect(() => { if (!value && required) { - generateUid.current(); + generateUid.current(true); } // eslint-disable-next-line react-hooks/exhaustive-deps }, []); @@ -143,7 +144,7 @@ const InputUID = ({ }, [availability]); useEffect(() => { - if (!isCustomized && isCreation && debouncedTargetFieldValue !== null) { + if (!isCustomized && isCreation && debouncedTargetFieldValue) { generateUid.current(); } }, [debouncedTargetFieldValue, isCustomized, isCreation]); diff --git a/packages/strapi-plugin-content-manager/admin/src/components/Inputs/index.js b/packages/strapi-plugin-content-manager/admin/src/components/Inputs/index.js index bfe449b3da..35807ba65d 100644 --- a/packages/strapi-plugin-content-manager/admin/src/components/Inputs/index.js +++ b/packages/strapi-plugin-content-manager/admin/src/components/Inputs/index.js @@ -184,6 +184,7 @@ function Inputs({ autoFocus, keys, layout, name, onBlur }) { validations={validations} value={inputValue} withDefaultValue={false} + required={isRequired} /> ); }} diff --git a/packages/strapi-plugin-content-manager/admin/src/components/RepeatableComponent/index.js b/packages/strapi-plugin-content-manager/admin/src/components/RepeatableComponent/index.js index 7a23c813e8..235215e487 100644 --- a/packages/strapi-plugin-content-manager/admin/src/components/RepeatableComponent/index.js +++ b/packages/strapi-plugin-content-manager/admin/src/components/RepeatableComponent/index.js @@ -53,13 +53,10 @@ const RepeatableComponent = ({ }); }; const missingComponentsValue = min - componentValueLength; - const errorsArray = componentErrorKeys.map(key => - get(formErrors, [key, 'id'], '') - ); + const errorsArray = componentErrorKeys.map(key => get(formErrors, [key, 'id'], '')); const hasMinError = - get(errorsArray, [0], '').includes('min') && - !collapses.some(obj => obj.isOpen === true); + get(errorsArray, [0], '').includes('min') && !collapses.some(obj => obj.isOpen === true); return (
@@ -84,9 +81,7 @@ const RepeatableComponent = ({ { +const EditView = ({ components, currentEnvironment, deleteLayout, layouts, plugins, slug }) => { const formatLayoutRef = useRef(); formatLayoutRef.current = createAttributesLayout; // Retrieve push to programmatically navigate between views @@ -95,6 +95,9 @@ const EditView = ({ components, currentEnvironment, layouts, plugins, slug }) => currentContentTypeSchema.attributes ), }); + + return () => deleteLayout(slug); + // eslint-disable-next-line react-hooks/exhaustive-deps }, [currentContentTypeLayout, currentContentTypeSchema.attributes]); const { formattedContentTypeLayout, isDraggingComponent } = reducerState.toJS(); @@ -265,12 +268,13 @@ EditView.defaultProps = { }; EditView.propTypes = { - currentEnvironment: PropTypes.string, components: PropTypes.array.isRequired, + currentEnvironment: PropTypes.string, + deleteLayout: PropTypes.func.isRequired, emitEvent: PropTypes.func, layouts: PropTypes.object.isRequired, - slug: PropTypes.string.isRequired, plugins: PropTypes.object, + slug: PropTypes.string.isRequired, }; export { EditView }; diff --git a/packages/strapi-plugin-content-manager/admin/src/containers/EditViewDataManagerProvider/index.js b/packages/strapi-plugin-content-manager/admin/src/containers/EditViewDataManagerProvider/index.js index 7d9d127f60..fffc28f3cc 100644 --- a/packages/strapi-plugin-content-manager/admin/src/containers/EditViewDataManagerProvider/index.js +++ b/packages/strapi-plugin-content-manager/admin/src/containers/EditViewDataManagerProvider/index.js @@ -77,6 +77,7 @@ const EditViewDataManagerProvider = ({ allLayoutData, children, redirectToPrevio return acc; }, {}); + const contentTypeDataStructure = createDefaultForm( currentContentTypeLayout.schema.attributes, allLayoutData.components @@ -181,7 +182,7 @@ const EditViewDataManagerProvider = ({ allLayoutData, children, redirectToPrevio }); }; - const handleChange = ({ target: { name, value, type } }) => { + const handleChange = ({ target: { name, value, type } }, initialValue = false) => { let inputValue = value; // Empty string is not a valid date, @@ -213,6 +214,7 @@ const EditViewDataManagerProvider = ({ allLayoutData, children, redirectToPrevio type: 'ON_CHANGE', keys: name.split('.'), value: inputValue, + initialValue, }); }; diff --git a/packages/strapi-plugin-content-manager/admin/src/containers/EditViewDataManagerProvider/reducer.js b/packages/strapi-plugin-content-manager/admin/src/containers/EditViewDataManagerProvider/reducer.js index 9978d48344..399f0fb1ab 100644 --- a/packages/strapi-plugin-content-manager/admin/src/containers/EditViewDataManagerProvider/reducer.js +++ b/packages/strapi-plugin-content-manager/admin/src/containers/EditViewDataManagerProvider/reducer.js @@ -135,11 +135,21 @@ const reducer = (state, action) => { let newState = state; const [nonRepeatableComponentKey] = action.keys; + // This is used to set the initialData for inputs + // that needs an asynchronous initial value like the UID field + // This is just a temporary patch. + // TODO : Refactor the default form creation (workflow) to accept async default values. + if (action.initialValue) { + newState = state.updateIn(['initialData', ...action.keys], () => { + return action.value; + }); + } + if ( action.keys.length === 2 && state.getIn(['modifiedData', nonRepeatableComponentKey]) === null ) { - newState = state.updateIn(['modifiedData', nonRepeatableComponentKey], () => fromJS({})); + newState = newState.updateIn(['modifiedData', nonRepeatableComponentKey], () => fromJS({})); } return newState.updateIn(['modifiedData', ...action.keys], () => { diff --git a/packages/strapi-plugin-content-manager/admin/src/containers/EditViewDataManagerProvider/utils/createDefaultForm.js b/packages/strapi-plugin-content-manager/admin/src/containers/EditViewDataManagerProvider/utils/createDefaultForm.js index 317bcc8ffc..5e829c34c4 100644 --- a/packages/strapi-plugin-content-manager/admin/src/containers/EditViewDataManagerProvider/utils/createDefaultForm.js +++ b/packages/strapi-plugin-content-manager/admin/src/containers/EditViewDataManagerProvider/utils/createDefaultForm.js @@ -3,14 +3,7 @@ import { get } from 'lodash'; const createDefaultForm = (attributes, allComponentsSchema) => { return Object.keys(attributes).reduce((acc, current) => { const attribute = get(attributes, [current], {}); - const { - default: defaultValue, - component, - type, - required, - min, - repeatable, - } = attribute; + const { default: defaultValue, component, type, required, min, repeatable } = attribute; if (type === 'json') { acc[current] = null; From 4079ecbaf165fa540cae3241002e031db07cdc8c Mon Sep 17 00:00:00 2001 From: HichamELBSI Date: Fri, 24 Apr 2020 09:39:18 +0200 Subject: [PATCH 2/2] Fix PR review Signed-off-by: HichamELBSI --- .../admin/src/components/InputUID/index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/strapi-plugin-content-manager/admin/src/components/InputUID/index.js b/packages/strapi-plugin-content-manager/admin/src/components/InputUID/index.js index fea38c567f..4fe0a78108 100644 --- a/packages/strapi-plugin-content-manager/admin/src/components/InputUID/index.js +++ b/packages/strapi-plugin-content-manager/admin/src/components/InputUID/index.js @@ -58,7 +58,7 @@ const InputUID = ({ const wrapperRef = useRef(null); const generateUid = useRef(); const initialValue = initialData[name]; - const createdAtName = get(layout, ['schema', 'options', 'timestamps'], ['created_at'])[0]; + const createdAtName = get(layout, ['schema', 'options', 'timestamps', 0]); const isCreation = !initialData[createdAtName]; generateUid.current = async (shouldSetInitialValue = false) => { @@ -271,7 +271,6 @@ InputUID.propTypes = { error: PropTypes.string, name: PropTypes.string.isRequired, onChange: PropTypes.func.isRequired, - required: PropTypes.bool, validations: PropTypes.object, value: PropTypes.string, }; @@ -280,7 +279,6 @@ InputUID.defaultProps = { description: '', editable: false, error: null, - required: false, validations: {}, value: '', };