Skip to content

Commit b15317a

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
https://web.tracklify.com/project/2b7ZVgE5/AdminForth/1254/ZDp08cU0/disable-this-message-when-no-c
1 parent 254f7a3 commit b15317a

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

adminforth/spa/src/utils/utils.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { i18nInstance } from '../i18n'
1313

1414

1515

16+
1617
const LS_LANG_KEY = `afLanguage`;
1718
const MAX_CONSECUTIVE_EMPTY_RESULTS = 2;
1819
const ITEMS_PER_PAGE_LIMIT = 100;
@@ -522,3 +523,42 @@ export function btoa_function(source: string): string {
522523
export function atob_function(source: string): string {
523524
return atob(source);
524525
}
526+
527+
export function compareOldAndNewRecord(oldRecord: Record<string, any>, newRecord: Record<string, any>): boolean {
528+
const newKeys = Object.keys(newRecord);
529+
const coreStore = useCoreStore();
530+
531+
for (const key of newKeys) {
532+
if (oldRecord[key] !== newRecord[key]) {
533+
if (
534+
(
535+
oldRecord[key] === undefined ||
536+
oldRecord[key] === null ||
537+
oldRecord[key] === '' ||
538+
(Array.isArray(oldRecord[key]) && oldRecord[key].length === 0)
539+
)
540+
&&
541+
(
542+
newRecord[key] === undefined ||
543+
newRecord[key] === null ||
544+
newRecord[key] === '' ||
545+
(Array.isArray(newRecord[key]) && newRecord[key].length === 0)
546+
)
547+
) {
548+
// console.log(`Value for key ${key} is considered equal (empty)`)
549+
continue;
550+
}
551+
552+
const column = coreStore.resource.columns.find((c) => c.name === key);
553+
if (column?.foreignResource) {
554+
if (newRecord[key] === oldRecord[key]?.pk) {
555+
// console.log(`Value for key ${key} is considered equal (foreign key)`)
556+
continue;
557+
}
558+
}
559+
// console.log(`Value for key ${key} is different`, { oldValue: oldRecord[key], newValue: newRecord[key] });
560+
return false;
561+
}
562+
}
563+
return true;
564+
}

adminforth/spa/src/views/CreateView.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ import BreadcrumbsWithButtons from '@/components/BreadcrumbsWithButtons.vue';
7979
import ResourceForm from '@/components/ResourceForm.vue';
8080
import SingleSkeletLoader from '@/components/SingleSkeletLoader.vue';
8181
import { useCoreStore } from '@/stores/core';
82-
import { callAdminForthApi, getCustomComponent,checkAcessByAllowedActions, initThreeDotsDropdown, checkShowIf } from '@/utils';
82+
import { callAdminForthApi, getCustomComponent,checkAcessByAllowedActions, initThreeDotsDropdown, checkShowIf, compareOldAndNewRecord } from '@/utils';
8383
import { IconFloppyDiskSolid } from '@iconify-prerendered/vue-flowbite';
8484
import { onMounted, onBeforeMount, onBeforeUnmount, ref, watch, nextTick } from 'vue';
8585
import { useRoute, useRouter, onBeforeRouteLeave } from 'vue-router';
@@ -121,7 +121,7 @@ async function onUpdateRecord(newRecord: any) {
121121
}
122122
123123
function checkIfWeCanLeavePage() {
124-
return wasSaveSuccessful.value || cancelButtonClicked.value || JSON.stringify(record.value) === JSON.stringify(initialValues.value);
124+
return wasSaveSuccessful.value || cancelButtonClicked.value || compareOldAndNewRecord(initialValues.value, record.value);
125125
}
126126
127127
function onBeforeUnload(event: BeforeUnloadEvent) {

adminforth/spa/src/views/EditView.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ import BreadcrumbsWithButtons from '@/components/BreadcrumbsWithButtons.vue';
7474
import ResourceForm from '@/components/ResourceForm.vue';
7575
import SingleSkeletLoader from '@/components/SingleSkeletLoader.vue';
7676
import { useCoreStore } from '@/stores/core';
77-
import { callAdminForthApi, getCustomComponent,checkAcessByAllowedActions, initThreeDotsDropdown } from '@/utils';
77+
import { callAdminForthApi, getCustomComponent,checkAcessByAllowedActions, initThreeDotsDropdown, compareOldAndNewRecord } from '@/utils';
7878
import { IconFloppyDiskSolid } from '@iconify-prerendered/vue-flowbite';
7979
import { computed, onMounted, onBeforeMount, ref, type Ref, nextTick, watch, onBeforeUnmount } from 'vue';
8080
import { useRoute, useRouter, onBeforeRouteLeave } from 'vue-router';
@@ -113,7 +113,7 @@ function onBeforeUnload(event: BeforeUnloadEvent) {
113113
}
114114
115115
function checkIfWeCanLeavePage() {
116-
return wasSaveSuccessful.value || cancelButtonClicked.value || JSON.stringify(record.value) === JSON.stringify(initialRecord.value);
116+
return wasSaveSuccessful.value || cancelButtonClicked.value || compareOldAndNewRecord(initialRecord.value, record.value);
117117
}
118118
119119
window.addEventListener('beforeunload', onBeforeUnload);

0 commit comments

Comments
 (0)