Skip to content

Commit

Permalink
Adds custom redirects after creating or deleting a team
Browse files Browse the repository at this point in the history
  • Loading branch information
thisiskj committed Oct 23, 2020
1 parent eb35206 commit 6449631
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
15 changes: 11 additions & 4 deletions src/Http/Controllers/Inertia/TeamController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
use Laravel\Jetstream\Contracts\DeletesTeams;
use Laravel\Jetstream\Contracts\UpdatesTeamNames;
use Laravel\Jetstream\Jetstream;
use Laravel\Jetstream\RedirectsActions;

class TeamController extends Controller
{
use RedirectsActions;

/**
* Show the team management screen.
*
Expand Down Expand Up @@ -62,9 +65,11 @@ public function create(Request $request)
*/
public function store(Request $request)
{
app(CreatesTeams::class)->create($request->user(), $request->all());
$creator = app(CreatesTeams::class);

$creator->create($request->user(), $request->all());

return redirect(config('fortify.home'));
return redirect($this->redirectPath($creator));
}

/**
Expand Down Expand Up @@ -96,8 +101,10 @@ public function destroy(Request $request, $teamId)

app(ValidateTeamDeletion::class)->validate($request->user(), $team);

app(DeletesTeams::class)->delete($team);
$deleter = app(DeletesTeams::class);

$deleter->delete($team);

return redirect(config('fortify.home'));
return redirect($this->redirectPath($deleter));
}
}
5 changes: 4 additions & 1 deletion src/Http/Livewire/CreateTeamForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

use Illuminate\Support\Facades\Auth;
use Laravel\Jetstream\Contracts\CreatesTeams;
use Laravel\Jetstream\RedirectsActions;
use Livewire\Component;

class CreateTeamForm extends Component
{
use RedirectsActions;

/**
* The component's state.
*
Expand All @@ -27,7 +30,7 @@ public function createTeam(CreatesTeams $creator)

$creator->create(Auth::user(), $this->state);

return redirect(config('fortify.home'));
return redirect($this->redirectPath($creator));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Livewire/DeleteTeamForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function deleteTeam(ValidateTeamDeletion $validator, DeletesTeams $delete

$deleter->delete($this->team);

return redirect(config('fortify.home'));
return redirect($this->redirectPath($deleter));
}

/**
Expand Down
21 changes: 21 additions & 0 deletions src/RedirectsActions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Laravel\Jetstream;

trait RedirectsActions
{
/**
* Get the redirect path for the action.
*
* @param $action
* @return string
*/
public function redirectPath($action)
{
if (method_exists($action, 'redirectTo')) {
return $action->redirectTo();
}

return property_exists($action, 'redirectTo') ? $action->redirectTo : config('fortify.home');
}
}

0 comments on commit 6449631

Please sign in to comment.