@contentful/react-apps-toolkit 0.5.10 → 0.6.1
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/README.md +85 -22
- package/dist/useCMA.js +2 -1
- package/dist/useFieldValue.d.ts +2 -1
- package/dist/useFieldValue.js +1 -3
- package/dist/useFieldValue.spec.js +55 -3
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -4,9 +4,9 @@ This library is still in development and should not be used in production.
|
|
|
4
4
|
|
|
5
5
|
# React Toolkit for Contentful Apps
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
React Hooks for the App Framework offer a simple way to bring frequently needed functionality into your react based [Contentful apps](/developers/docs/extensibility/app-framework/).
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
They can be used in apps created with [`create-contentful-app`](https://www.npmjs.com/package/@contentful/create-contentful-app), as well as any other React app using Contentful's [App SDK](https://www.npmjs.com/package/@contentful/app-sdk).
|
|
10
10
|
|
|
11
11
|
## Installation
|
|
12
12
|
|
|
@@ -20,52 +20,115 @@ yarn add @contentful/react-apps-toolkit
|
|
|
20
20
|
|
|
21
21
|
The following hooks and utilities are exported from the package:
|
|
22
22
|
|
|
23
|
-
###
|
|
23
|
+
### SDKProvider
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
The `SDKProvider` is a wrapper component, which automatically makes the Contentful [App SDK](https://www.npmjs.com/package/@contentful/app-sdk) available to any child components using React Context. To use any of the hooks contained in this package, they must be wrapped in the `<SDKProvider>`, because all of the hooks depend on the App SDK.
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
If it is not, the hook will throw an error.
|
|
29
|
-
|
|
30
|
-
Here is an example of how you can use it:
|
|
27
|
+
Usage:
|
|
31
28
|
|
|
32
29
|
```tsx
|
|
33
|
-
import { useSDK } from '@contentful/react-apps-toolkit'
|
|
30
|
+
import { SDKProvider, useSDK } from '@contentful/react-apps-toolkit';
|
|
34
31
|
|
|
35
|
-
function
|
|
32
|
+
function ChildComponentUsingHook() {
|
|
36
33
|
const sdk = useSDK<FieldExtensionSDK>();
|
|
37
34
|
|
|
38
|
-
return <>App Id: {sdk.ids.app}
|
|
35
|
+
return <>App Id: {sdk.ids.app}</>;
|
|
39
36
|
}
|
|
40
37
|
|
|
38
|
+
function App() {
|
|
39
|
+
return (
|
|
40
|
+
<SDKProvider>
|
|
41
|
+
<ChildComponentUsingHook />
|
|
42
|
+
</SDKProvider>
|
|
43
|
+
);
|
|
44
|
+
}
|
|
41
45
|
```
|
|
42
46
|
|
|
43
|
-
###
|
|
47
|
+
### useSDK
|
|
44
48
|
|
|
45
|
-
|
|
49
|
+
`useSDK` returns an instance of the Contentful [App SDK](https://www.npmjs.com/package/@contentful/app-sdk).
|
|
46
50
|
|
|
51
|
+
It must be wrapped it within the `SDKProvider`, otherwise, it will throw an error.
|
|
52
|
+
|
|
53
|
+
Usage:
|
|
47
54
|
|
|
48
55
|
```tsx
|
|
49
|
-
import {
|
|
56
|
+
import { SDKProvider, useSDK } from '@contentful/react-apps-toolkit';
|
|
57
|
+
|
|
58
|
+
function ComponentUsingSDK() {
|
|
59
|
+
const sdk = useSDK<FieldExtensionSDK>();
|
|
60
|
+
|
|
61
|
+
return <>App Id: {sdk.ids.app}</>;
|
|
62
|
+
}
|
|
50
63
|
|
|
51
64
|
function App() {
|
|
65
|
+
return (
|
|
66
|
+
<SDKProvider>
|
|
67
|
+
<ChildComponentUsingSDK />
|
|
68
|
+
</SDKProvider>
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### useCMA
|
|
74
|
+
|
|
75
|
+
`useCMA` returns an initialized [client for the Contentful Management API](https://www.npmjs.com/package/contentful-management). This can be used immediately to communicate with the environment the app is rendered in. [Contentful Management API docs](/developers/docs/references/content-management-api/).
|
|
76
|
+
|
|
77
|
+
**Note**: The CMA client instance returned by this hook is automatically scoped to the contentful space and environment in which it is called.
|
|
78
|
+
|
|
79
|
+
Usage:
|
|
80
|
+
|
|
81
|
+
```tsx
|
|
82
|
+
import { SDKProvider, useCMA } from '@contentful/react-apps-toolkit';
|
|
83
|
+
|
|
84
|
+
function ComponentUsingCMA() {
|
|
52
85
|
const cma = useCMA();
|
|
86
|
+
const [entries, setEntries] = useState();
|
|
53
87
|
|
|
54
88
|
useEffect(() => {
|
|
55
|
-
cma.
|
|
56
|
-
|
|
57
|
-
});
|
|
58
|
-
}, []);
|
|
89
|
+
cma.entries.getMany().then(setEntries);
|
|
90
|
+
}, [cma]);
|
|
59
91
|
|
|
60
|
-
return <>
|
|
92
|
+
return <>{entries?.length}</>;
|
|
61
93
|
}
|
|
62
94
|
|
|
95
|
+
function App() {
|
|
96
|
+
return (
|
|
97
|
+
<SDKProvider>
|
|
98
|
+
<ComponentUsingCMA />
|
|
99
|
+
</SDKProvider>
|
|
100
|
+
);
|
|
101
|
+
}
|
|
63
102
|
```
|
|
64
103
|
|
|
65
|
-
|
|
104
|
+
### useFieldValue
|
|
105
|
+
|
|
106
|
+
`useFieldValue` provides the current value, and a setter function for updating the current value, of a given field in Contentful. If used in the [field location](/developers/docs/extensibility/app-framework/locations/#entry-field), it will initialize using the current field id by default.
|
|
107
|
+
|
|
108
|
+
If used in the [entry sidebar location](/developers/docs/extensibility/app-framework/locations/#entry-sidebar), or the [entry editor location](/developers/docs/extensibility/app-framework/locations/#entry-editor), it must be passed a field ID to initialize.
|
|
109
|
+
|
|
110
|
+
`useFieldValue` also optionally accepts a locale, if the field has multiple locales. If no locale is passed, it will use the environment's default locale.
|
|
111
|
+
|
|
112
|
+
Usage:
|
|
66
113
|
|
|
67
|
-
|
|
114
|
+
```tsx
|
|
115
|
+
import { SDKProvider, useFieldValue } from '@contentful/react-apps-toolkit';
|
|
116
|
+
|
|
117
|
+
function ComponentUsingFieldValue() {
|
|
118
|
+
const [value, setValue] = useFieldValue('slug', 'en-US');
|
|
119
|
+
|
|
120
|
+
return <input value={value} onChange={(e) => setValue(e.target.value)} />;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function App() {
|
|
124
|
+
return (
|
|
125
|
+
<SDKProvider>
|
|
126
|
+
<ComponentUsingFieldValue />
|
|
127
|
+
</SDKProvider>
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
```
|
|
68
131
|
|
|
69
132
|
### Resources
|
|
70
133
|
|
|
71
|
-
- [
|
|
134
|
+
- [create-contentful-app](https://www.npmjs.com/package/create-contentful-app): A starter that makes it easy to bootstrap apps for Contentful.
|
package/dist/useCMA.js
CHANGED
|
@@ -11,10 +11,11 @@ var useSDK_1 = require("./useSDK");
|
|
|
11
11
|
function useCMA() {
|
|
12
12
|
var sdk = (0, useSDK_1.useSDK)();
|
|
13
13
|
var cma = (0, react_1.useMemo)(function () {
|
|
14
|
+
var _a;
|
|
14
15
|
return (0, contentful_management_1.createClient)({ apiAdapter: sdk.cmaAdapter }, {
|
|
15
16
|
type: 'plain',
|
|
16
17
|
defaults: {
|
|
17
|
-
environmentId: sdk.ids.environment,
|
|
18
|
+
environmentId: (_a = sdk.ids.environmentAlias) !== null && _a !== void 0 ? _a : sdk.ids.environment,
|
|
18
19
|
spaceId: sdk.ids.space,
|
|
19
20
|
},
|
|
20
21
|
});
|
package/dist/useFieldValue.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { SerializedJSONValue } from '@contentful/app-sdk';
|
|
1
2
|
export declare type UseFieldValueReturnValue<Value = unknown> = [
|
|
2
3
|
value: Value | undefined,
|
|
3
|
-
setValue: (newValue: Value | undefined) => Promise<
|
|
4
|
+
setValue: (newValue: Value | undefined) => Promise<SerializedJSONValue | undefined>
|
|
4
5
|
];
|
|
5
6
|
/**
|
|
6
7
|
* Returns field value and a function to update it.
|
package/dist/useFieldValue.js
CHANGED
|
@@ -61,9 +61,7 @@ function useFieldValue(fieldId, locale) {
|
|
|
61
61
|
case 0:
|
|
62
62
|
setValue(newValue);
|
|
63
63
|
return [4 /*yield*/, entryFieldApi.setValue(newValue, localeWithDefault)];
|
|
64
|
-
case 1:
|
|
65
|
-
_a.sent();
|
|
66
|
-
return [2 /*return*/];
|
|
64
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
67
65
|
}
|
|
68
66
|
});
|
|
69
67
|
}); }, [entryFieldApi, localeWithDefault]);
|
|
@@ -77,7 +77,11 @@ var useSDKMock = useSDK_1.useSDK;
|
|
|
77
77
|
beforeEach(function () {
|
|
78
78
|
jest.resetAllMocks();
|
|
79
79
|
mockSDK.entry.fields.fieldId.onValueChanged.mockImplementation(function () { return function () { }; });
|
|
80
|
+
mockSDK.entry.fields.fieldId.setValue.mockImplementation(function () { return Promise.resolve('return value'); });
|
|
80
81
|
mockSDK.entry.fields.otherFieldId.onValueChanged.mockImplementation(function () { return function () { }; });
|
|
82
|
+
mockSDK.entry.fields.otherFieldId.setValue.mockImplementation(function () {
|
|
83
|
+
return Promise.resolve('other return value');
|
|
84
|
+
});
|
|
81
85
|
});
|
|
82
86
|
describe('useFieldValue', function () {
|
|
83
87
|
describe('throws error', function () {
|
|
@@ -120,7 +124,16 @@ describe('useFieldValue', function () {
|
|
|
120
124
|
it('updates value', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
121
125
|
return __generator(this, function (_a) {
|
|
122
126
|
switch (_a.label) {
|
|
123
|
-
case 0: return [4 /*yield*/, (0, react_hooks_1.act)(function () { return
|
|
127
|
+
case 0: return [4 /*yield*/, (0, react_hooks_1.act)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
128
|
+
return __generator(this, function (_a) {
|
|
129
|
+
switch (_a.label) {
|
|
130
|
+
case 0: return [4 /*yield*/, result.current[1]('new value')];
|
|
131
|
+
case 1:
|
|
132
|
+
_a.sent();
|
|
133
|
+
return [2 /*return*/];
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}); })];
|
|
124
137
|
case 1:
|
|
125
138
|
_a.sent();
|
|
126
139
|
expect(result.current[0]).toBe('new value');
|
|
@@ -135,6 +148,27 @@ describe('useFieldValue', function () {
|
|
|
135
148
|
(0, react_hooks_1.act)(function () { return calls[0][1]('new value'); });
|
|
136
149
|
expect(result.current[0]).toBe('new value');
|
|
137
150
|
});
|
|
151
|
+
it('returns the updated value', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
152
|
+
var returnedValue;
|
|
153
|
+
return __generator(this, function (_a) {
|
|
154
|
+
switch (_a.label) {
|
|
155
|
+
case 0: return [4 /*yield*/, (0, react_hooks_1.act)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
156
|
+
return __generator(this, function (_a) {
|
|
157
|
+
switch (_a.label) {
|
|
158
|
+
case 0: return [4 /*yield*/, result.current[1]('new value')];
|
|
159
|
+
case 1:
|
|
160
|
+
returnedValue = _a.sent();
|
|
161
|
+
return [2 /*return*/];
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
}); })];
|
|
165
|
+
case 1:
|
|
166
|
+
_a.sent();
|
|
167
|
+
expect(returnedValue).toBe('return value');
|
|
168
|
+
return [2 /*return*/];
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
}); });
|
|
138
172
|
});
|
|
139
173
|
describe('with `fieldId`', function () {
|
|
140
174
|
var result;
|
|
@@ -149,7 +183,16 @@ describe('useFieldValue', function () {
|
|
|
149
183
|
it('updates value', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
150
184
|
return __generator(this, function (_a) {
|
|
151
185
|
switch (_a.label) {
|
|
152
|
-
case 0: return [4 /*yield*/, (0, react_hooks_1.act)(function () { return
|
|
186
|
+
case 0: return [4 /*yield*/, (0, react_hooks_1.act)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
187
|
+
return __generator(this, function (_a) {
|
|
188
|
+
switch (_a.label) {
|
|
189
|
+
case 0: return [4 /*yield*/, result.current[1]('new value')];
|
|
190
|
+
case 1:
|
|
191
|
+
_a.sent();
|
|
192
|
+
return [2 /*return*/];
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
}); })];
|
|
153
196
|
case 1:
|
|
154
197
|
_a.sent();
|
|
155
198
|
expect(result.current[0]).toBe('new value');
|
|
@@ -178,7 +221,16 @@ describe('useFieldValue', function () {
|
|
|
178
221
|
it('updates value', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
179
222
|
return __generator(this, function (_a) {
|
|
180
223
|
switch (_a.label) {
|
|
181
|
-
case 0: return [4 /*yield*/, (0, react_hooks_1.act)(function () { return
|
|
224
|
+
case 0: return [4 /*yield*/, (0, react_hooks_1.act)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
225
|
+
return __generator(this, function (_a) {
|
|
226
|
+
switch (_a.label) {
|
|
227
|
+
case 0: return [4 /*yield*/, result.current[1]('new value')];
|
|
228
|
+
case 1:
|
|
229
|
+
_a.sent();
|
|
230
|
+
return [2 /*return*/];
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
}); })];
|
|
182
234
|
case 1:
|
|
183
235
|
_a.sent();
|
|
184
236
|
expect(result.current[0]).toBe('new value');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentful/react-apps-toolkit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "Toolkit for building a Contentful app in React",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"author": "Contentful GmbH",
|
|
@@ -40,6 +40,7 @@
|
|
|
40
40
|
"@types/react": "^17.0.39",
|
|
41
41
|
"jest": "^27.5.0",
|
|
42
42
|
"react": "^17.0.2",
|
|
43
|
+
"react-test-renderer": "17.0.2",
|
|
43
44
|
"ts-jest": "^27.1.3",
|
|
44
45
|
"typescript": "^4.5.5"
|
|
45
46
|
},
|
|
@@ -50,5 +51,5 @@
|
|
|
50
51
|
"@contentful/app-sdk": "^4.0.0",
|
|
51
52
|
"contentful-management": ">=7.30.0"
|
|
52
53
|
},
|
|
53
|
-
"gitHead": "
|
|
54
|
+
"gitHead": "92d4d8ae34401669f4548d408f7168837a30a539"
|
|
54
55
|
}
|