Skip to content

Commit 096d337

Browse files
fix: fields with relationTo[] correctly load returned data from form submission (#3317)
1 parent 0bd3353 commit 096d337

File tree

5 files changed

+61
-16
lines changed

5 files changed

+61
-16
lines changed

src/admin/components/elements/DocumentDrawer/DrawerContent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const Content: React.FC<DocumentDrawerProps> = ({
9090

9191
const isEditing = Boolean(id);
9292
const apiURL = id ? `${serverURL}${api}/${collectionSlug}/${id}?locale=${locale}` : null;
93-
const action = `${serverURL}${api}/${collectionSlug}${id ? `/${id}` : ''}?locale=${locale}&depth=0&fallback-locale=null`;
93+
const action = `${serverURL}${api}/${collectionSlug}${id ? `/${id}` : ''}?locale=${locale}&fallback-locale=null`;
9494
const hasSavePermission = (isEditing && docPermissions?.update?.permission) || (!isEditing && (docPermissions as CollectionPermission)?.create?.permission);
9595
const isLoading = !internalState || !docPermissions || isLoadingDocument;
9696

src/admin/components/elements/Status/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ const Status: React.FC = () => {
6666
}
6767

6868
if (collection) {
69-
url = `${serverURL}${api}/${collection.slug}/${id}?depth=0&locale=${locale}&fallback-locale=null`;
69+
url = `${serverURL}${api}/${collection.slug}/${id}?locale=${locale}&fallback-locale=null`;
7070
method = 'patch';
7171
}
7272
if (global) {
73-
url = `${serverURL}${api}/globals/${global.slug}?depth=0&locale=${locale}&fallback-locale=null`;
73+
url = `${serverURL}${api}/globals/${global.slug}?locale=${locale}&fallback-locale=null`;
7474
method = 'post';
7575
}
7676

src/admin/components/forms/Form/buildStateFromSchema/addFieldStatePromise.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,12 @@ export const addFieldStatePromise = async ({
248248
case 'relationship': {
249249
if (field.hasMany) {
250250
const relationshipValue = Array.isArray(valueWithDefault) ? valueWithDefault.map((relationship) => {
251+
if (Array.isArray(field.relationTo)) {
252+
return {
253+
relationTo: relationship.relationTo,
254+
value: typeof relationship.value === 'string' ? relationship.value : relationship.value?.id,
255+
};
256+
}
251257
if (typeof relationship === 'object' && relationship !== null) {
252258
return relationship.id;
253259
}
@@ -256,16 +262,24 @@ export const addFieldStatePromise = async ({
256262

257263
fieldState.value = relationshipValue;
258264
fieldState.initialValue = relationshipValue;
259-
260-
state[`${path}${field.name}`] = fieldState;
265+
} else if (Array.isArray(field.relationTo)) {
266+
if (valueWithDefault && typeof valueWithDefault === 'object' && 'relationTo' in valueWithDefault && 'value' in valueWithDefault) {
267+
const value = typeof valueWithDefault?.value === 'object' && 'id' in valueWithDefault.value ? valueWithDefault.value.id : valueWithDefault.value;
268+
const relationshipValue = {
269+
relationTo: valueWithDefault?.relationTo,
270+
value,
271+
};
272+
fieldState.value = relationshipValue;
273+
fieldState.initialValue = relationshipValue;
274+
}
261275
} else {
262276
const relationshipValue = valueWithDefault && typeof valueWithDefault === 'object' && 'id' in valueWithDefault ? valueWithDefault.id : valueWithDefault;
263277
fieldState.value = relationshipValue;
264278
fieldState.initialValue = relationshipValue;
265-
266-
state[`${path}${field.name}`] = fieldState;
267279
}
268280

281+
state[`${path}${field.name}`] = fieldState;
282+
269283
break;
270284
}
271285

src/admin/components/views/Account/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const AccountView: React.FC = () => {
5959
const dataToRender = locationState?.data || data;
6060
const apiURL = `${serverURL}${api}/${slug}/${data?.id}?locale=${locale}`;
6161

62-
const action = `${serverURL}${api}/${slug}/${data?.id}?locale=${locale}&depth=0`;
62+
const action = `${serverURL}${api}/${slug}/${data?.id}?locale=${locale}`;
6363

6464
const onSave = React.useCallback(async (json: any) => {
6565
getDocPermissions();

test/fields-relationship/e2e.spec.ts

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,34 @@ describe('fields - relationship', () => {
123123
await expect(field).toContainText(relationOneDoc.id);
124124

125125
await saveDocAndAssert(page);
126+
await wait(200);
127+
await expect(field).toContainText(relationOneDoc.id);
128+
});
129+
130+
test('should create relations to multiple collections', async () => {
131+
await page.goto(url.create);
132+
133+
const field = page.locator('#field-relationshipMultiple');
134+
const value = page.locator('#field-relationshipMultiple .relationship--single-value__text');
135+
136+
await field.click({ delay: 100 });
137+
138+
const options = page.locator('.rs__option');
139+
140+
await expect(options).toHaveCount(3); // 3 docs
141+
142+
// Add one relationship
143+
await options.locator(`text=${relationOneDoc.id}`).click();
144+
await expect(value).toContainText(relationOneDoc.id);
145+
146+
// Add relationship of different collection
147+
await field.click({ delay: 100 });
148+
await options.locator(`text=${relationTwoDoc.id}`).click();
149+
await expect(value).toContainText(relationTwoDoc.id);
150+
151+
await saveDocAndAssert(page);
152+
await wait(200);
153+
await expect(value).toContainText(relationTwoDoc.id);
126154
});
127155

128156
test('should create hasMany relationship', async () => {
@@ -152,30 +180,33 @@ describe('fields - relationship', () => {
152180
await expect(page.locator('.rs__menu')).toHaveText('No options');
153181

154182
await saveDocAndAssert(page);
183+
await wait(200);
184+
await expect(values).toHaveText([relationOneDoc.id, anotherRelationOneDoc.id]);
155185
});
156186

157-
test('should create relations to multiple collections', async () => {
187+
test('should create many relations to multiple collections', async () => {
158188
await page.goto(url.create);
159189

160-
const field = page.locator('#field-relationshipMultiple');
161-
const value = page.locator('#field-relationshipMultiple .relationship--single-value__text');
162-
190+
const field = page.locator('#field-relationshipHasManyMultiple');
163191
await field.click({ delay: 100 });
164192

165193
const options = page.locator('.rs__option');
194+
await expect(options).toHaveCount(3);
166195

167-
await expect(options).toHaveCount(3); // 3 docs
196+
const values = page.locator('#field-relationshipHasManyMultiple .relationship--multi-value-label__text');
168197

169198
// Add one relationship
170199
await options.locator(`text=${relationOneDoc.id}`).click();
171-
await expect(value).toContainText(relationOneDoc.id);
200+
await expect(values).toHaveText([relationOneDoc.id]);
172201

173-
// Add relationship of different collection
202+
// Add second relationship
174203
await field.click({ delay: 100 });
175204
await options.locator(`text=${relationTwoDoc.id}`).click();
176-
await expect(value).toContainText(relationTwoDoc.id);
205+
await expect(values).toHaveText([relationOneDoc.id, relationTwoDoc.id]);
177206

178207
await saveDocAndAssert(page);
208+
await wait(200);
209+
await expect(values).toHaveText([relationOneDoc.id, relationTwoDoc.id]);
179210
});
180211

181212
test('should duplicate document with relationships', async () => {

0 commit comments

Comments
 (0)