Fix relation display

Signed-off-by: soupette <cyril.lpz@gmail.com>
This commit is contained in:
soupette 2020-09-17 16:04:44 +02:00 committed by Pierre Noël
parent 7221412d68
commit b6e6f7cbbd
9 changed files with 105 additions and 23 deletions

View File

@ -4,11 +4,11 @@ import { NavLink } from 'react-router-dom';
import Icon from './Icon'; import Icon from './Icon';
function LeftMenuLink({ children, to }) { function LeftMenuLink({ children, to, Component }) {
return ( return (
<NavLink to={to}> <NavLink to={to}>
<Icon /> <Icon />
<p>{children}</p> {Component ? <Component /> : <p>{children}</p>}
</NavLink> </NavLink>
); );
} }

View File

@ -22,7 +22,23 @@ const RelationTargetPicker = ({ onChange, oneThatIsCreatingARelationWithAnother,
[sortedContentTypesList] [sortedContentTypesList]
); );
const targetFriendlyName = get(contentTypes, [target, 'schema', 'name'], 'error'); const pluginInfo = useMemo(() => {
const plugin = get(contentTypes, [target, 'plugin'], null);
if (plugin) {
return (
<span style={{ fontStyle: 'italic', textTransform: 'none' }}>&nbsp; (from: {plugin})</span>
);
}
return null;
}, [contentTypes, target]);
const targetFriendlyName = useMemo(() => {
const name = get(contentTypes, [target, 'schema', 'name'], 'error');
return name;
}, [contentTypes, target]);
return ( return (
<Wrapper> <Wrapper>
@ -39,22 +55,25 @@ const RelationTargetPicker = ({ onChange, oneThatIsCreatingARelationWithAnother,
style={{ fontSize: 12, marginTop: '-3px' }} style={{ fontSize: 12, marginTop: '-3px' }}
/> />
{targetFriendlyName} {targetFriendlyName}
{pluginInfo}
</p> </p>
</DropdownToggle> </DropdownToggle>
<DropdownMenu style={{ paddingTop: '3px' }}> <DropdownMenu style={{ paddingTop: '3px' }}>
{allowedContentTypesForRelation.map(({ uid, title, restrictRelationsTo }) => { {allowedContentTypesForRelation.map(({ uid, title, restrictRelationsTo, plugin }) => {
return ( return (
<DropdownItem <DropdownItem
key={uid} key={uid}
onClick={() => { onClick={() => {
// Change the target // Change the target
const selectedContentTypeFriendlyName = plugin ? `${plugin}_${title}` : title;
onChange({ onChange({
target: { target: {
name: 'target', name: 'target',
value: uid, value: uid,
type: 'relation', type: 'relation',
oneThatIsCreatingARelationWithAnother, oneThatIsCreatingARelationWithAnother,
selectedContentTypeFriendlyName: title, selectedContentTypeFriendlyName,
targetContentTypeAllowedRelations: restrictRelationsTo, targetContentTypeAllowedRelations: restrictRelationsTo,
}, },
}); });
@ -66,6 +85,11 @@ const RelationTargetPicker = ({ onChange, oneThatIsCreatingARelationWithAnother,
style={{ fontSize: 12, marginTop: '-3px' }} style={{ fontSize: 12, marginTop: '-3px' }}
/> />
{title} {title}
{plugin && (
<span style={{ fontStyle: 'italic', textTransform: 'none' }}>
&nbsp; (from: {plugin})
</span>
)}
</p> </p>
</DropdownItem> </DropdownItem>
); );

View File

@ -148,6 +148,7 @@ const sortContentType = types =>
editable: types[uid].schema.editable, editable: types[uid].schema.editable,
name: uid, name: uid,
title: types[uid].schema.name, title: types[uid].schema.name,
plugin: types[uid].plugin || null,
uid, uid,
to: `/plugins/${pluginId}/content-types/${uid}`, to: `/plugins/${pluginId}/content-types/${uid}`,
kind: types[uid].schema.kind, kind: types[uid].schema.kind,

View File

@ -100,6 +100,8 @@ const expectedData = {
to: '/plugins/content-type-builder/content-types/plugins::myplugins.atest', to: '/plugins/content-type-builder/content-types/plugins::myplugins.atest',
kind: 'collectionType', kind: 'collectionType',
editable: true, editable: true,
plugin: null,
restrictRelationsTo: [],
}, },
{ {
uid: 'plugins::myplugins.btest', uid: 'plugins::myplugins.btest',
@ -108,6 +110,8 @@ const expectedData = {
to: '/plugins/content-type-builder/content-types/plugins::myplugins.btest', to: '/plugins/content-type-builder/content-types/plugins::myplugins.btest',
kind: 'collectionType', kind: 'collectionType',
editable: true, editable: true,
plugin: null,
restrictRelationsTo: null,
}, },
{ {
uid: 'plugins::myplugins.ctest', uid: 'plugins::myplugins.ctest',
@ -116,6 +120,8 @@ const expectedData = {
to: '/plugins/content-type-builder/content-types/plugins::myplugins.ctest', to: '/plugins/content-type-builder/content-types/plugins::myplugins.ctest',
kind: 'collectionType', kind: 'collectionType',
editable: true, editable: true,
plugin: null,
restrictRelationsTo: ['oneWay'],
}, },
{ {
uid: 'plugins::myplugins.test', uid: 'plugins::myplugins.test',
@ -124,6 +130,8 @@ const expectedData = {
to: '/plugins/content-type-builder/content-types/plugins::myplugins.test', to: '/plugins/content-type-builder/content-types/plugins::myplugins.test',
kind: 'singleType', kind: 'singleType',
editable: true, editable: true,
plugin: null,
restrictRelationsTo: null,
}, },
], ],

View File

@ -150,19 +150,39 @@ const data = {
contentTypesToSort: { contentTypesToSort: {
'plugins::myplugins.test': { 'plugins::myplugins.test': {
uid: 'plugins::myplugins.test', uid: 'plugins::myplugins.test',
schema: { name: 'plugins::myplugins.test', kind: 'singleType', editable: true }, schema: {
name: 'plugins::myplugins.test',
kind: 'singleType',
editable: true,
restrictRelationsTo: null,
},
}, },
'plugins::myplugins.btest': { 'plugins::myplugins.btest': {
uid: 'plugins::myplugins.btest', uid: 'plugins::myplugins.btest',
schema: { name: 'plugins::myplugins.btest', kind: 'collectionType', editable: true }, schema: {
name: 'plugins::myplugins.btest',
kind: 'collectionType',
editable: true,
restrictRelationsTo: null,
},
}, },
'plugins::myplugins.atest': { 'plugins::myplugins.atest': {
uid: 'plugins::myplugins.atest', uid: 'plugins::myplugins.atest',
schema: { name: 'plugins::myplugins.atest', kind: 'collectionType', editable: true }, schema: {
name: 'plugins::myplugins.atest',
kind: 'collectionType',
editable: true,
restrictRelationsTo: [],
},
}, },
'plugins::myplugins.ctest': { 'plugins::myplugins.ctest': {
uid: 'plugins::myplugins.ctest', uid: 'plugins::myplugins.ctest',
schema: { name: 'plugins::myplugins.ctest', kind: 'collectionType', editable: true }, schema: {
name: 'plugins::myplugins.ctest',
kind: 'collectionType',
editable: true,
restrictRelationsTo: ['oneWay'],
},
}, },
}, },
// TODO add test for component // TODO add test for component

View File

@ -84,24 +84,35 @@ const reducer = (state, action) => {
} }
if (keys.length === 1 && keys.includes('target')) { if (keys.length === 1 && keys.includes('target')) {
const { targetContentTypeAllowedRelations } = action;
let didChangeNatureBecauseOfRestrictedRelation = false;
return obj return obj
.update('target', () => value) .update('target', () => value)
.update('nature', currentNature => { .update('nature', currentNature => {
const { targetContentTypeAllowedRelations } = action;
if (targetContentTypeAllowedRelations === null) { if (targetContentTypeAllowedRelations === null) {
return currentNature; return currentNature;
} }
if (!targetContentTypeAllowedRelations.includes(currentNature)) { if (!targetContentTypeAllowedRelations.includes(currentNature)) {
didChangeNatureBecauseOfRestrictedRelation = true;
return targetContentTypeAllowedRelations[0]; return targetContentTypeAllowedRelations[0];
} }
return currentNature; return currentNature;
}) })
.update('name', () => { .update('name', () => {
if (didChangeNatureBecauseOfRestrictedRelation) {
return pluralize( return pluralize(
snakeCase(selectedContentTypeFriendlyName), snakeCase(selectedContentTypeFriendlyName),
shouldPluralizeName(targetContentTypeAllowedRelations[0])
);
}
return pluralize(
snakeCase(selectedContentTypeFriendlyName),
shouldPluralizeName(obj.get('nature')) shouldPluralizeName(obj.get('nature'))
); );
}) })

View File

@ -244,7 +244,7 @@ describe('CTB | containers | FormModal | reducer | actions', () => {
}; };
const expected = state const expected = state
.setIn(['modifiedData', 'target'], 'application::country.country') .setIn(['modifiedData', 'target'], 'application::country.country')
.setIn(['modifiedData', 'name'], 'countries') .setIn(['modifiedData', 'name'], 'country')
.setIn(['modifiedData', 'nature'], 'oneWay'); .setIn(['modifiedData', 'nature'], 'oneWay');
expect(reducer(state, action)).toEqual(expected); expect(reducer(state, action)).toEqual(expected);

View File

@ -107,9 +107,27 @@ function LeftMenu({ wait }) {
} }
}; };
const displayedContentTypes = useMemo(() => sortedContentTypesList.filter(obj => obj.editable), [ const displayedContentTypes = useMemo(() => {
sortedContentTypesList, return sortedContentTypesList
]); .filter(obj => obj.editable)
.map(obj => {
if (obj.plugin) {
return {
...obj,
Component: () => (
<p style={{ justifyContent: 'normal' }}>
{obj.title}&nbsp;
<span style={{ fontStyle: 'italic', textTransform: 'none' }}>
({formatMessage({ id: getTrad('from') })}): : {obj.plugin})
</span>
</p>
),
};
}
return obj;
});
}, [sortedContentTypesList, formatMessage]);
const data = [ const data = [
{ {

View File

@ -29,17 +29,17 @@ const getRestrictRelationsTo = (contentType = {}) => {
}; };
const getformattedName = (contentType = {}) => { const getformattedName = (contentType = {}) => {
const { uid, info, plugin } = contentType; const { uid, info } = contentType;
const name = _.get(info, 'name') || _.upperFirst(pluralize(uid)); const name = _.get(info, 'name') || _.upperFirst(pluralize(uid));
const isUser = name.toLowerCase() === 'user'; // const isUser = name.toLowerCase() === 'user';
if (isUser && plugin === 'admin') { // if (isUser && plugin === 'admin') {
return 'User (Admin panel)'; // return 'User (Admin panel)';
} // }
if (isUser && plugin === 'users-permissions') { // if (isUser && plugin === 'users-permissions') {
return 'User (Permissions plugin)'; // return 'User (Permissions plugin)';
} // }
return name; return name;
}; };