Fix CT list in relations (content type builder)

Signed-off-by: HichamELBSI <elabbassih@gmail.com>
This commit is contained in:
HichamELBSI 2021-11-26 10:59:34 +01:00
parent 8125a9d3bc
commit ca1ec07f3b
6 changed files with 110 additions and 10 deletions

View File

@ -48,7 +48,7 @@ import SingularName from '../SingularName';
import TabForm from '../TabForm';
import TextareaEnum from '../TextareaEnum';
import findAttribute from '../../utils/findAttribute';
import getTrad from '../../utils/getTrad';
import { getTrad, isAllowedContentTypesForRelations } from '../../utils';
import {
canEditContentType,
getAttributesToDisplay,
@ -141,7 +141,7 @@ const FormModal = () => {
useEffect(() => {
if (isOpen) {
const collectionTypesForRelation = sortedContentTypesList.filter(
({ kind, visible }) => kind === 'collectionType' && visible
isAllowedContentTypesForRelations
);
// Reset all the modification when opening the edit category modal

View File

@ -5,18 +5,15 @@ import get from 'lodash/get';
import { MenuItem, SimpleMenu } from '@strapi/design-system/SimpleMenu';
import useDataManager from '../../../../hooks/useDataManager';
import { ON_CHANGE_RELATION_TARGET } from '../../../FormModal/constants';
import { isAllowedContentTypesForRelations } from '../../../../utils';
const RelationTargetPicker = ({ oneThatIsCreatingARelationWithAnother, target }) => {
const { contentTypes, sortedContentTypesList } = useDataManager();
const dispatch = useDispatch();
// TODO: replace with an obj { relation: 'x', bidirctional: true|false }
const allowedContentTypesForRelation = sortedContentTypesList
.filter(obj => obj.kind === 'collectionType' && obj.visible)
.filter(
obj =>
obj.restrictRelationsTo === null ||
(Array.isArray(obj.restrictRelationsTo) && obj.restrictRelationsTo.length > 0)
);
const allowedContentTypesForRelation = sortedContentTypesList.filter(
isAllowedContentTypesForRelations
);
const plugin = get(contentTypes, [target, 'plugin'], null);

View File

@ -1 +1,2 @@
export { default as getTrad } from './getTrad';
export { default as isAllowedContentTypesForRelations } from './isAllowedContentTypesForRelations';

View File

@ -0,0 +1,10 @@
const isAllowedContentTypesForRelations = contentType => {
return (
contentType.kind === 'collectionType' &&
(contentType.restrictRelationsTo === null ||
(Array.isArray(contentType.restrictRelationsTo) &&
contentType.restrictRelationsTo.length > 0))
);
};
export default isAllowedContentTypesForRelations;

View File

@ -0,0 +1,91 @@
import isAllowedContentTypesForRelations from '../isAllowedContentTypesForRelations';
describe('CTB | utils | isAllowedContentTypesForRelations', () => {
it('should be falsy if the model is a single type', () => {
const contentType = {
visible: true,
name: 'plugin::users-permissions.user',
title: 'User',
plugin: 'users-permissions',
uid: 'plugin::users-permissions.user',
to: '/plugins/content-type-builder/content-types/plugin::users-permissions.user',
kind: 'singleType',
restrictRelationsTo: null,
};
expect(isAllowedContentTypesForRelations(contentType)).toBeFalsy();
});
it('should be falsy if the restricted relations is an empty array', () => {
const contentType = {
visible: true,
name: 'plugin::users-permissions.user',
title: 'User',
plugin: 'users-permissions',
uid: 'plugin::users-permissions.user',
to: '/plugins/content-type-builder/content-types/plugin::users-permissions.user',
kind: 'collectionType',
restrictRelationsTo: [],
};
expect(isAllowedContentTypesForRelations(contentType)).toBeFalsy();
});
it('should be truthy if the model is a collection type and the restricted relations is null', () => {
const contentType = {
visible: true,
name: 'plugin::users-permissions.user',
title: 'User',
plugin: 'users-permissions',
uid: 'plugin::users-permissions.user',
to: '/plugins/content-type-builder/content-types/plugin::users-permissions.user',
kind: 'collectionType',
restrictRelationsTo: null,
};
expect(isAllowedContentTypesForRelations(contentType)).toBeTruthy();
});
it('should be truthy if the model is a collection type and the restricted relations is not empty array', () => {
const contentType = {
visible: true,
name: 'plugin::users-permissions.user',
title: 'User',
plugin: 'users-permissions',
uid: 'plugin::users-permissions.user',
to: '/plugins/content-type-builder/content-types/plugin::users-permissions.user',
kind: 'collectionType',
restrictRelationsTo: ['oneWay', 'manyWay'],
};
expect(isAllowedContentTypesForRelations(contentType)).toBeTruthy();
});
it('should be falsy if restrictRelationsTo is undefined', () => {
const contentType = {
visible: true,
name: 'plugin::users-permissions.user',
title: 'User',
plugin: 'users-permissions',
uid: 'plugin::users-permissions.user',
to: '/plugins/content-type-builder/content-types/plugin::users-permissions.user',
kind: 'collectionType',
};
expect(isAllowedContentTypesForRelations(contentType)).toBeFalsy();
});
it('should be falsy if kind is undefined', () => {
const contentType = {
visible: true,
name: 'plugin::users-permissions.user',
title: 'User',
plugin: 'users-permissions',
uid: 'plugin::users-permissions.user',
to: '/plugins/content-type-builder/content-types/plugin::users-permissions.user',
restrictRelationsTo: ['oneWay', 'manyWay'],
};
expect(isAllowedContentTypesForRelations(contentType)).toBeFalsy();
});
});

View File

@ -1,5 +1,6 @@
'use strict';
const { isUndefined } = require('lodash/fp');
const { yup } = require('@strapi/utils');
const { typeKinds, coreUids } = require('../../services/constants');
const { isValidName } = require('./common');
@ -9,7 +10,7 @@ const STRAPI_USER_RELATIONS = ['oneToOne', 'oneToMany'];
const isValidRelation = validNatures =>
function(value) {
if (this.parent.target === coreUids.STRAPI_USER) {
if (!validNatures.includes(value) || typeof this.parent.targetAttribute !== undefined) {
if (!validNatures.includes(value) || !isUndefined(this.parent.targetAttribute)) {
return this.createError({
path: this.path,
message: `must be one of the following values: ${STRAPI_USER_RELATIONS.join(', ')}`,