F
Filamentβ€’17mo ago
fblaser

Table action to open url in new tab (asynchronous)

Hi, In my app, I have a ressource that represents servers, with a table action that allows the user to login to this server's control panel through a specially crafted url. I want the url to be opened in a new browser/tab/window. The url is generated through an API that is quite slow. Generating the url for each server in a table is therefore unpractical. This is therefore not a solution:
Action::make('login')->url(function ($record) { return $record->loginUrl()})->openUrlInNewTab();
Action::make('login')->url(function ($record) { return $record->loginUrl()})->openUrlInNewTab();
In filament v2, I had resorted to a dirty trick: - Create a custom action in the Resources/ServerResource/ListServer.php. This action generates the url and emits/dispatch a livewire event "openinnewtab" with the url as parameter - Add a global livewire listener through a small js file included on all pages:
Livewire.on('openInNewTab', url => {
window.open(url, '_blank');
})
Livewire.on('openInNewTab', url => {
window.open(url, '_blank');
})
This worked well, but didn't look good. After upgrading to v3, I had to change the way the file is loaded (now using the FilamentAsset facade), and now the listener throws the error in the browser: [Error] ReferenceError: Can't find variable: Livewire This is probably due to a change in which the files are loaded, but it triggers me to find a better way of doing the whole thing. Has anyone any idea on how to have Filament call $record->loginUrl() asynchronously? Thanks heaps,
Solution:
Feels, hacky to me too, but sometimes that's just what we have to do. πŸ™‚
Jump to solution
3 Replies
awcodes
awcodesβ€’17mo ago
Will the login url ever really change? If not, I would generate on create and just persist the url as a field in the db then it’s just always available on the record as a standard attribute.
fblaser
fblaserOPβ€’17mo ago
Thanks for the reply @awcodes. The login url changes on every call, unfortunately. The API creates a session on the control panel, and the link allows for a passwordless sign-in, so it better be somewhat random πŸ˜‰ In the meantime, I've managed to get my old solution working, by making sure livewire is initialised before listening for the event:
document.addEventListener('livewire:initialized', () => {
Livewire.on('openInNewTab', url => {
window.open(url, '_blank');
})
})
document.addEventListener('livewire:initialized', () => {
Livewire.on('openInNewTab', url => {
window.open(url, '_blank');
})
})
This works, but feels hacky...
Solution
awcodes
awcodesβ€’17mo ago
Feels, hacky to me too, but sometimes that's just what we have to do. πŸ™‚
Want results from more Discord servers?
Add your server