Skip to content

Commit d2fa912

Browse files
committed
fix: improoved detection of changes in create/edit view to avoid notification "There are unsaved changes..." when there are no any changes for JSON fields
https://web.tracklify.com/project/2b7ZVgE5/AdminForth/1234/cuxniiDH/changes-were-not-saved-too-ear
1 parent c1e3f8a commit d2fa912

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

adminforth/spa/src/utils/utils.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -529,20 +529,22 @@ export function compareOldAndNewRecord(oldRecord: Record<string, any>, newRecord
529529
const coreStore = useCoreStore();
530530

531531
for (const key of newKeys) {
532-
if (oldRecord[key] !== newRecord[key]) {
532+
const oldValue = typeof oldRecord[key] === 'object' && oldRecord[key] !== null ? JSON.stringify(oldRecord[key]) : oldRecord[key];
533+
const newValue = typeof newRecord[key] === 'object' && newRecord[key] !== null ? JSON.stringify(newRecord[key]) : newRecord[key];
534+
if (oldValue !== newValue) {
533535
if (
534536
(
535-
oldRecord[key] === undefined ||
536-
oldRecord[key] === null ||
537-
oldRecord[key] === '' ||
538-
(Array.isArray(oldRecord[key]) && oldRecord[key].length === 0)
537+
oldValue === undefined ||
538+
oldValue === null ||
539+
oldValue === '' ||
540+
(Array.isArray(oldValue) && oldValue.length === 0)
539541
)
540542
&&
541543
(
542-
newRecord[key] === undefined ||
543-
newRecord[key] === null ||
544-
newRecord[key] === '' ||
545-
(Array.isArray(newRecord[key]) && newRecord[key].length === 0)
544+
newValue === undefined ||
545+
newValue === null ||
546+
newValue === '' ||
547+
(Array.isArray(newValue) && newValue.length === 0)
546548
)
547549
) {
548550
// console.log(`Value for key ${key} is considered equal (empty)`)
@@ -556,7 +558,7 @@ export function compareOldAndNewRecord(oldRecord: Record<string, any>, newRecord
556558
continue;
557559
}
558560
}
559-
// console.log(`Value for key ${key} is different`, { oldValue: oldRecord[key], newValue: newRecord[key] });
561+
// console.log(`Value for key ${key} is different`, { oldValue: oldValue, newValue: newValue });
560562
return false;
561563
}
562564
}

0 commit comments

Comments
 (0)