@frontify/guideline-blocks-settings 0.31.1 → 0.31.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/CHANGELOG.md +13 -0
- package/dist/hooks/useAttachments.es.js +38 -39
- package/dist/hooks/useAttachments.es.js.map +1 -1
- package/dist/index.cjs.js +2 -2
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +1 -3
- package/dist/index.umd.js +3 -3
- package/dist/index.umd.js.map +1 -1
- package/package.json +2 -2
- package/setupTests.ts +6 -1
- package/src/hooks/useAttachments.spec.tsx +4 -8
- package/src/hooks/useAttachments.tsx +10 -15
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frontify/guideline-blocks-settings",
|
|
3
|
-
"version": "0.31.
|
|
3
|
+
"version": "0.31.3",
|
|
4
4
|
"description": "Provides types and helpers for the guideline block development",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "dist/index.umd.js",
|
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
"@frontify/sidebar-settings": "^0.9.1"
|
|
66
66
|
},
|
|
67
67
|
"peerDependencies": {
|
|
68
|
-
"@frontify/app-bridge": "^3.0.0",
|
|
68
|
+
"@frontify/app-bridge": "^3.0.0 || ^4.0.0-alpha.0",
|
|
69
69
|
"react": "^18",
|
|
70
70
|
"react-dom": "^18"
|
|
71
71
|
},
|
package/setupTests.ts
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
/* (c) Copyright Frontify Ltd., all rights reserved. */
|
|
2
2
|
|
|
3
|
-
import { afterEach, vi } from 'vitest';
|
|
3
|
+
import { afterEach, beforeAll, vi } from 'vitest';
|
|
4
|
+
import { configure } from '@testing-library/react';
|
|
4
5
|
|
|
5
6
|
vi.stubGlobal('crypto', {
|
|
6
7
|
getRandomValues: vi.fn(),
|
|
7
8
|
});
|
|
8
9
|
|
|
10
|
+
beforeAll(() => {
|
|
11
|
+
configure({ testIdAttribute: 'data-test-id' });
|
|
12
|
+
});
|
|
13
|
+
|
|
9
14
|
afterEach(() => {
|
|
10
15
|
vi.restoreAllMocks();
|
|
11
16
|
});
|
|
@@ -8,11 +8,7 @@ import { AttachmentsProvider, useAttachments, useAttachmentsContext, withAttachm
|
|
|
8
8
|
|
|
9
9
|
const MOCK_SETTINGS_ID = 'attachments';
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
* @vitest-environment happy-dom
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
describe('useAttachments', () => {
|
|
11
|
+
describe('useAttachments', async () => {
|
|
16
12
|
it('should have 1 attachment if attachment is added', async () => {
|
|
17
13
|
const STUB_WITH_NO_ASSETS = getAppBridgeBlockStub({
|
|
18
14
|
blockId: 1,
|
|
@@ -115,7 +111,7 @@ describe('withAttachmentsProvider', () => {
|
|
|
115
111
|
return (
|
|
116
112
|
<ul>
|
|
117
113
|
{context.attachments.map((attachment) => (
|
|
118
|
-
<li key={attachment.id}
|
|
114
|
+
<li key={attachment.id} data-test-id="item-test">
|
|
119
115
|
{attachment.id}
|
|
120
116
|
</li>
|
|
121
117
|
))}
|
|
@@ -123,8 +119,8 @@ describe('withAttachmentsProvider', () => {
|
|
|
123
119
|
);
|
|
124
120
|
}, MOCK_SETTINGS_ID);
|
|
125
121
|
|
|
126
|
-
const {
|
|
122
|
+
const { getAllByTestId } = render(<Component appBridge={STUB_WITH_THREE_ASSETS} />);
|
|
127
123
|
|
|
128
|
-
await waitFor(() => expect(
|
|
124
|
+
await waitFor(() => expect(getAllByTestId('item-test')).toHaveLength(3));
|
|
129
125
|
});
|
|
130
126
|
});
|
|
@@ -5,24 +5,19 @@ import { type ReactNode, createContext, useContext } from 'react';
|
|
|
5
5
|
|
|
6
6
|
import { type BlockProps } from '../index';
|
|
7
7
|
|
|
8
|
-
export const useAttachments = (appBridge: AppBridgeBlock,
|
|
9
|
-
const { blockAssets, updateAssetIdsFromKey } = useBlockAssets(appBridge);
|
|
10
|
-
const attachments = blockAssets?.[
|
|
8
|
+
export const useAttachments = (appBridge: AppBridgeBlock, attachmentKey: string) => {
|
|
9
|
+
const { blockAssets, addAssetIdsToKey, deleteAssetIdsFromKey, updateAssetIdsFromKey } = useBlockAssets(appBridge);
|
|
10
|
+
const attachments = blockAssets?.[attachmentKey] || [];
|
|
11
11
|
|
|
12
12
|
const onAttachmentsAdd = async (newAssets: Asset[]) => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
await updateAssetIdsFromKey(assetId, newAssetIds);
|
|
13
|
+
await addAssetIdsToKey(
|
|
14
|
+
attachmentKey,
|
|
15
|
+
newAssets.map((asset) => asset.id),
|
|
16
|
+
);
|
|
18
17
|
};
|
|
19
18
|
|
|
20
19
|
const onAttachmentDelete = async (assetToDelete: Asset) => {
|
|
21
|
-
|
|
22
|
-
.filter((attachment) => attachment.id !== assetToDelete.id)
|
|
23
|
-
.map((attachment) => attachment.id);
|
|
24
|
-
|
|
25
|
-
await updateAssetIdsFromKey(assetId, newAssetIds);
|
|
20
|
+
await deleteAssetIdsFromKey(attachmentKey, [assetToDelete.id]);
|
|
26
21
|
};
|
|
27
22
|
|
|
28
23
|
const onAttachmentReplace = async (attachmentToReplace: Asset, newAsset: Asset) => {
|
|
@@ -30,13 +25,13 @@ export const useAttachments = (appBridge: AppBridgeBlock, assetId: string) => {
|
|
|
30
25
|
attachment.id === attachmentToReplace.id ? newAsset.id : attachment.id,
|
|
31
26
|
);
|
|
32
27
|
|
|
33
|
-
await updateAssetIdsFromKey(
|
|
28
|
+
await updateAssetIdsFromKey(attachmentKey, newAssetIds);
|
|
34
29
|
};
|
|
35
30
|
|
|
36
31
|
const onAttachmentsSorted = async (assets: Asset[]) => {
|
|
37
32
|
const newAssetIds = assets.map((asset) => asset.id);
|
|
38
33
|
|
|
39
|
-
await updateAssetIdsFromKey(
|
|
34
|
+
await updateAssetIdsFromKey(attachmentKey, newAssetIds);
|
|
40
35
|
};
|
|
41
36
|
|
|
42
37
|
return {
|