Connecting to external API and redirecting after success

Not fully understanding how much of the business logic should be in the Resource classes, or if those are more strictly used for forms and tables? Related to the above, I have a "Location" model and an action button on my edit location page (defined in EditLocation resource class) that redirects to an external URL that connects to an API integration. Once the API connection is successful, it redirects back to my application and passes along some data to validate. In the old (non-filament) version of my app, I would then just return redirect to the locations.edit view and they're back where they started, and there's a toastr message that pops up indicating the action was successful. Here's what I have in my EditLocation Resource class:
class EditLocation extends EditRecord
{
protected static string $resource = LocationResource::class;

protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
Action::make('connectToQuickbooks')
->button()
->visible(fn () => $this->record->quickbooksToken()->exists() === false)
->action(function () {
$quickbooks = new QuickbooksService($this->record);
$qbAuthUrl = $quickbooks->getAuthCodeUrl();
return redirect($qbAuthUrl);
}),
];
}

public function connect(Request $request, ConnectToQuickbooks $action)
{
if (!$action->handle($request->only(['state', 'code', 'realmId']))) {
//return with an error toast in filament - not sure how to do this.
}
//the below is my old code, but need to do the equivalent in filament.
//return redirect()->route('locations.edit', ['location' => $request['state']]); //->with('notification', Helper::notification('Success', 'Quickbooks connected.'));
}
class EditLocation extends EditRecord
{
protected static string $resource = LocationResource::class;

protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
Action::make('connectToQuickbooks')
->button()
->visible(fn () => $this->record->quickbooksToken()->exists() === false)
->action(function () {
$quickbooks = new QuickbooksService($this->record);
$qbAuthUrl = $quickbooks->getAuthCodeUrl();
return redirect($qbAuthUrl);
}),
];
}

public function connect(Request $request, ConnectToQuickbooks $action)
{
if (!$action->handle($request->only(['state', 'code', 'realmId']))) {
//return with an error toast in filament - not sure how to do this.
}
//the below is my old code, but need to do the equivalent in filament.
//return redirect()->route('locations.edit', ['location' => $request['state']]); //->with('notification', Helper::notification('Success', 'Quickbooks connected.'));
}
3 Replies
Jon Mason
Jon MasonOP15mo ago
Am I putting this logic in the right place? It works up until the point where I need to redirect. I don't know how to accomplish that. I tried adding this to my web routes: Route::get('locations/{location}/edit', EditLocation::class)->name('locations.edit'); I at least figured out part of the problem by running sail artisan route:list and finding out that the name of the route was different than what I was using, so I got it working. Still not sure if the connect() method I showed should be in the resource class or what's actually supposed to live there. I don't guess it matters, but just trying to understand best practices.
DrByte
DrByte15mo ago
As far as where to put your connect() function, it's fine if it's in that file since that's what it's related to. Or you could put it anyplace else that you have services/actions/etc that you want to centralize.
Jon Mason
Jon MasonOP15mo ago
ok, thanks for your help!
Want results from more Discord servers?
Add your server