Skip to content

Commit

Permalink
FIX: Max number of items for related items widget is not checked when…
Browse files Browse the repository at this point in the history
… 'create new' is used. [SDESK-5096] (#3428)

* FIX: Max number of items for related items widget is not checked when 'create new' is used. [SDESK-5096]

* Use getAssociationsByField.
  • Loading branch information
GyanP authored Apr 14, 2020
1 parent dfd6ab4 commit fbe7966
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion scripts/apps/relations/directives/RelatedItemsDirective.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {gettext} from 'core/utils';
import {AuthoringWorkspaceService} from 'apps/authoring/authoring/services/AuthoringWorkspaceService';
import {IArticle, IArticleField} from 'superdesk-api';
import {IDirectiveScope} from 'types/Angular/DirectiveScope';
import {getAssociationsByField} from '../../authoring/authoring/controllers/AssociationController';

const ARCHIVE_TYPES = ['archive', 'published'];
const isInArchive = (item: IArticle) => item._type != null && ARCHIVE_TYPES.includes(item._type);
Expand Down Expand Up @@ -73,7 +74,16 @@ export function RelatedItemsDirective(
return lock.isLocked(item) || lock.isLockedInCurrentSession(item);
};

scope.canAddRelatedItems = () => scope.field?.field_options?.allowed_workflows?.in_progress === true;
scope.canAddRelatedItems = () => {
const currentItemsLength = getAssociationsByField(scope.item, scope.field).length;

const maxCount = scope.field?.field_options?.multiple_items?.enabled === true
? scope.field.field_options.multiple_items.max_items
: 1;

return scope.field?.field_options?.allowed_workflows?.in_progress === true
&& currentItemsLength < maxCount;
};

const dragOverClass = 'dragover';
const fieldOptions = scope.field?.field_options || {};
Expand Down

0 comments on commit fbe7966

Please sign in to comment.