Skip to content

Commit

Permalink
fix: refactor permission check [FUS-115]
Browse files Browse the repository at this point in the history
  • Loading branch information
marcolink committed Jan 9, 2025
1 parent b8e7814 commit 8cb3708
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import noop from 'lodash/noop';
import { EntityProvider } from '../common/EntityStore';
import { ReferenceEditorProps } from '../common/ReferenceEditor';
import { SortableLinkList } from '../common/SortableLinkList';
import { useEditorPermissions } from '../common/useEditorPermissions';
import { CombinedLinkEntityActions } from '../components/LinkActions/LinkEntityActions';
import { ResourceLink } from '../types';
import { EntryRoute } from './Cards/ContentfulEntryCard';
Expand All @@ -29,11 +28,10 @@ type ChildProps = {
type EditorProps = ReferenceEditorProps &
Omit<ChildProps, 'onSortStart' | 'onSortEnd' | 'onMove' | 'onRemoteItemAtIndex'> & {
children: (props: ReferenceEditorProps & ChildProps) => React.ReactElement;
apiUrl: string;
};

function ResourceEditor(props: EditorProps) {
const { setValue, items, apiUrl } = props;
const { setValue, items } = props;

const onSortStart = () => noop();
const onSortEnd = useCallback(
Expand All @@ -58,18 +56,9 @@ function ResourceEditor(props: EditorProps) {
[items, setValue]
);

const editorPermissions = useEditorPermissions({
...props,
entityType: 'Entry',
allContentTypes: [],
});

const { dialogs, field } = props.sdk;
const linkActionsProps = useResourceLinkActions({
dialogs,
field,
apiUrl,
editorPermissions,
sdk: props.sdk,
parameters: props.parameters,
});

return (
Expand Down
16 changes: 2 additions & 14 deletions packages/reference/src/resources/SingleResourceReferenceEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { FieldConnector } from '@contentful/field-editor-shared';

import { EntityProvider } from '../common/EntityStore';
import { ReferenceEditorProps } from '../common/ReferenceEditor';
import { useEditorPermissions } from '../common/useEditorPermissions';
import { CombinedLinkEntityActions } from '../components/LinkActions/LinkEntityActions';
import { ResourceLink } from '../types';
import { EntryRoute } from './Cards/ContentfulEntryCard';
Expand All @@ -14,22 +13,11 @@ import { useResourceLinkActions } from './useResourceLinkActions';
export function SingleResourceReferenceEditor(
props: ReferenceEditorProps & {
getEntryRouteHref: (entryRoute: EntryRoute) => string;
apiUrl: string;
}
) {
const { dialogs, field } = props.sdk;

const editorPermissions = useEditorPermissions({
...props,
entityType: 'Entry',
allContentTypes: [],
});

const linkActionsProps = useResourceLinkActions({
dialogs,
field,
apiUrl: props.apiUrl,
editorPermissions,
sdk: props.sdk,
parameters: props.parameters,
});

return (
Expand Down
25 changes: 15 additions & 10 deletions packages/reference/src/resources/useResourceLinkActions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useMemo } from 'react';

import type { FieldAPI, FieldAppSDK } from '@contentful/app-sdk';
import type { FieldAPI } from '@contentful/app-sdk';
import type { ResourceLink } from 'contentful-management';

import { EditorPermissions } from '../common/useEditorPermissions';
import { EditorPermissionsProps, useEditorPermissions } from '../common/useEditorPermissions';
import { LinkActionsProps } from '../components';

const getUpdatedValue = (
Expand All @@ -19,16 +19,14 @@ const getUpdatedValue = (
}
};

type ResourceLinkActionProps = Pick<FieldAppSDK, 'field' | 'dialogs'> & {
apiUrl: string;
editorPermissions: Pick<EditorPermissions, 'canLinkEntity'>;
};
type ResourceLinkActionProps = Pick<EditorPermissionsProps, 'parameters' | 'sdk'>;

export function useResourceLinkActions({
dialogs,
field,
editorPermissions,
parameters,
sdk,
}: ResourceLinkActionProps): LinkActionsProps {
const { field, dialogs } = sdk;

const onLinkedExisting = useMemo(() => {
return (
links: ResourceLink<'Contentful:Entry'>[] | [ResourceLink<'Contentful:Entry'> | null]
Expand Down Expand Up @@ -63,6 +61,13 @@ export function useResourceLinkActions({
// @ts-expect-error wait for update of app-sdk version
}, [dialogs, field.allowedResources, multiple, onLinkedExisting]);

const { canLinkEntity } = useEditorPermissions({
entityType: 'Entry',
allContentTypes: [],
sdk,
parameters,
});

return {
onLinkExisting,
// @ts-expect-error
Expand All @@ -72,7 +77,7 @@ export function useResourceLinkActions({
contentTypes: [],
canCreateEntity: false,
canLinkMultiple: multiple,
canLinkEntity: editorPermissions.canLinkEntity,
canLinkEntity,
isDisabled: false,
isEmpty: false,
isFull: false,
Expand Down

0 comments on commit 8cb3708

Please sign in to comment.