@contentful/field-editor-reference 6.4.1 → 6.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/common/useContentTypePermissions.js +18 -12
- package/dist/cjs/common/useEditorPermissions.js +8 -6
- package/dist/cjs/common/useEditorPermissions.spec.js +8 -4
- package/dist/esm/common/useContentTypePermissions.js +18 -12
- package/dist/esm/common/useEditorPermissions.js +8 -6
- package/dist/esm/common/useEditorPermissions.spec.js +8 -4
- package/dist/types/common/useContentTypePermissions.d.ts +1 -1
- package/dist/types/common/useEditorPermissions.d.ts +1 -1
- package/package.json +4 -4
|
@@ -9,39 +9,45 @@ Object.defineProperty(exports, "useContentTypePermissions", {
|
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
11
|
const _react = require("react");
|
|
12
|
+
const _lodash = require("lodash");
|
|
12
13
|
const _useAccessApi = require("./useAccessApi");
|
|
13
14
|
async function filter(arr, predicate) {
|
|
14
15
|
const fail = Symbol();
|
|
15
16
|
const results = await Promise.all(arr.map(async (item)=>await predicate(item) ? item : fail));
|
|
16
17
|
return results.filter((x)=>x !== fail);
|
|
17
18
|
}
|
|
18
|
-
function useContentTypePermissions(
|
|
19
|
+
function useContentTypePermissions({ entityType, validations, sdk, allContentTypes }) {
|
|
19
20
|
const availableContentTypes = (0, _react.useMemo)(()=>{
|
|
20
|
-
if (
|
|
21
|
+
if (entityType === 'Asset') {
|
|
21
22
|
return [];
|
|
22
23
|
}
|
|
23
|
-
if (
|
|
24
|
-
return
|
|
24
|
+
if (validations.contentTypes) {
|
|
25
|
+
return allContentTypes.filter((ct)=>validations.contentTypes?.includes(ct.sys.id));
|
|
25
26
|
}
|
|
26
|
-
return
|
|
27
|
+
return allContentTypes;
|
|
27
28
|
}, [
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
allContentTypes,
|
|
30
|
+
validations.contentTypes,
|
|
31
|
+
entityType
|
|
31
32
|
]);
|
|
32
33
|
const [creatableContentTypes, setCreatableContentTypes] = (0, _react.useState)(availableContentTypes);
|
|
33
|
-
const { canPerformActionOnEntryOfType } = (0, _useAccessApi.useAccessApi)(
|
|
34
|
+
const { canPerformActionOnEntryOfType } = (0, _useAccessApi.useAccessApi)(sdk.access);
|
|
34
35
|
(0, _react.useEffect)(()=>{
|
|
35
36
|
function getContentTypes(action) {
|
|
36
37
|
return filter(availableContentTypes, (ct)=>canPerformActionOnEntryOfType(action, ct.sys.id));
|
|
37
38
|
}
|
|
38
39
|
async function checkContentTypeAccess() {
|
|
39
40
|
const creatable = await getContentTypes('create');
|
|
40
|
-
|
|
41
|
+
if (!(0, _lodash.isEqual)(creatable, creatableContentTypes)) {
|
|
42
|
+
setCreatableContentTypes(creatable);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (availableContentTypes.length > 0) {
|
|
46
|
+
void checkContentTypeAccess();
|
|
41
47
|
}
|
|
42
|
-
void checkContentTypeAccess();
|
|
43
48
|
}, [
|
|
44
|
-
availableContentTypes
|
|
49
|
+
availableContentTypes,
|
|
50
|
+
creatableContentTypes
|
|
45
51
|
]);
|
|
46
52
|
return {
|
|
47
53
|
creatableContentTypes,
|
|
@@ -12,16 +12,18 @@ const _react = require("react");
|
|
|
12
12
|
const _fromFieldValidations = require("../utils/fromFieldValidations");
|
|
13
13
|
const _useAccessApi = require("./useAccessApi");
|
|
14
14
|
const _useContentTypePermissions = require("./useContentTypePermissions");
|
|
15
|
-
function useEditorPermissions(
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
props.sdk.field
|
|
15
|
+
function useEditorPermissions({ sdk, entityType, parameters, allContentTypes }) {
|
|
16
|
+
const validations = (0, _react.useMemo)(()=>(0, _fromFieldValidations.fromFieldValidations)(sdk.field), [
|
|
17
|
+
sdk.field
|
|
19
18
|
]);
|
|
20
19
|
const [canCreateEntity, setCanCreateEntity] = (0, _react.useState)(true);
|
|
21
20
|
const [canLinkEntity, setCanLinkEntity] = (0, _react.useState)(true);
|
|
22
21
|
const { creatableContentTypes, availableContentTypes } = (0, _useContentTypePermissions.useContentTypePermissions)({
|
|
23
|
-
|
|
24
|
-
validations
|
|
22
|
+
entityType,
|
|
23
|
+
validations,
|
|
24
|
+
sdk,
|
|
25
|
+
allContentTypes,
|
|
26
|
+
parameters
|
|
25
27
|
});
|
|
26
28
|
const { canPerformAction } = (0, _useAccessApi.useAccessApi)(sdk.access);
|
|
27
29
|
(0, _react.useEffect)(()=>{
|
|
@@ -18,7 +18,7 @@ describe('useEditorPermissions', ()=>{
|
|
|
18
18
|
id
|
|
19
19
|
}
|
|
20
20
|
});
|
|
21
|
-
const renderEditorPermissions = async ({ entityType, params = {}, allContentTypes = [], customizeMock, customizeSdk })=>{
|
|
21
|
+
const renderEditorPermissions = async ({ entityType, params = {}, allContentTypes = [], customizeMock, customizeSdk, waitForUpdate = false })=>{
|
|
22
22
|
const sdk = makeFieldAppSDK(customizeMock);
|
|
23
23
|
customizeSdk?.(sdk);
|
|
24
24
|
const renderResult = (0, _reacthooks.renderHook)(()=>(0, _useEditorPermissions.useEditorPermissions)({
|
|
@@ -29,7 +29,9 @@ describe('useEditorPermissions', ()=>{
|
|
|
29
29
|
instance: params
|
|
30
30
|
}
|
|
31
31
|
}));
|
|
32
|
-
|
|
32
|
+
if (waitForUpdate) {
|
|
33
|
+
await renderResult.waitForNextUpdate();
|
|
34
|
+
}
|
|
33
35
|
return {
|
|
34
36
|
...renderResult,
|
|
35
37
|
sdk
|
|
@@ -102,7 +104,8 @@ describe('useEditorPermissions', ()=>{
|
|
|
102
104
|
allContentTypes,
|
|
103
105
|
customizeSdk: (sdk)=>{
|
|
104
106
|
allowContentTypes(sdk, 'create', 'one');
|
|
105
|
-
}
|
|
107
|
+
},
|
|
108
|
+
waitForUpdate: true
|
|
106
109
|
});
|
|
107
110
|
expect(result.current.canCreateEntity).toBe(true);
|
|
108
111
|
});
|
|
@@ -130,7 +133,8 @@ describe('useEditorPermissions', ()=>{
|
|
|
130
133
|
allContentTypes,
|
|
131
134
|
customizeSdk: (sdk)=>{
|
|
132
135
|
allowContentTypes(sdk, 'read', 'one');
|
|
133
|
-
}
|
|
136
|
+
},
|
|
137
|
+
waitForUpdate: true
|
|
134
138
|
});
|
|
135
139
|
expect(result.current.canLinkEntity).toBe(true);
|
|
136
140
|
});
|
|
@@ -1,37 +1,43 @@
|
|
|
1
1
|
import { useEffect, useMemo, useState } from 'react';
|
|
2
|
+
import { isEqual } from 'lodash';
|
|
2
3
|
import { useAccessApi } from './useAccessApi';
|
|
3
4
|
async function filter(arr, predicate) {
|
|
4
5
|
const fail = Symbol();
|
|
5
6
|
const results = await Promise.all(arr.map(async (item)=>await predicate(item) ? item : fail));
|
|
6
7
|
return results.filter((x)=>x !== fail);
|
|
7
8
|
}
|
|
8
|
-
export function useContentTypePermissions(
|
|
9
|
+
export function useContentTypePermissions({ entityType, validations, sdk, allContentTypes }) {
|
|
9
10
|
const availableContentTypes = useMemo(()=>{
|
|
10
|
-
if (
|
|
11
|
+
if (entityType === 'Asset') {
|
|
11
12
|
return [];
|
|
12
13
|
}
|
|
13
|
-
if (
|
|
14
|
-
return
|
|
14
|
+
if (validations.contentTypes) {
|
|
15
|
+
return allContentTypes.filter((ct)=>validations.contentTypes?.includes(ct.sys.id));
|
|
15
16
|
}
|
|
16
|
-
return
|
|
17
|
+
return allContentTypes;
|
|
17
18
|
}, [
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
allContentTypes,
|
|
20
|
+
validations.contentTypes,
|
|
21
|
+
entityType
|
|
21
22
|
]);
|
|
22
23
|
const [creatableContentTypes, setCreatableContentTypes] = useState(availableContentTypes);
|
|
23
|
-
const { canPerformActionOnEntryOfType } = useAccessApi(
|
|
24
|
+
const { canPerformActionOnEntryOfType } = useAccessApi(sdk.access);
|
|
24
25
|
useEffect(()=>{
|
|
25
26
|
function getContentTypes(action) {
|
|
26
27
|
return filter(availableContentTypes, (ct)=>canPerformActionOnEntryOfType(action, ct.sys.id));
|
|
27
28
|
}
|
|
28
29
|
async function checkContentTypeAccess() {
|
|
29
30
|
const creatable = await getContentTypes('create');
|
|
30
|
-
|
|
31
|
+
if (!isEqual(creatable, creatableContentTypes)) {
|
|
32
|
+
setCreatableContentTypes(creatable);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (availableContentTypes.length > 0) {
|
|
36
|
+
void checkContentTypeAccess();
|
|
31
37
|
}
|
|
32
|
-
void checkContentTypeAccess();
|
|
33
38
|
}, [
|
|
34
|
-
availableContentTypes
|
|
39
|
+
availableContentTypes,
|
|
40
|
+
creatableContentTypes
|
|
35
41
|
]);
|
|
36
42
|
return {
|
|
37
43
|
creatableContentTypes,
|
|
@@ -2,16 +2,18 @@ import { useEffect, useMemo, useState } from 'react';
|
|
|
2
2
|
import { fromFieldValidations } from '../utils/fromFieldValidations';
|
|
3
3
|
import { useAccessApi } from './useAccessApi';
|
|
4
4
|
import { useContentTypePermissions } from './useContentTypePermissions';
|
|
5
|
-
export function useEditorPermissions(
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
props.sdk.field
|
|
5
|
+
export function useEditorPermissions({ sdk, entityType, parameters, allContentTypes }) {
|
|
6
|
+
const validations = useMemo(()=>fromFieldValidations(sdk.field), [
|
|
7
|
+
sdk.field
|
|
9
8
|
]);
|
|
10
9
|
const [canCreateEntity, setCanCreateEntity] = useState(true);
|
|
11
10
|
const [canLinkEntity, setCanLinkEntity] = useState(true);
|
|
12
11
|
const { creatableContentTypes, availableContentTypes } = useContentTypePermissions({
|
|
13
|
-
|
|
14
|
-
validations
|
|
12
|
+
entityType,
|
|
13
|
+
validations,
|
|
14
|
+
sdk,
|
|
15
|
+
allContentTypes,
|
|
16
|
+
parameters
|
|
15
17
|
});
|
|
16
18
|
const { canPerformAction } = useAccessApi(sdk.access);
|
|
17
19
|
useEffect(()=>{
|
|
@@ -14,7 +14,7 @@ describe('useEditorPermissions', ()=>{
|
|
|
14
14
|
id
|
|
15
15
|
}
|
|
16
16
|
});
|
|
17
|
-
const renderEditorPermissions = async ({ entityType, params = {}, allContentTypes = [], customizeMock, customizeSdk })=>{
|
|
17
|
+
const renderEditorPermissions = async ({ entityType, params = {}, allContentTypes = [], customizeMock, customizeSdk, waitForUpdate = false })=>{
|
|
18
18
|
const sdk = makeFieldAppSDK(customizeMock);
|
|
19
19
|
customizeSdk?.(sdk);
|
|
20
20
|
const renderResult = renderHook(()=>useEditorPermissions({
|
|
@@ -25,7 +25,9 @@ describe('useEditorPermissions', ()=>{
|
|
|
25
25
|
instance: params
|
|
26
26
|
}
|
|
27
27
|
}));
|
|
28
|
-
|
|
28
|
+
if (waitForUpdate) {
|
|
29
|
+
await renderResult.waitForNextUpdate();
|
|
30
|
+
}
|
|
29
31
|
return {
|
|
30
32
|
...renderResult,
|
|
31
33
|
sdk
|
|
@@ -98,7 +100,8 @@ describe('useEditorPermissions', ()=>{
|
|
|
98
100
|
allContentTypes,
|
|
99
101
|
customizeSdk: (sdk)=>{
|
|
100
102
|
allowContentTypes(sdk, 'create', 'one');
|
|
101
|
-
}
|
|
103
|
+
},
|
|
104
|
+
waitForUpdate: true
|
|
102
105
|
});
|
|
103
106
|
expect(result.current.canCreateEntity).toBe(true);
|
|
104
107
|
});
|
|
@@ -126,7 +129,8 @@ describe('useEditorPermissions', ()=>{
|
|
|
126
129
|
allContentTypes,
|
|
127
130
|
customizeSdk: (sdk)=>{
|
|
128
131
|
allowContentTypes(sdk, 'read', 'one');
|
|
129
|
-
}
|
|
132
|
+
},
|
|
133
|
+
waitForUpdate: true
|
|
130
134
|
});
|
|
131
135
|
expect(result.current.canLinkEntity).toBe(true);
|
|
132
136
|
});
|
|
@@ -12,5 +12,5 @@ type ContentTypePermissions = {
|
|
|
12
12
|
creatableContentTypes: ContentType[];
|
|
13
13
|
availableContentTypes: ContentType[];
|
|
14
14
|
};
|
|
15
|
-
export declare function useContentTypePermissions(
|
|
15
|
+
export declare function useContentTypePermissions({ entityType, validations, sdk, allContentTypes, }: ContentTypePermissionsProps): ContentTypePermissions;
|
|
16
16
|
export {};
|
|
@@ -6,7 +6,7 @@ export type EditorPermissionsProps = {
|
|
|
6
6
|
parameters: ReferenceEditorProps['parameters'];
|
|
7
7
|
allContentTypes: ContentType[];
|
|
8
8
|
};
|
|
9
|
-
export declare function useEditorPermissions(
|
|
9
|
+
export declare function useEditorPermissions({ sdk, entityType, parameters, allContentTypes, }: EditorPermissionsProps): {
|
|
10
10
|
canCreateEntity: boolean;
|
|
11
11
|
canLinkEntity: boolean;
|
|
12
12
|
creatableContentTypes: ContentType[];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentful/field-editor-reference",
|
|
3
|
-
"version": "6.4.
|
|
3
|
+
"version": "6.4.3",
|
|
4
4
|
"main": "dist/cjs/index.js",
|
|
5
5
|
"module": "dist/esm/index.js",
|
|
6
6
|
"types": "dist/types/index.d.ts",
|
|
@@ -38,14 +38,14 @@
|
|
|
38
38
|
"@contentful/f36-components": "^4.70.0",
|
|
39
39
|
"@contentful/f36-icons": "^4.29.0",
|
|
40
40
|
"@contentful/f36-tokens": "^4.0.5",
|
|
41
|
-
"@contentful/field-editor-shared": "^2.3.
|
|
41
|
+
"@contentful/field-editor-shared": "^2.3.1",
|
|
42
42
|
"@contentful/mimetype": "^2.2.29",
|
|
43
43
|
"@dnd-kit/core": "^6.0.8",
|
|
44
44
|
"@dnd-kit/modifiers": "^7.0.0",
|
|
45
45
|
"@dnd-kit/sortable": "^8.0.0",
|
|
46
46
|
"@tanstack/react-query": "^4.3.9",
|
|
47
47
|
"constate": "^3.3.2",
|
|
48
|
-
"contentful-management": "^11.
|
|
48
|
+
"contentful-management": "^11.41.0",
|
|
49
49
|
"emotion": "^10.0.17",
|
|
50
50
|
"lodash": "^4.17.15",
|
|
51
51
|
"moment": "^2.20.0",
|
|
@@ -64,5 +64,5 @@
|
|
|
64
64
|
"publishConfig": {
|
|
65
65
|
"registry": "https://npm.pkg.github.com/"
|
|
66
66
|
},
|
|
67
|
-
"gitHead": "
|
|
67
|
+
"gitHead": "21b8e36ce7ed50abfe2a3a477103d5de1043674b"
|
|
68
68
|
}
|