Skip to content

Commit 3ff10a9

Browse files
committed
Copy field value from source to target locales
1 parent ec8e983 commit 3ff10a9

File tree

5 files changed

+60
-8
lines changed

5 files changed

+60
-8
lines changed

apps/field-populator/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"start": "vite",
2121
"build": "tsc && vite build",
2222
"test": "vitest",
23+
"test:ci": "vitest run",
2324
"create-app-definition": "contentful-app-scripts create-app-definition",
2425
"add-locations": "contentful-app-scripts add-locations",
2526
"upload": "contentful-app-scripts upload --bundle-dir ./build",

apps/field-populator/src/locations/Dialog.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ const Dialog = () => {
2828
return;
2929
}
3030

31-
// todo : copy and paste logic to populate fields
32-
33-
sdk.close();
31+
sdk.close({
32+
sourceLocale: selectedSourceLocale,
33+
targetLocales: selectedTargetLocales.map((locale) => locale.code),
34+
});
3435
};
3536

3637
return (

apps/field-populator/src/locations/Sidebar.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,28 @@ const Sidebar = () => {
99
useAutoResizer();
1010

1111
const openDialog = async () => {
12-
return sdk.dialogs.openCurrentApp({
12+
const result = await sdk.dialogs.openCurrentApp({
1313
title: APP_NAME,
1414
width: 'large',
1515
minHeight: '340px',
1616
});
17+
if (!result) {
18+
return;
19+
}
20+
const { sourceLocale, targetLocales } = result as {
21+
sourceLocale: string;
22+
targetLocales: string[];
23+
};
24+
updateFields(sourceLocale, targetLocales);
25+
};
26+
27+
const updateFields = (sourceLocale: string, targetLocales: string[]) => {
28+
for (const fieldId of Object.keys(sdk.entry.fields)) {
29+
const newValue = sdk.entry.fields[fieldId].getValue(sourceLocale);
30+
for (const targetLocale of targetLocales) {
31+
sdk.entry.fields[fieldId].setValue(newValue, targetLocale);
32+
}
33+
}
1734
};
1835

1936
return (

apps/field-populator/test/Dialog.spec.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { act, render, screen, waitFor } from '@testing-library/react';
22
import userEvent from '@testing-library/user-event';
33
import { describe, expect, it, vi, beforeEach } from 'vitest';
4-
import { mockCma, mockSdk } from './mocks';
4+
import { mockSdk } from './mocks';
55
import Dialog from '../src/locations/Dialog';
66

77
vi.mock('@contentful/react-apps-toolkit', () => ({
88
useSDK: () => mockSdk,
9-
useCMA: () => mockCma,
109
useAutoResizer: () => {},
1110
}));
1211

apps/field-populator/test/Sidebar.spec.tsx

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { render, screen } from '@testing-library/react';
22
import { beforeEach, describe, expect, it, vi } from 'vitest';
3-
import { mockCma, mockSdk } from './mocks';
3+
import { mockSdk } from './mocks';
44
import Sidebar from '../src/locations/Sidebar';
55
import userEvent from '@testing-library/user-event';
66
import { APP_NAME } from '../src/utils/consts';
77

88
vi.mock('@contentful/react-apps-toolkit', () => ({
99
useSDK: () => mockSdk,
10-
useCMA: () => mockCma,
1110
useAutoResizer: () => {},
1211
}));
1312

@@ -33,4 +32,39 @@ describe('Sidebar component', () => {
3332

3433
expect(mockSdk.dialogs.openCurrentApp).toHaveBeenCalledOnce();
3534
});
35+
36+
it('should copy field values from source locale to target locales', async () => {
37+
const user = userEvent.setup();
38+
const titleGetValue = vi.fn().mockReturnValue('Hello');
39+
const titleSetValue = vi.fn();
40+
const bodyGetValue = vi.fn().mockReturnValue('World');
41+
const bodySetValue = vi.fn();
42+
43+
mockSdk.entry = {
44+
fields: {
45+
title: {
46+
getValue: titleGetValue,
47+
setValue: titleSetValue,
48+
},
49+
body: {
50+
getValue: bodyGetValue,
51+
setValue: bodySetValue,
52+
},
53+
},
54+
};
55+
56+
mockSdk.dialogs.openCurrentApp.mockResolvedValue({
57+
sourceLocale: 'en-US',
58+
targetLocales: ['de', 'fr'],
59+
});
60+
61+
render(<Sidebar />);
62+
63+
await user.click(screen.getByRole('button', { name: APP_NAME }));
64+
65+
expect(titleSetValue).toHaveBeenCalledWith('Hello', 'de');
66+
expect(titleSetValue).toHaveBeenCalledWith('Hello', 'fr');
67+
expect(bodySetValue).toHaveBeenCalledWith('World', 'de');
68+
expect(bodySetValue).toHaveBeenCalledWith('World', 'fr');
69+
});
3670
});

0 commit comments

Comments
 (0)