@contentful/react-apps-toolkit 0.5.11 → 0.7.0

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 CHANGED
@@ -1,12 +1,8 @@
1
- # In development
2
-
3
- This library is still in development and should not be used in production.
4
-
5
1
  # React Toolkit for Contentful Apps
6
2
 
7
- This package consists of helpers and hooks to create a Contentful app with React.
3
+ 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
4
 
9
- This library can be used in apps created by [`create-contentful-app`](https://www.npmjs.com/package/create-contentful-app), but can also be used with any other React app using Contentful's [App SDK](https://www.npmjs.com/package/@contentful/app-sdk).
5
+ 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
6
 
11
7
  ## Installation
12
8
 
@@ -20,52 +16,115 @@ yarn add @contentful/react-apps-toolkit
20
16
 
21
17
  The following hooks and utilities are exported from the package:
22
18
 
23
- ### `useSDK`
24
-
25
- This hook returns the App SDK.
19
+ ### SDKProvider
26
20
 
27
- The only requirement for using it is that the component that uses it is wrapped within the `SDKProvider`.
28
- If it is not, the hook will throw an error.
21
+ 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.
29
22
 
30
- Here is an example of how you can use it:
23
+ Usage:
31
24
 
32
25
  ```tsx
33
- import { useSDK } from '@contentful/react-apps-toolkit'
26
+ import { SDKProvider, useSDK } from '@contentful/react-apps-toolkit';
34
27
 
35
- function App() {
28
+ function ChildComponentUsingHook() {
36
29
  const sdk = useSDK<FieldExtensionSDK>();
37
30
 
38
- return <>App Id: {sdk.ids.app}</>
31
+ return <>App Id: {sdk.ids.app}</>;
39
32
  }
40
33
 
34
+ function App() {
35
+ return (
36
+ <SDKProvider>
37
+ <ChildComponentUsingHook />
38
+ </SDKProvider>
39
+ );
40
+ }
41
41
  ```
42
42
 
43
- ### `useCMA`
43
+ ### useSDK
44
+
45
+ `useSDK` returns an instance of the Contentful [App SDK](https://www.npmjs.com/package/@contentful/app-sdk).
44
46
 
45
- Returns an initialized [plain client](https://github.com/contentful/contentful-management.js/#alternative-plain-api) for the Contentful Management API, which can immediately be used to communicate with the rest of your Contentful space. [Contentful Management API docs](https://www.contentful.com/developers/docs/references/content-management-api/).
47
+ It must be wrapped it within the `SDKProvider`, otherwise, it will throw an error.
46
48
 
49
+ Usage:
47
50
 
48
51
  ```tsx
49
- import { useCMA } from '@contentful/react-apps-toolkit'
52
+ import { SDKProvider, useSDK } from '@contentful/react-apps-toolkit';
53
+
54
+ function ComponentUsingSDK() {
55
+ const sdk = useSDK<FieldExtensionSDK>();
56
+
57
+ return <>App Id: {sdk.ids.app}</>;
58
+ }
50
59
 
51
60
  function App() {
61
+ return (
62
+ <SDKProvider>
63
+ <ChildComponentUsingSDK />
64
+ </SDKProvider>
65
+ );
66
+ }
67
+ ```
68
+
69
+ ### useCMA
70
+
71
+ `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/).
72
+
73
+ **Note**: The CMA client instance returned by this hook is automatically scoped to the contentful space and environment in which it is called.
74
+
75
+ Usage:
76
+
77
+ ```tsx
78
+ import { SDKProvider, useCMA } from '@contentful/react-apps-toolkit';
79
+
80
+ function ComponentUsingCMA() {
52
81
  const cma = useCMA();
82
+ const [entries, setEntries] = useState();
53
83
 
54
84
  useEffect(() => {
55
- cma.entry.get({ entryId: '2VO9yOaeSyAFA19e649SxG' }).then((entry) => {
56
- console.log(entry);
57
- });
58
- }, []);
85
+ cma.entries.getMany().then(setEntries);
86
+ }, [cma]);
59
87
 
60
- return <>Hello world!</>
88
+ return <>{entries?.length}</>;
61
89
  }
62
90
 
91
+ function App() {
92
+ return (
93
+ <SDKProvider>
94
+ <ComponentUsingCMA />
95
+ </SDKProvider>
96
+ );
97
+ }
63
98
  ```
64
99
 
65
- #### SDKProvider
100
+ ### useFieldValue
101
+
102
+ `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.
103
+
104
+ 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.
105
+
106
+ `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.
107
+
108
+ Usage:
66
109
 
67
- Wrapper component, which makes the Apps SDK available to children via React Context. To use any of the hooks contained in this package, an application must be wrapped in the SDK provider, as all hooks depend on the Apps SDK.
110
+ ```tsx
111
+ import { SDKProvider, useFieldValue } from '@contentful/react-apps-toolkit';
112
+
113
+ function ComponentUsingFieldValue() {
114
+ const [value, setValue] = useFieldValue('slug', 'en-US');
115
+
116
+ return <input value={value} onChange={(e) => setValue(e.target.value)} />;
117
+ }
118
+
119
+ function App() {
120
+ return (
121
+ <SDKProvider>
122
+ <ComponentUsingFieldValue />
123
+ </SDKProvider>
124
+ );
125
+ }
126
+ ```
68
127
 
69
128
  ### Resources
70
129
 
71
- - [Create Contentful App](https://www.contentful.com/developers/docs/extensibility/app-framework/create-contentful-app/)
130
+ - [create-contentful-app](https://www.npmjs.com/package/create-contentful-app): A starter that makes it easy to bootstrap apps for Contentful.
@@ -1,6 +1,6 @@
1
- import React, { FC, ReactElement } from 'react';
2
1
  import { KnownSDK } from '@contentful/app-sdk';
3
- export declare const SDKContext: React.Context<{
2
+ import { FC, ReactElement } from 'react';
3
+ export declare const SDKContext: import("react").Context<{
4
4
  sdk: KnownSDK | null;
5
5
  }>;
6
6
  interface SDKProviderProps {
@@ -10,23 +10,20 @@ var __assign = (this && this.__assign) || function () {
10
10
  };
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
- var __importDefault = (this && this.__importDefault) || function (mod) {
14
- return (mod && mod.__esModule) ? mod : { "default": mod };
15
- };
16
13
  Object.defineProperty(exports, "__esModule", { value: true });
17
14
  exports.SDKProvider = exports.SDKContext = void 0;
18
15
  var jsx_runtime_1 = require("react/jsx-runtime");
19
- var react_1 = __importDefault(require("react"));
20
16
  var app_sdk_1 = require("@contentful/app-sdk");
21
- exports.SDKContext = react_1.default.createContext({ sdk: null });
17
+ var react_1 = require("react");
18
+ exports.SDKContext = (0, react_1.createContext)({ sdk: null });
22
19
  var DELAY_TIMEOUT = 4 * 1000;
23
20
  /**
24
21
  * The Component providing the AppSdk, the useSDK hook can only be used within this Provider
25
22
  * @param props.loading an optional loading element that gets rendered while initializing the app
26
23
  */
27
24
  var SDKProvider = function (props) {
28
- var _a = react_1.default.useState(), sdk = _a[0], setSDK = _a[1];
29
- react_1.default.useEffect(function () {
25
+ var _a = (0, react_1.useState)(), sdk = _a[0], setSDK = _a[1];
26
+ (0, react_1.useEffect)(function () {
30
27
  var timeout = window.setTimeout(function () {
31
28
  console.warn("Your app is taking longer than expected to initialize. If you think this is an error with Contentful's App SDK, let us know: https://github.com/contentful/ui-extensions-sdk/issues");
32
29
  }, DELAY_TIMEOUT);
@@ -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<void>
4
+ setValue: (newValue: Value | undefined) => Promise<SerializedJSONValue | undefined>
4
5
  ];
5
6
  /**
6
7
  * Returns field value and a function to update it.
@@ -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 result.current[1]('new value'); })];
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 result.current[1]('new value'); })];
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 result.current[1]('new value'); })];
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.5.11",
3
+ "version": "0.7.0",
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-dom": "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": "61e3382ccad0a4bfb72625f5681271667e15acd6"
54
+ "gitHead": "9b4d1a392a1018d6f11dd6b9589a313854173fd3"
54
55
  }