|
12 | 12 |
|
13 | 13 | <BreadcrumbsWithButtons> |
14 | 14 | <!-- save and cancle --> |
15 | | - <button @click="$router.back()" |
| 15 | + <button @click="() => {cancelButtonClicked = true; $router.back()}" |
16 | 16 | class="af-cancel-button flex items-center py-1 px-3 me-2 text-sm font-medium rounded-default text-lightCreateViewButtonText focus:outline-none bg-lightCreateViewButtonBackground rounded border border-lightCreateViewButtonBorder hover:bg-lightCreateViewButtonBackgroundHover hover:text-lightCreateViewButtonTextHover focus:z-10 focus:ring-4 focus:ring-lightCreateViewButtonFocusRing dark:focus:ring-darkCreateViewButtonFocusRing dark:bg-darkCreateViewButtonBackground dark:text-darkCreateViewButtonText dark:border-darkCreateViewButtonBorder dark:hover:text-darkCreateViewButtonTextHover dark:hover:bg-darkCreateViewButtonBackgroundHover" |
17 | 17 | > |
18 | 18 | {{ $t('Cancel') }} |
@@ -81,8 +81,8 @@ import SingleSkeletLoader from '@/components/SingleSkeletLoader.vue'; |
81 | 81 | import { useCoreStore } from '@/stores/core'; |
82 | 82 | import { callAdminForthApi, getCustomComponent,checkAcessByAllowedActions, initThreeDotsDropdown, checkShowIf } from '@/utils'; |
83 | 83 | import { IconFloppyDiskSolid } from '@iconify-prerendered/vue-flowbite'; |
84 | | -import { onMounted, onBeforeMount, ref, watch, nextTick } from 'vue'; |
85 | | -import { useRoute, useRouter } from 'vue-router'; |
| 84 | +import { onMounted, onBeforeMount, onBeforeUnmount, ref, watch, nextTick } from 'vue'; |
| 85 | +import { useRoute, useRouter, onBeforeRouteLeave } from 'vue-router'; |
86 | 86 | import { computed } from 'vue'; |
87 | 87 | import { showErrorTost } from '@/composables/useFrontendApi'; |
88 | 88 | import ThreeDotsMenu from '@/components/ThreeDotsMenu.vue'; |
@@ -113,11 +113,36 @@ const initialValues = ref({}); |
113 | 113 |
|
114 | 114 | const readonlyColumns = ref([]); |
115 | 115 |
|
| 116 | +const cancelButtonClicked = ref(false); |
| 117 | +const wasSaveSuccessful = ref(false); |
116 | 118 |
|
117 | 119 | async function onUpdateRecord(newRecord: any) { |
118 | 120 | record.value = newRecord; |
119 | 121 | } |
120 | 122 |
|
| 123 | +function checkIfWeCanLeavePage() { |
| 124 | + return cancelButtonClicked.value || JSON.stringify(record.value) === JSON.stringify(initialValues.value); |
| 125 | +} |
| 126 | +
|
| 127 | +window.addEventListener('beforeunload', (event) => { |
| 128 | + if (!checkIfWeCanLeavePage()) { |
| 129 | + event.preventDefault(); |
| 130 | + event.returnValue = ''; |
| 131 | + } |
| 132 | +}); |
| 133 | +
|
| 134 | +onBeforeUnmount(() => { |
| 135 | + window.removeEventListener('beforeunload', () => {}); |
| 136 | +}); |
| 137 | +
|
| 138 | +onBeforeRouteLeave((to, from, next) => { |
| 139 | + if (!checkIfWeCanLeavePage()) { |
| 140 | + const answer = confirm('There are unsaved changes. Are you sure you want to leave this page?'); |
| 141 | + if (!answer) return next(false); |
| 142 | + } |
| 143 | + next(); |
| 144 | +}); |
| 145 | +
|
121 | 146 | onBeforeMount(() => { |
122 | 147 | clearSaveInterceptors(route.params.resourceId as string); |
123 | 148 | }); |
|
0 commit comments