Skip to content

Commit 6d74c63

Browse files
committed
fix(#7794): populate relation widget from url
The createEmptyDraft action creator didn't combine repeated query params into a List, instead returning the last param. Now relation widgets with isMultiple set will populate correctly.
1 parent 0a35acb commit 6d74c63

3 files changed

Lines changed: 59 additions & 8 deletions

File tree

packages/decap-cms-core/src/actions/__tests__/entries.spec.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fromJS, Map } from 'immutable';
1+
import { fromJS, List, Map } from 'immutable';
22
import configureMockStore from 'redux-mock-store';
33
import thunk from 'redux-thunk';
44

@@ -99,6 +99,41 @@ describe('entries', () => {
9999
});
100100
});
101101

102+
it('should populate draft entry from repeated URL param', () => {
103+
const store = mockStore({ mediaLibrary: fromJS({ files: [] }) });
104+
105+
const collection = fromJS({
106+
fields: [{ name: 'post', multiple: true }],
107+
});
108+
109+
return store
110+
.dispatch(createEmptyDraft(collection, '?post=2026-05-07-test&post=2026-05-08-test'))
111+
.then(() => {
112+
const actions = store.getActions();
113+
expect(actions).toHaveLength(1);
114+
115+
expect(actions[0]).toEqual({
116+
payload: {
117+
author: '',
118+
collection: undefined,
119+
data: { post: List(['2026-05-07-test', '2026-05-08-test']) },
120+
meta: {},
121+
i18n: {},
122+
isModification: null,
123+
label: null,
124+
mediaFiles: [],
125+
partial: false,
126+
path: '',
127+
raw: '',
128+
slug: '',
129+
status: '',
130+
updatedOn: '',
131+
},
132+
type: 'DRAFT_CREATE_EMPTY',
133+
});
134+
});
135+
});
136+
102137
it('should html escape URL params', () => {
103138
const store = mockStore({ mediaLibrary: fromJS({ files: [] }) });
104139

packages/decap-cms-core/src/actions/entries.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
import { fromJS, List, Map } from 'immutable';
1+
import { fromJS, List, Map, Set } from 'immutable';
22
import isEqual from 'lodash/isEqual';
33
import { Cursor } from 'decap-cms-lib-util';
44

55
import { selectCollectionEntriesCursor } from '../reducers/cursors';
6-
import { selectFields, updateFieldByKey, selectDefaultSortField } from '../reducers/collections';
6+
import {
7+
selectFields,
8+
selectField,
9+
updateFieldByKey,
10+
selectDefaultSortField,
11+
} from '../reducers/collections';
712
import { selectIntegration, selectPublishedSlugs } from '../reducers';
813
import { getIntegrationProvider } from '../integrations';
914
import { currentBackend } from '../backend';
@@ -38,7 +43,6 @@ import type {
3843
import type { EntryValue } from '../valueObjects/Entry';
3944
import type { Backend } from '../backend';
4045
import type AssetProxy from '../valueObjects/AssetProxy';
41-
import type { Set } from 'immutable';
4246

4347
/*
4448
* Constant Declarations
@@ -740,10 +744,21 @@ function getMetaFields(fields: EntryFields) {
740744
export function createEmptyDraft(collection: Collection, search: string) {
741745
return async (dispatch: ThunkDispatch<State, {}, AnyAction>, getState: () => State) => {
742746
const params = new URLSearchParams(search);
743-
params.forEach((value, key) => {
744-
collection = updateFieldByKey(collection, key, field =>
745-
field.set('default', processValue(value)),
746-
);
747+
const uniqueKeys = Set([...params.keys()]).toArray();
748+
749+
uniqueKeys.forEach(key => {
750+
const field = selectField(collection, key);
751+
const isMultiple = field?.get('multiple', false);
752+
const values = params.getAll(key);
753+
754+
collection = updateFieldByKey(collection, key, field => {
755+
if (isMultiple) {
756+
const allValues = values.flatMap(v => v.split(',')).map(processValue);
757+
return field.set('default', List(allValues));
758+
} else {
759+
return field.set('default', processValue(values[values.length - 1]));
760+
}
761+
});
747762
});
748763

749764
const fields = collection.get('fields', List());

packages/decap-cms-core/src/types/redux.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ export type EntryField = StaticallyTypedRecord<{
596596
name: string;
597597
default: string | null | boolean | List<unknown>;
598598
media_folder?: string;
599+
multiple?: boolean;
599600
public_folder?: string;
600601
comment?: string;
601602
meta?: boolean;

0 commit comments

Comments
 (0)