Skip to content

Commit

Permalink
Improve redirect to step
Browse files Browse the repository at this point in the history
  • Loading branch information
ycs77 committed Sep 10, 2024
1 parent 9f02f7a commit c9bf9be
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 145 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,18 @@ $name = $secondStep->find('first')->data('name');
// 'Lucas'
```

### Redirect to the step

If you want to manually redirect to another step, you can use the `redirectToStep()` of the wizard, it will return a redirect response.

```php
// given a step slug
return $wizard->redirectToStep('second');

// given a step insatnce
return $wizard->redirectToStep($secondStep);
```

### Step repository

Step repository saves all steps data, if you want to use another step, you need to use it:
Expand Down
12 changes: 1 addition & 11 deletions src/Step.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,16 +390,6 @@ public function nextSlug()
*/
public function cacheProgress(Request $request, array $additionalData = [])
{
// Get cache data, and push this step data.
$cacheData = $this->wizard->cache()->get();
$cacheData[$this->slug] = $this->getRequestData($request);
$cacheData = array_merge($cacheData, $additionalData);

$nextStepIndex = $this->wizard->nextStepIndex();

// Save data to cache.
$this->wizard->cacheStepData($cacheData, $nextStepIndex);

return $this->wizard->cache()->get();
return $this->wizard->cacheProgress($request, $this, $additionalData);
}
}
71 changes: 66 additions & 5 deletions src/Wizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use RuntimeException;
use Ycs77\LaravelWizard\Cache\CacheManager;
Expand Down Expand Up @@ -117,6 +118,59 @@ public function nextStepIndex()
return $nextStepIndex;
}

/**
* Cache progress data.
*
* @param \Illuminate\Http\Request $request
* @param \Ycs77\LaravelWizard\Step $step
* @param array $additionalData
* @return array
*/
public function cacheProgress(Request $request, Step $step, array $additionalData = [])
{
if (! $this->option('cache')) {
return;
}

// Get cache data, and push this step data.
$cacheData = $this->cache->get();
$cacheData[$step->slug()] = $step->getRequestData($request);
$cacheData = array_merge($cacheData, $additionalData);

$nextStepIndex = $this->nextStepIndex();

// Save data to cache.
$this->cacheStepData($cacheData, $nextStepIndex);

return $this->cache->get();
}

/**
* Set the last processed step index.
*
* @return self
*/
public function setLastProcessedIndex($stepIndex)
{
if (! $this->option('cache')) {
return;
}

$this->cacheStepData($this->cache->get(), $stepIndex);

return $this;
}

/**
* Get the last processed step index.
*
* @return int|null
*/
public function getLastProcessedIndex()
{
return $this->cache->getLastProcessedIndex();
}

/**
* Get the action URL.
*
Expand All @@ -136,16 +190,23 @@ public function getActionUrl(string $method, $parameters = [])
/**
* Redirect to the given step.
*
* @param string|null $stepSlug
* @param string|\Ycs77\LaravelWizard\Step|null $step
* @param bool $setLastIndex
* @return \Illuminate\Http\RedirectResponse
*/
public function redirectToStep(string $stepSlug = null)
public function redirectToStep($step = null, $setLastIndex = true)
{
if (is_null($stepSlug)) {
$stepSlug = $this->nextSlug();
if (is_null($step)) {
$step = $this->stepRepo()->next();
} elseif (is_string($step)) {
$step = $this->stepRepo()->find($step);
}

if ($this->option('cache') && $setLastIndex) {
$this->setLastProcessedIndex($step->index());
}

return redirect($this->getActionUrl('create', [$stepSlug]));
return redirect($this->getActionUrl('create', [$step->slug()]));
}

/**
Expand Down
51 changes: 15 additions & 36 deletions src/Wizardable.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,13 @@ public function store(Request $request, string $step)

// If trigger from 'back', set this step index and redirect to prev step.
if ($request->query('_trigger') === 'back' && $this->beforeBackWizardStep($request, $step)) {
if ($this->wizard()->option('cache')) {
$step->cacheProgress($request);
}
$this->wizard()->cacheProgress($request, $step);

$prevStep = $this->wizard()->stepRepo()->prev();

return $this->setThisStepAndRedirectTo($request, $prevStep);
return $this->redirectToStep($prevStep);
} elseif ($step->skip() && $request->query('_trigger') === 'skip') {
if ($this->wizard()->option('cache')) {
$step->cacheProgress($request);
}
$this->wizard()->cacheProgress($request, $step);
} else {
// Form validation.
if ($this->canValidate($request)) {
Expand All @@ -128,7 +124,7 @@ public function store(Request $request, string $step)
}

if ($this->wizard()->option('cache')) {
$step->cacheProgress($request);
$this->wizard()->cacheProgress($request, $step);
} else {
$step->saveData($request, $step->getRequestData($request), $step->getModel());
}
Expand All @@ -153,7 +149,7 @@ public function store(Request $request, string $step)
return $this->redirectToDone($data);
}

return $this->redirectToStep();
return $this->redirectToStep(null, false);
}

/**
Expand Down Expand Up @@ -181,25 +177,6 @@ protected function renderDoneView(Request $request)
]);
}

/**
* Set this step and redirect to this step.
*
* @param \Illuminate\Http\Request $request
* @param \Ycs77\LaravelWizard\Step $step
* @return \Illuminate\Http\RedirectResponse
*/
protected function setThisStepAndRedirectTo(Request $request, Step $step)
{
if ($this->wizard()->option('cache')) {
$this->wizard()->cacheStepData(
$this->wizard()->cache()->get(),
$step->index()
);
}

return $this->redirectToStep($step->slug());
}

/**
* Redirect to last processed step.
*
Expand All @@ -211,7 +188,7 @@ protected function redirectToLastProcessedStep(Request $request, int $lastProces
{
$lastProcessedStep = $this->wizard()->stepRepo()->get($lastProcessedIndex);

return $this->redirectToStep($lastProcessedStep->slug());
return $this->redirectToStep($lastProcessedStep->slug(), false);
}

/**
Expand All @@ -228,25 +205,27 @@ protected function canValidate(Request $request)
/**
* Redirect to the step.
*
* @param string|null $stepSlug
* @param string|\Ycs77\LaravelWizard\Step|null $step
* @param bool $setLastIndex
* @return \Illuminate\Http\RedirectResponse
*/
protected function redirectToStep(string $stepSlug = null)
protected function redirectToStep($step = null, $setLastIndex = true)
{
return $this->wizard()->redirectToStep($stepSlug);
return $this->wizard()->redirectToStep($step, $setLastIndex);
}

/**
* Redirect to the step.
*
* Alias of `redirectToStep()`.
*
* @param string|null $stepSlug
* @param string|\Ycs77\LaravelWizard\Step|null $step
* @param bool $setLastIndex
* @return \Illuminate\Http\RedirectResponse
*/
protected function redirectTo(string $stepSlug = null)
protected function redirectTo($step = null, $setLastIndex = true)
{
return $this->redirectToStep($stepSlug);
return $this->redirectToStep($step, $setLastIndex);
}

/**
Expand Down Expand Up @@ -313,7 +292,7 @@ public function getActionUrl(string $method, $parameters = [])
protected function getLastProcessedStepIndex(string $stepSlug = null)
{
if ($this->wizard()->option('cache')) {
return $this->wizard()->cache()->getLastProcessedIndex() ?? 0;
return $this->wizard()->getLastProcessedIndex() ?? 0;
} elseif (is_string($stepSlug)) {
return $this->wizard()->stepRepo()->findKey($stepSlug, 0);
}
Expand Down
92 changes: 1 addition & 91 deletions tests/Unit/StepTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
namespace Ycs77\LaravelWizard\Test\Unit;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Request;
use Ycs77\LaravelWizard\Test\Stubs\PostStepStub;
use Ycs77\LaravelWizard\Contracts\CacheStore;
use Ycs77\LaravelWizard\Test\Stubs\StepStub;
use Ycs77\LaravelWizard\Test\Stubs\UserStepStub;
use Ycs77\LaravelWizard\Test\TestCase;
Expand Down Expand Up @@ -93,93 +92,4 @@ public function testGetDataKey()
$this->assertEquals('user-step-stub', $this->step->getDataKey());
$this->assertEquals('user-step-stub.field', $this->step->getDataKey('field'));
}

public function testCacheProgress()
{
// arrange
$expected = [
'user-step-stub' => [
'name' => 'Lucas Yang',
],
'_last_index' => 1,
];
$request = Request::create('http://example.com');

$this->step->shouldReceive('getRequestData')
->once()
->andReturn(['name' => 'Lucas Yang']);

/** @param \Mockery\MockInterface $mock */
$cache = $this->mock(CacheStore::class, function ($mock) use ($expected) {
$mock->shouldReceive('get')
->twice()
->andReturn([], $expected);
});
$this->wizard->shouldReceive('cache')->twice()->andReturn($cache);
$this->wizard->shouldReceive('nextStepIndex')->once()->andReturn(1);
$this->wizard->shouldReceive('cacheStepData')
->once()
->with([
'user-step-stub' => [
'name' => 'Lucas Yang',
],
], 1);

// act
$actual = $this->step->cacheProgress($request);

// assert
$this->assertEquals($expected, $actual);
}

public function testSecondStepCacheProgress()
{
// arrange
$expected = [
'user-step-stub' => [
'name' => 'Lucas Yang',
],
'post-step-stub' => [
'phone' => '12345678',
],
'_last_index' => 1,
];
$request = Request::create('http://example.com');

$this->step = $this->mock(PostStepStub::class, [$this->wizard, 1])->makePartial();
$this->step->shouldReceive('getRequestData')
->once()
->andReturn(['phone' => '12345678']);

/** @param \Mockery\MockInterface $mock */
$cache = $this->mock(CacheStore::class, function ($mock) use ($expected) {
$mock->shouldReceive('get')
->twice()
->andReturn([
'user-step-stub' => [
'name' => 'Lucas Yang',
],
'_last_index' => 1,
], $expected);
});
$this->wizard->shouldReceive('cache')->twice()->andReturn($cache);
$this->wizard->shouldReceive('nextStepIndex')->once()->andReturn(null);
$this->wizard->shouldReceive('cacheStepData')
->once()
->with([
'user-step-stub' => [
'name' => 'Lucas Yang',
],
'post-step-stub' => [
'phone' => '12345678',
],
'_last_index' => 1,
], null);

// act
$actual = $this->step->cacheProgress($request);

// assert
$this->assertEquals($expected, $actual);
}
}
Loading

0 comments on commit c9bf9be

Please sign in to comment.