Split open modal creates method

Signed-off-by: soupette <cyril@strapi.io>
This commit is contained in:
soupette 2021-10-18 08:58:11 +02:00
parent 8a71cd6491
commit d0f6dcad75
4 changed files with 113 additions and 31 deletions

View File

@ -1,9 +1,12 @@
import { useState } from 'react'; import { useState } from 'react';
import { useNotification, useTracking } from '@strapi/helper-plugin'; import { useNotification, useTracking } from '@strapi/helper-plugin';
import { camelCase, isEmpty, sortBy, toLower, upperFirst } from 'lodash'; import isEmpty from 'lodash/isEmpty';
import sortBy from 'lodash/sortBy';
import toLower from 'lodash/toLower';
import matchSorter from 'match-sorter'; import matchSorter from 'match-sorter';
import { useHistory } from 'react-router-dom'; import { useHistory } from 'react-router-dom';
import useDataManager from '../../hooks/useDataManager'; import useDataManager from '../../hooks/useDataManager';
import useFormModalNavigation from '../../hooks/useFormModalNavigation';
import pluginId from '../../pluginId'; import pluginId from '../../pluginId';
import getTrad from '../../utils/getTrad'; import getTrad from '../../utils/getTrad';
import makeSearch from '../../utils/makeSearch'; import makeSearch from '../../utils/makeSearch';
@ -20,6 +23,7 @@ const useContentTypeBuilderMenu = () => {
const { trackUsage } = useTracking(); const { trackUsage } = useTracking();
const { push } = useHistory(); const { push } = useHistory();
const [search, setSearch] = useState(''); const [search, setSearch] = useState('');
const { onOpenModal } = useFormModalNavigation();
const componentsData = sortBy( const componentsData = sortBy(
Object.keys(componentsGroupedByCategory).map(category => ({ Object.keys(componentsGroupedByCategory).map(category => ({
@ -50,38 +54,105 @@ const useContentTypeBuilderMenu = () => {
obj => obj.title obj => obj.title
); );
const canOpenModalCreateCTorComponent = () => { const canOpenModalCreateCTorComponent =
return ( !Object.keys(contentTypes).some(ct => contentTypes[ct].isTemporary === true) &&
!Object.keys(contentTypes).some(ct => contentTypes[ct].isTemporary === true) && !Object.keys(components).some(component => components[component].isTemporary === true);
!Object.keys(components).some(component => components[component].isTemporary === true)
); const toggleNotificationCannotCreateSchema = () => {
toggleNotification({
type: 'info',
message: {
id: `${getTrad('notification.info.creating.notSaved')}`,
defaultMessage: 'Please save your work before creating a new collection type or component',
},
});
}; };
const handleClickOpenModal = async (modalType, kind = '') => { const handleClickOpenModalCreateCollectionType = () => {
const type = kind === 'singleType' ? kind : modalType; if (canOpenModalCreateCTorComponent) {
trackUsage(`willCreateContentType`);
if (canOpenModalCreateCTorComponent()) { const nextState = {
trackUsage(`willCreate${upperFirst(camelCase(type))}`); modalType: 'contentType',
kind: 'collectionType',
const search = makeSearch({
modalType,
kind,
actionType: 'create', actionType: 'create',
settingType: 'base', settingType: 'base',
forTarget: modalType, forTarget: 'contentType',
}); };
onOpenModal(nextState);
// FIXME: to remove
const search = makeSearch(nextState);
push({ push({
search, search,
}); });
} else { } else {
toggleNotification({ toggleNotificationCannotCreateSchema();
type: 'info', }
message: { };
id: `${getTrad('notification.info.creating.notSaved')}`,
defaultMessage: const handleClickOpenModalCreateSingleType = () => {
'Please save your work before creating a new collection type or component', if (canOpenModalCreateCTorComponent) {
}, trackUsage(`willCreateSingleType`);
const nextState = {
modalType: 'contentType',
kind: 'singleType',
actionType: 'create',
settingType: 'base',
forTarget: 'contentType',
};
const search = makeSearch(nextState);
onOpenModal(nextState);
push({
search,
}); });
} else {
toggleNotificationCannotCreateSchema();
}
};
// FIXME : open modal edit
// const handleClickOpenModalEditCategory = categoryName => {
// if (canOpenModalCreateCTorComponent) {
// const search = makeSearch({
// actionType: 'edit',
// modalType: 'editCategory',
// categoryName,
// settingType: 'base',
// });
// push({ search });
// } else {
// toggleNotificationCannotCreateSchema();
// }
// };
const handleClickOpenModalCreateComponent = () => {
if (canOpenModalCreateCTorComponent) {
trackUsage('willCreateComponent');
const nextState = {
modalType: 'component',
kind: null,
actionType: 'create',
settingType: 'base',
forTarget: 'component',
};
onOpenModal(nextState);
const search = makeSearch(nextState);
push({
search,
});
} else {
toggleNotificationCannotCreateSchema();
} }
}; };
@ -97,7 +168,7 @@ const useContentTypeBuilderMenu = () => {
customLink: isInDevelopmentMode && { customLink: isInDevelopmentMode && {
id: `${getTrad('button.model.create')}`, id: `${getTrad('button.model.create')}`,
defaultMessage: 'Create new collection type', defaultMessage: 'Create new collection type',
onClick: () => handleClickOpenModal('contentType', 'collectionType'), onClick: handleClickOpenModalCreateCollectionType,
}, },
links: displayedContentTypes.filter(contentType => contentType.kind === 'collectionType'), links: displayedContentTypes.filter(contentType => contentType.kind === 'collectionType'),
}, },
@ -110,7 +181,7 @@ const useContentTypeBuilderMenu = () => {
customLink: isInDevelopmentMode && { customLink: isInDevelopmentMode && {
id: `${getTrad('button.single-types.create')}`, id: `${getTrad('button.single-types.create')}`,
defaultMessage: 'Create new single type', defaultMessage: 'Create new single type',
onClick: () => handleClickOpenModal('contentType', 'singleType'), onClick: handleClickOpenModalCreateSingleType,
}, },
links: displayedContentTypes.filter(singleType => singleType.kind === 'singleType'), links: displayedContentTypes.filter(singleType => singleType.kind === 'singleType'),
}, },
@ -123,7 +194,7 @@ const useContentTypeBuilderMenu = () => {
customLink: { customLink: {
id: `${getTrad('button.component.create')}`, id: `${getTrad('button.component.create')}`,
defaultMessage: 'Create a new component', defaultMessage: 'Create a new component',
onClick: () => handleClickOpenModal('component'), onClick: handleClickOpenModalCreateComponent,
}, },
links: componentsData, links: componentsData,
}, },

View File

@ -71,9 +71,7 @@ import {
const FormModal = () => { const FormModal = () => {
const [state, setState] = useState(INITIAL_STATE_DATA); const [state, setState] = useState(INITIAL_STATE_DATA);
const c = useFormModalNavigation(); const { onCloseModal } = useFormModalNavigation();
console.log(c);
const formModalSelector = useMemo(makeSelectFormModal, []); const formModalSelector = useMemo(makeSelectFormModal, []);
const dispatch = useDispatch(); const dispatch = useDispatch();
@ -128,6 +126,7 @@ const FormModal = () => {
const dynamicZoneTarget = query.get('dynamicZoneTarget'); const dynamicZoneTarget = query.get('dynamicZoneTarget');
const forTarget = query.get('forTarget'); const forTarget = query.get('forTarget');
const modalType = query.get('modalType'); const modalType = query.get('modalType');
// FIXME
const kind = query.get('kind') || get(allDataSchema, ['contentType', 'schema', 'kind'], null); const kind = query.get('kind') || get(allDataSchema, ['contentType', 'schema', 'kind'], null);
const targetUid = query.get('targetUid'); const targetUid = query.get('targetUid');
const settingType = query.get('settingType'); const settingType = query.get('settingType');
@ -898,6 +897,7 @@ const FormModal = () => {
}; };
const handleClosed = () => { const handleClosed = () => {
onCloseModal();
// Close the modal // Close the modal
push({ search: '' }); push({ search: '' });
// Reset the state // Reset the state

View File

@ -8,7 +8,6 @@ const INITIAL_STATE_DATA = {
modalType: null, modalType: null,
isOpen: false, isOpen: false,
kind: null, kind: null,
pathToSchema: [],
settingType: null, settingType: null,
step: null, step: null,
targetUid: null, targetUid: null,

View File

@ -6,8 +6,20 @@ import { INITIAL_STATE_DATA } from './constants';
const FormModalNavigationProvider = ({ children }) => { const FormModalNavigationProvider = ({ children }) => {
const [state, setFormModalNavigationState] = useState(INITIAL_STATE_DATA); const [state, setFormModalNavigationState] = useState(INITIAL_STATE_DATA);
const onOpenModal = nextState => {
setFormModalNavigationState(prevState => {
return { ...prevState, ...nextState, isOpen: true };
});
};
const onCloseModal = () => {
setFormModalNavigationState(INITIAL_STATE_DATA);
};
return ( return (
<FormModalNavigationContext.Provider value={{ ...state, setFormModalNavigationState }}> <FormModalNavigationContext.Provider
value={{ ...state, onCloseModal, onOpenModal, setFormModalNavigationState }}
>
{children} {children}
</FormModalNavigationContext.Provider> </FormModalNavigationContext.Provider>
); );