1
1
<script setup>
2
- import { onBeforeMount , onUnmounted , ref , watch } from ' vue'
2
+ import { onBeforeMount , onUnmounted , ref } from ' vue'
3
3
import { router } from ' @inertiajs/vue3'
4
- import { usePage } from ' @inertiajs/vue3'
5
4
import { useModalStack } from ' ./modalStack'
6
5
import ModalRenderer from ' ./ModalRenderer.vue'
7
6
import { default as Axios } from ' axios'
8
7
9
8
const modalStack = useModalStack ()
10
9
11
10
const isNavigating = ref (false )
11
+ const previousModalOnBase = ref (null )
12
+
12
13
onUnmounted (router .on (' start' , () => (isNavigating .value = true )))
13
14
onUnmounted (router .on (' finish' , () => (isNavigating .value = false )))
15
+ onUnmounted (
16
+ router .on (' navigate' , ($event ) => {
17
+ const modalOnBase = $event .detail .page .props ._inertiaui_modal
14
18
15
- const axiosRequestInterceptor = (config ) => {
16
- // A Modal is opened on top of a base route, so we need to pass this base route
17
- // so it can redirect back with the back() helper method...
18
- config .headers [' X-InertiaUI-Modal-Base-Url' ] = modalStack .getBaseUrl ()
19
-
20
- return config
21
- }
22
-
23
- onBeforeMount (() => {
24
- Axios .interceptors .request .use (axiosRequestInterceptor)
25
- })
26
-
27
- onUnmounted (() => {
28
- Axios .interceptors .request .eject (axiosRequestInterceptor)
29
- })
30
-
31
- const $page = usePage ()
32
-
33
- watch (
34
- () => $page .props ? ._inertiaui_modal ,
35
- (modalOnBase ) => {
36
19
if (! modalOnBase) {
37
- modalStack .closeAll ()
20
+ previousModalOnBase . value && modalStack .closeAll ()
38
21
return
39
22
}
40
23
41
- // TODO: Is this really necessary?
24
+ previousModalOnBase . value = modalOnBase
42
25
modalStack .setBaseUrl (modalOnBase .baseUrl )
43
26
44
27
modalStack .pushFromResponseData (modalOnBase, {}, () => {
@@ -54,9 +37,24 @@ watch(
54
37
})
55
38
}
56
39
})
57
- },
58
- { immediate: true },
40
+ }),
59
41
)
42
+
43
+ const axiosRequestInterceptor = (config ) => {
44
+ // A Modal is opened on top of a base route, so we need to pass this base route
45
+ // so it can redirect back with the back() helper method...
46
+ config .headers [' X-InertiaUI-Modal-Base-Url' ] = modalStack .getBaseUrl ()
47
+
48
+ return config
49
+ }
50
+
51
+ onBeforeMount (() => {
52
+ Axios .interceptors .request .use (axiosRequestInterceptor)
53
+ })
54
+
55
+ onUnmounted (() => {
56
+ Axios .interceptors .request .eject (axiosRequestInterceptor)
57
+ })
60
58
</script >
61
59
62
60
<template >
0 commit comments