That's just strange. Have you checked that you call the right route ?
Call Controller Method on Button Click (After initial page render) to return Temp Signed URL
I have a share button that should call the share method in the controller after clicking it. This would generate a temporary signed url and then the frontend react code would get that string and save it to the clipboard.
// Controller
public function share(Room $room)
{
$this->authorize('share', $room);
return URL::temporarySignedRoute('room.show', now()->addHour(1), [
'room' => $room,
]);
}
// React Inertia Frontend
const handleShare = () => {
// Using router.post (from inertia) will not give me the return response of the signed url so I can use it
//router.postroute('room.share', room.data.id));
//navigator.clipboard.writeText(??));
};
<button className="btn btn-primary" onClick={handleShare}>
Share
</button>
// web.php routes file
Route::post('rooms/{room}/share',
[RoomController::class, 'shareRoom'])->name('room.share');
Using inertia's router does not give me to return response for the controller method, a signed temporary url. The success callbacks would return the entire page objects, when I don't want to refresh the page, as that'd refresh all the other data.
Nor do I want to pass the temporary signed url in the initial page load, as I want to generate it when the user clicks the share button.
Would I need to setup Axios and then do a standard post call to get the results of my controller method?
So I don't believe what I want really is possible per say. Although you can call routes, it doesn't seem like you can return arbitrary data like a standard POST call, since it's necessary to redirect back to some inertia route.
That is to say, a button that would trigger a controller action to return data to use in the React component is not possible without some work arounds.
- Return data in the initial render and then use that as the props link. This works, but if I wanted dynamic temporary signed routes for N items, and the user would only really interact with a few of them, it's not the greatest design.
- Setup a traditional Post request and use the data after the Post response. This is fine, just the inertia-react starter kit does not include an axios setup so need to deal with that.
- Partial Reloads for only the shareURL I can set the share url to be optional and then do a partial reload with only the shareUrl requested. This means all the other data isn't refreshed and the shareUrl is dynamic as initially desired. And the solution I ended up going with
// React Frontend
const handleShare = () => {
router.reload({
only: ['shareUrl'],
onSuccess: (page) => {
console.log('Share URL: ' + page.props.shareUrl);
if (!navigator.clipboard || !page.props.shareUrl) return;
console.log('Share URL After Check: ' + page.props.shareUrl);
navigator.clipboard.writeText(page.props.shareUrl as string);
},
});
};
Notice how I use onSuccess page props instead of directly using the shareUrl prop? Because, the shareUrl prop only is populated after I click the shareUrl button twice, a double reload, no idea why.
Please or to participate in this conversation.