mirror of
https://github.com/strapi/strapi.git
synced 2025-12-16 01:34:23 +00:00
Fix CT list in relations (content type builder)
Signed-off-by: HichamELBSI <elabbassih@gmail.com>
This commit is contained in:
parent
8125a9d3bc
commit
ca1ec07f3b
@ -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
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
@ -1 +1,2 @@
|
||||
export { default as getTrad } from './getTrad';
|
||||
export { default as isAllowedContentTypesForRelations } from './isAllowedContentTypesForRelations';
|
||||
|
||||
@ -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;
|
||||
@ -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();
|
||||
});
|
||||
});
|
||||
@ -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(', ')}`,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user