Skip to content

Commit

Permalink
Fix issue when using Nova under a /path (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
milewski authored Oct 2, 2023
1 parent 1e3a1d6 commit f03ec59
Show file tree
Hide file tree
Showing 10 changed files with 254 additions and 246 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class ExampleNovaResource extends Resource {
return [
ResourceNavigationField::make('Information'), // show all the available cards by default
ResourceNavigationField::make('Activities')->withCards([ DailySalesCard::class, ClientProfileCard::class ]), // only show these cards when this tab is active
ResourceNavigationField::make('Settings')->withoutCards([ ... ]), // hide all cards when this tab is active
ResourceNavigationField::make('Settings')->withoutCards(), // hide all cards when this tab is active
];
}

Expand Down
2 changes: 1 addition & 1 deletion dist/js/card.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions dist/js/card.js.LICENSE

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"@vue/compiler-sfc": "^3.3.4",
"laravel-mix": "^6.0.49",
"mix-tailwindcss": "^1.3.0",
"postcss": "^8.4.29",
"postcss": "^8.4.31",
"tailwindcss": "^3.3.3",
"vue-loader": "^17.2.2"
}
Expand Down
47 changes: 47 additions & 0 deletions resources/js/components/RequestHighjacker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const novaRequest = Nova.request

const interceptors = []
const interceptorsInstance = []

Nova.request = (...params) => {

for (const param of params) {

for (const interceptor of interceptors) {
interceptor(param)
}

}

const axiosInstance = novaRequest(...params)

if (axiosInstance instanceof Promise) {
return axiosInstance
}

for (const interceptor of interceptors) {

interceptorsInstance.push({
instance: axiosInstance,
interceptor: axiosInstance.interceptors.request.use(config => interceptor(config)),
})

}

return axiosInstance

}

function cleanUpInterceptors() {

for (const { instance, interceptor } of interceptorsInstance) {
instance.interceptors.request.eject(interceptor)
}

while (interceptors.length) {
interceptors.pop()
}

}

export { interceptors, cleanUpInterceptors }
55 changes: 16 additions & 39 deletions resources/js/components/ResourceNavigationCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,50 +21,26 @@

<script>
const novaRequest = Nova.request
import { interceptors } from './RequestHighjacker'
const interceptors = []
const interceptorsInstance = []
Nova.request = (...params) => {
interceptors.push(config => {
for (const param of params) {
try {
for (const interceptor of interceptors) {
interceptor(param)
if (config.params === undefined || config.params === null) {
config.params = {}
}
}
const searchParams = new URLSearchParams(window.location.search)
const axiosInstance = novaRequest(...params)
if (axiosInstance instanceof Promise) {
return axiosInstance
}
for (const interceptor of interceptors) {
interceptorsInstance.push({
instance: axiosInstance,
interceptor: axiosInstance.interceptors.request.use(config => interceptor(config)),
})
}
return axiosInstance
}
interceptors.push(config => {
if (searchParams.has('x-tab')) {
config.params[ 'x-resource-navigation-tab' ] = searchParams.get('x-tab')
}
if (config.params === undefined) {
config.params = {}
}
} catch (error) {
const searchParams = new URLSearchParams(window.location.search)
console.log('ResourceNavigationCard.vue error:', error)
if (searchParams.has('x-tab')) {
config.params[ 'x-resource-navigation-tab' ] = searchParams.get('x-tab')
}
return config
Expand All @@ -78,17 +54,18 @@
onNavigate: slug => {
const searchParams = new URLSearchParams(window.location.search)
const url = window.location.pathname.replace(new RegExp(`^${ Nova.config('base') }`), '')
Nova.visit(window.location.pathname, {
Nova.visit(url, {
data: {
...Object.fromEntries(searchParams.entries()),
'x-tab': slug,
}
},
})
}
},
}
}
},
}
</script>
Expand Down
Loading

0 comments on commit f03ec59

Please sign in to comment.