From af8b17da1b83c8f6ac41b456684f174acb205f20 Mon Sep 17 00:00:00 2001 From: soupette Date: Thu, 14 Nov 2019 19:11:45 +0100 Subject: [PATCH] First step of ct creation --- .../containers/DataManagerProvider/index.js | 14 ++++++++++- .../containers/DataManagerProvider/reducer.js | 16 +++++++++++++ .../admin/src/containers/FormModal/index.js | 7 +++++- .../admin/src/containers/LeftMenu/index.js | 24 +++++++++++++------ 4 files changed, 52 insertions(+), 9 deletions(-) diff --git a/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/index.js b/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/index.js index 653ce32fbf..dd34c5d973 100644 --- a/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/index.js +++ b/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/index.js @@ -18,6 +18,7 @@ const DataManagerProvider = ({ children }) => { isLoading, initialData, modifiedData, + newSchema, } = reducerState.toJS(); const contentTypeMatch = useRouteMatch( @@ -60,6 +61,15 @@ const DataManagerProvider = ({ children }) => { getDataRef.current(); }, []); + const createSchema = (data, schemaType, uid) => { + dispatch({ + type: 'CREATE_SCHEMA', + data, + schemaType, + uid, + }); + }; + const setModifiedData = () => { const currentSchemas = isInContentTypeView ? contentTypes : components; const schemaToSet = get(currentSchemas, currentUid, {}); @@ -70,15 +80,17 @@ const DataManagerProvider = ({ children }) => { }); }; - console.log({ contentTypes }); + console.log({ contentTypes, components }); return ( diff --git a/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/reducer.js b/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/reducer.js index e3f76f21d4..a7b295bb86 100644 --- a/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/reducer.js +++ b/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/reducer.js @@ -6,6 +6,16 @@ const initialState = fromJS({ initialData: {}, modifiedData: {}, isLoading: true, + newSchema: { + schemaType: '', + schema: {}, + uid: '', + }, + newSchemaClone: { + schemaType: '', + schema: {}, + uid: '', + }, }); const reducer = (state, action) => { @@ -15,6 +25,12 @@ const reducer = (state, action) => { .update('components', () => fromJS(action.components)) .update('contentTypes', () => fromJS(action.contentTypes)) .update('isLoading', () => false); + case 'CREATE_SCHEMA': + console.log({ action }); + return state + .updateIn(['newSchema', 'schema'], () => fromJS(action.data)) + .updateIn(['newSchema', 'uid'], () => fromJS(action.uid)) + .updateIn(['newSchema', 'schemaType'], () => fromJS(action.schemaType)); case 'SET_MODIFIED_DATA': return state .update('initialData', () => OrderedMap(action.schemaToSet)) diff --git a/packages/strapi-plugin-content-type-builder/admin/src/containers/FormModal/index.js b/packages/strapi-plugin-content-type-builder/admin/src/containers/FormModal/index.js index 78eb74d9db..2df0eb8ae8 100644 --- a/packages/strapi-plugin-content-type-builder/admin/src/containers/FormModal/index.js +++ b/packages/strapi-plugin-content-type-builder/admin/src/containers/FormModal/index.js @@ -22,6 +22,7 @@ import ModalHeader from '../../components/ModalHeader'; import HeaderModalNavContainer from '../../components/HeaderModalNavContainer'; import HeaderNavLink from '../../components/HeaderNavLink'; import forms from './utils/forms'; +import { createUid } from './utils/createUid'; import init from './init'; import reducer, { initialState } from './reducer'; @@ -42,7 +43,7 @@ const FormModal = () => { const { formatMessage } = useGlobalContext(); const isOpen = !isEmpty(search); const query = useQuery(); - const { contentTypes, initialData } = useDataManager(); + const { contentTypes, createSchema, initialData } = useDataManager(); const { formErrors, modifiedData } = reducerState.toJS(); useEffect(() => { @@ -89,6 +90,9 @@ const FormModal = () => { const schema = forms.contentType.schema(Object.keys(contentTypes)); await schema.validate(modifiedData, { abortEarly: false }); + createSchema(modifiedData, state.modalType, createUid(modifiedData.name)); + handleToggle(); + // push({ p}) } catch (err) { const errors = getYupInnerErrors(err); // TODO @@ -175,6 +179,7 @@ const FormModal = () => { : formatMessage({ id: errorId }) } onChange={handleChange} + onBlur={() => {}} description={ get(input, 'description.id', null) ? formatMessage(input.description) diff --git a/packages/strapi-plugin-content-type-builder/admin/src/containers/LeftMenu/index.js b/packages/strapi-plugin-content-type-builder/admin/src/containers/LeftMenu/index.js index bcae303d09..e2c7bf521c 100644 --- a/packages/strapi-plugin-content-type-builder/admin/src/containers/LeftMenu/index.js +++ b/packages/strapi-plugin-content-type-builder/admin/src/containers/LeftMenu/index.js @@ -20,7 +20,7 @@ import Wrapper from './Wrapper'; // }; function LeftMenu() { - const { components, contentTypes } = useDataManager(); + const { components, contentTypes, newSchema } = useDataManager(); const { currentEnvironment } = useGlobalContext(); const { push } = useHistory(); const isProduction = currentEnvironment === 'production'; @@ -40,7 +40,14 @@ function LeftMenu() { })), obj => obj.title ); - + const tempSchemaCT = + newSchema.schemaType === 'contentType' + ? { + name: newSchema.uid, + title: newSchema.schema.name, + to: `/plugins/${pluginId}/content-types/${newSchema.uid}`, + } + : null; const data = [ { name: 'models', @@ -62,11 +69,14 @@ function LeftMenu() { }, }, links: sortBy( - Object.keys(contentTypes).map(uid => ({ - name: uid, - title: contentTypes[uid].schema.name, - to: `/plugins/${pluginId}/content-types/${uid}`, - })), + Object.keys(contentTypes) + .map(uid => ({ + name: uid, + title: contentTypes[uid].schema.name, + to: `/plugins/${pluginId}/content-types/${uid}`, + })) + .concat(tempSchemaCT) + .filter(obj => obj !== null), obj => obj.title ), },