Skip to content

Commit

Permalink
Extracts the query string from fallback url and sends to native route…
Browse files Browse the repository at this point in the history
…s too
  • Loading branch information
tonysm committed Oct 30, 2023
1 parent 5a0d04c commit 90735b1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected function refreshOrRedirectBack(?string $fallbackUrl, array $options =
protected function redirectToTurboNativeAction(string $action, string $fallbackUrl, string $redirectType = 'to', array $options = [])
{
if (request()->wasFromTurboNative()) {
return new TurboNativeRedirectResponse(route("turbo_{$action}_historical_location"));
return TurboNativeRedirectResponse::createFromFallbackUrl($action, $fallbackUrl);
}

if ($redirectType === 'back') {
Expand Down
15 changes: 15 additions & 0 deletions src/Http/TurboNativeRedirectResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

class TurboNativeRedirectResponse extends RedirectResponse
{
public static function createFromFallbackUrl(string $action, string $fallbackUrl)
{
return (new self(route("turbo_{$action}_historical_location")))
->withQueryString((new self($fallbackUrl))->getQueryString());
}

public function with($key, $value = null)
{
$params = $this->getQueryString();
Expand All @@ -15,6 +21,15 @@ public function with($key, $value = null)
->setTargetUrl($this->getTargetUrl().'?'.http_build_query($params + [$key => urlencode($value)]));
}

protected function withQueryString(array $params): self
{
foreach ($params as $key => $val) {
$this->with($key, $val);
}

return $this;
}

protected function getQueryString(): array
{
parse_str(str_contains($this->getTargetUrl(), '?') ? Str::after($this->getTargetUrl(), '?') : '', $query);
Expand Down
23 changes: 17 additions & 6 deletions tests/Http/TurboNativeNavigationControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,38 @@ public function recede_resume_or_refresh_when_native_or_redirect_when_not_withou
*/
public function recede_resume_or_refresh_when_native_or_redirect_when_not_with_flash(string $action)
{
// Non-Turbo Native redirect with flash, but without fragments...
// Non-Turbo Native redirect with only flash...
$this->post(route('trays.store'), ['return_to' => "{$action}_or_redirect", 'with' => true])
->assertRedirect(route('trays.show', 1))
->assertRedirect(route('trays.show', ['tray' => 1]))
->assertSessionHas('status', __('Tray created.'));

// Non-Turbo Native redirect With flash & fragments...
// Non-Turbo Native redirect with only flash & fragments...
$this->post(route('trays.store'), ['return_to' => "{$action}_or_redirect", 'with' => true, 'fragment' => true])
->assertRedirect(route('trays.show', 1).'#newly-created-tray')
->assertRedirect(route('trays.show', ['tray' => 1]).'#newly-created-tray')
->assertSessionHas('status', __('Tray created.'));

// Turbo Native redirect with flash, but without fragments...
// Non-Turbo Native redirect with only flash & fragments & queries...
$this->post(route('trays.store'), ['return_to' => "{$action}_or_redirect", 'with' => true, 'fragment' => true, 'query' => true])
->assertRedirect(route('trays.show', ['tray' => 1, 'lorem' => 'ipsum']).'#newly-created-tray')
->assertSessionHas('status', __('Tray created.'));

// Turbo Native redirect with only flash...
$this->turboNative()
->post(route('trays.store'), ['return_to' => "{$action}_or_redirect", 'with' => true])
->assertRedirect(route("turbo_{$action}_historical_location", ['status' => urlencode(__('Tray created.'))]))
->assertSessionMissing('status');

// Turbo Native redirect with flash & fragments...
// Turbo Native redirect with only flash & fragments...
$this->turboNative()
->post(route('trays.store'), ['return_to' => "{$action}_or_redirect", 'with' => true, 'fragment' => true])
->assertRedirect(route("turbo_{$action}_historical_location", ['status' => urlencode(__('Tray created.'))]).'#newly-created-tray')
->assertSessionMissing('status');

// Turbo Native redirect with only flash & fragments & query...
$this->turboNative()
->post(route('trays.store'), ['return_to' => "{$action}_or_redirect", 'with' => true, 'fragment' => true, 'query' => true])
->assertRedirect(route("turbo_{$action}_historical_location", ['lorem' => 'ipsum', 'status' => urlencode(__('Tray created.'))]).'#newly-created-tray')
->assertSessionMissing('status');
}

/**
Expand Down
16 changes: 10 additions & 6 deletions workbench/app/Http/Controllers/TraysController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ public function show($tray)

public function store(Request $request)
{
$query = $request->boolean('query')
? ['lorem' => 'ipsum']
: [];

$response = match ($request->input('return_to')) {
'recede_or_redirect' => $this->recedeOrRedirectTo(route('trays.show', ['tray' => 1])),
'resume_or_redirect' => $this->resumeOrRedirectTo(route('trays.show', ['tray' => 1])),
'refresh_or_redirect' => $this->refreshOrRedirectTo(route('trays.show', ['tray' => 1])),
'recede_or_redirect_back' => $this->recedeOrRedirectBack(route('trays.show', ['tray' => 5])),
'resume_or_redirect_back' => $this->resumeOrRedirectBack(route('trays.show', ['tray' => 5])),
'refresh_or_redirect_back' => $this->refreshOrRedirectBack(route('trays.show', ['tray' => 5])),
'recede_or_redirect' => $this->recedeOrRedirectTo(route('trays.show', ['tray' => 1] + $query)),
'resume_or_redirect' => $this->resumeOrRedirectTo(route('trays.show', ['tray' => 1] + $query)),
'refresh_or_redirect' => $this->refreshOrRedirectTo(route('trays.show', ['tray' => 1] + $query)),
'recede_or_redirect_back' => $this->recedeOrRedirectBack(route('trays.show', ['tray' => 5] + $query)),
'resume_or_redirect_back' => $this->resumeOrRedirectBack(route('trays.show', ['tray' => 5] + $query)),
'refresh_or_redirect_back' => $this->refreshOrRedirectBack(route('trays.show', ['tray' => 5] + $query)),
default => throw new Exception('Missing return_to param to redirect the response.'),
};

Expand Down

0 comments on commit 90735b1

Please sign in to comment.