Skip to content
This repository was archived by the owner on Sep 9, 2024. It is now read-only.

Commit 1ddcfbe

Browse files
authored
feat: update internal control value for list and object widget whenever store changes (#933)
1 parent 1d4ae8e commit 1ddcfbe

File tree

9 files changed

+49
-224
lines changed

9 files changed

+49
-224
lines changed

packages/core/src/backend.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import attempt from 'lodash/attempt';
33
import flatten from 'lodash/flatten';
44
import get from 'lodash/get';
55
import isError from 'lodash/isError';
6+
import set from 'lodash/set';
67
import uniq from 'lodash/uniq';
78
import { dirname } from 'path';
89

@@ -45,7 +46,6 @@ import filterEntries from './lib/util/filter.util';
4546
import { DRAFT_MEDIA_FILES, selectMediaFilePublicPath } from './lib/util/media.util';
4647
import { selectCustomPath, slugFromCustomPath } from './lib/util/nested.util';
4748
import { isNullish } from './lib/util/null.util';
48-
import { set } from './lib/util/object.util';
4949
import { fileSearch, sortByScore } from './lib/util/search.util';
5050
import { dateParsers, expandPath, extractTemplateVars } from './lib/widgets/stringTemplate';
5151
import createEntry from './valueObjects/createEntry';

packages/core/src/components/entry-editor/editor-control-pane/EditorControl.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,15 @@ const EditorControl = ({
110110
);
111111
const hidden = useHidden(field, entry, listItemPath);
112112

113+
useEffect(() => {
114+
if (!['list', 'object'].includes(field.widget)) {
115+
return;
116+
}
117+
118+
setInternalValue(finalStorageValue);
119+
// eslint-disable-next-line react-hooks/exhaustive-deps
120+
}, [finalStorageValue]);
121+
113122
useEffect(() => {
114123
if (hidden) {
115124
dispatch(changeDraftFieldValidation(path, [], i18n, isMeta));
@@ -220,10 +229,7 @@ const EditorControl = ({
220229
path,
221230
query,
222231
t,
223-
value:
224-
field.widget === 'list' || field.widget === 'object'
225-
? finalStorageValue
226-
: internalValue,
232+
value: internalValue,
227233
forList,
228234
listItemPath,
229235
forSingleList,

packages/core/src/lib/formatters.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import get from 'lodash/get';
2+
import set from 'lodash/set';
23

34
import { COMMIT_AUTHOR, COMMIT_DATE } from '../constants/commitProps';
45
import { sanitizeSlug } from './urlHelper';
56
import { selectIdentifier, selectInferredField } from './util/collection.util';
67
import { selectField } from './util/field.util';
7-
import { set } from './util/object.util';
88
import { isEmpty } from './util/string.util';
99
import {
1010
addFileTemplateFields,

packages/core/src/lib/i18n.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import escapeRegExp from 'lodash/escapeRegExp';
22
import get from 'lodash/get';
33
import groupBy from 'lodash/groupBy';
4+
import set from 'lodash/set';
45

56
import { fileForEntry, selectEntrySlug } from './util/collection.util';
6-
import { set } from './util/object.util';
77

88
import type {
99
BaseField,

packages/core/src/lib/util/__tests__/object.util.spec.ts

Lines changed: 0 additions & 159 deletions
This file was deleted.

packages/core/src/lib/util/object.util.ts

Lines changed: 0 additions & 45 deletions
This file was deleted.

packages/core/src/reducers/entries.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import once from 'lodash/once';
2+
import set from 'lodash/set';
23
import sortBy from 'lodash/sortBy';
34

45
import {
@@ -23,7 +24,6 @@ import {
2324
SORT_ENTRIES_SUCCESS,
2425
} from '../constants';
2526
import { VIEW_STYLES, VIEW_STYLE_TABLE } from '../constants/views';
26-
import { set } from '../lib/util/object.util';
2727

2828
import type { EntriesAction } from '../actions/entries';
2929
import type { SearchAction } from '../actions/search';

packages/core/src/reducers/entryDraft.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import isEqual from 'lodash/isEqual';
2+
import set from 'lodash/set';
3+
import cloneDeep from 'lodash/cloneDeep';
24
import { v4 as uuid } from 'uuid';
35

46
import {
@@ -23,7 +25,6 @@ import {
2325
import { duplicateI18nFields, getDataPath } from '../lib/i18n';
2426
import { fileForEntry } from '../lib/util/collection.util';
2527
import { applyDefaultsToDraftData } from '../lib/util/entry.util';
26-
import { set } from '../lib/util/object.util';
2728

2829
import type { EntriesAction } from '../actions/entries';
2930
import type { Entry, FieldsErrors } from '../interface';
@@ -70,7 +71,7 @@ function entryDraftReducer(
7071
...entry,
7172
data: applyDefaultsToDraftData(fields, undefined, entry.data),
7273
},
73-
original: entry,
74+
original: cloneDeep(entry),
7475
fieldsErrors: {},
7576
hasChanged: false,
7677
key: uuid(),
@@ -89,7 +90,7 @@ function entryDraftReducer(
8990
return {
9091
...newState,
9192
entry,
92-
original: entry,
93+
original: cloneDeep(entry),
9394
fieldsErrors: {},
9495
hasChanged: false,
9596
key: uuid(),
@@ -115,7 +116,7 @@ function entryDraftReducer(
115116
return {
116117
...state,
117118
entry,
118-
original: entry,
119+
original: cloneDeep(entry),
119120
fieldsErrors: {},
120121
hasChanged: true,
121122
key: uuid(),
@@ -135,7 +136,7 @@ function entryDraftReducer(
135136
return {
136137
...newState,
137138
entry,
138-
original: entry,
139+
original: cloneDeep(entry),
139140
fieldsErrors: {},
140141
hasChanged: true,
141142
};
@@ -203,24 +204,26 @@ function entryDraftReducer(
203204
? ['meta']
204205
: (i18n && getDataPath(i18n.currentLocale, i18n.defaultLocale)) || ['data'];
205206

207+
const newEntry = cloneDeep(newState.entry);
208+
206209
newState = {
207210
...newState,
208-
entry: set(newState.entry, `${dataPath.join('.')}.${path}`, value),
211+
entry: set(newEntry, `${dataPath.join('.')}.${path}`, value),
209212
};
210213

211214
if (i18n) {
212215
newState = duplicateI18nFields(newState, field, i18n.locales, i18n.defaultLocale, path);
213216
}
214217

215218
let hasChanged =
216-
!isEqual(newState.entry?.meta, newState.original?.meta) ||
217-
!isEqual(newState.entry?.data, newState.original?.data);
219+
!isEqual(newEntry?.meta, newState.original?.meta) ||
220+
!isEqual(newEntry?.data, newState.original?.data);
218221

219-
const i18nData = newState.entry?.i18n ?? {};
222+
const i18nData = newEntry?.i18n ?? {};
220223
for (const locale in i18nData) {
221224
hasChanged =
222225
hasChanged ||
223-
!isEqual(newState.entry?.i18n?.[locale]?.data, newState.original?.i18n?.[locale]?.data);
226+
!isEqual(newEntry?.i18n?.[locale]?.data, newState.original?.i18n?.[locale]?.data);
224227
}
225228

226229
return {
@@ -322,7 +325,7 @@ function entryDraftReducer(
322325
...newState,
323326
hasChanged: false,
324327
entry,
325-
original: entry,
328+
original: cloneDeep(entry),
326329
};
327330
}
328331

@@ -343,7 +346,7 @@ function entryDraftReducer(
343346
...newState,
344347
hasChanged: false,
345348
entry,
346-
original: entry,
349+
original: cloneDeep(entry),
347350
};
348351
}
349352

0 commit comments

Comments
 (0)