Skip to content

Commit

Permalink
Fix back step no save data bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ycs77 committed Jun 16, 2019
1 parent 037069d commit ff7b060
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 32 deletions.
36 changes: 19 additions & 17 deletions resources/views/base.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,26 @@

@include($step->view(), compact('step', 'errors'))

<div class="d-flex justify-content-between align-items-center">
@if ($stepRepo->hasPrev())
<a href="{{ action($formAction, ['step' => $stepRepo->prevSlug()]) }}?trigger=back" class="btn btn-primary">
@lang('wizard::generic.back')
</a>
@else
<span></span>
@endif
<div class="d-flex align-items-center">
<div class="mr-auto">
@if ($stepRepo->hasPrev())
<button type="button" class="btn btn-primary" onclick="this.form.action = '{{ action($postAction, [$step->slug(), '_trigger' => 'back']) }}'; this.form.submit();">
@lang('wizard::generic.back')
</button>
@endif
</div>

@if ($stepRepo->hasNext())
<button type="submit" class="btn btn-primary">
@lang('wizard::generic.next')
</button>
@else
<button type="submit" class="btn btn-primary">
@lang('wizard::generic.done')
</button>
@endif
<div class="ml-auto">
@if ($stepRepo->hasNext())
<button type="submit" class="btn btn-primary">
@lang('wizard::generic.next')
</button>
@else
<button type="submit" class="btn btn-primary">
@lang('wizard::generic.done')
</button>
@endif
</div>
</div>
</form>
</div>
Expand Down
24 changes: 13 additions & 11 deletions src/Http/Controllers/WizardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,6 @@ public function create(Request $request, $step = null)

// Check this step is not last processed step.
if ($step->index() !== $lastProcessedIndex) {

// If trigger from 'back',
// Set this step index and redirect to this step.
if ($request->query('trigger') === 'back') {
return $this->setThisStepAndRedirectTo($request, $step);
}

// Redirect to last processed step.
return $this->redirectToLastProcessedStep(
$request,
Expand Down Expand Up @@ -137,6 +130,13 @@ public function store(Request $request, string $step)
$step->saveData($request, $step->getRequestData($request), $step->model());
}

// If trigger from 'back',
// Set this step index and redirect to prev step.
if ($request->query('_trigger') === 'back') {
$prevStep = $this->wizard()->stepRepo()->prev();
return $this->setThisStepAndRedirectTo($request, $prevStep);
}

if (!$this->getNextStepSlug()) {
// Wizard done...
if ($this->wizard()->option('cache')) {
Expand Down Expand Up @@ -174,10 +174,12 @@ public function done(Request $request)
*/
protected function setThisStepAndRedirectTo(Request $request, Step $step)
{
$this->wizard()->cacheStepData(
$this->wizard()->cache()->get(),
$step->index()
);
if ($this->wizard()->option('cache')) {
$this->wizard()->cacheStepData(
$this->wizard()->cache()->get(),
$step->index()
);
}

return redirect()->route(
$request->route()->getName(),
Expand Down
2 changes: 1 addition & 1 deletion src/Step.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ abstract class Step
*
* @var array
*/
protected $inputExcept = ['_token', '_method'];
protected $inputExcept = ['_token', '_method', '_trigger'];

/**
* Create a new step instance.
Expand Down
23 changes: 20 additions & 3 deletions tests/Feature/HttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function testRunAllWizardSteps()
$this->assertDatabaseHas('users', [
'name' => 'John',
]);
$this->assertDatabaseHas('posts',[
$this->assertDatabaseHas('posts', [
'title' => 'Title',
'content' => 'Content.',
]);
Expand All @@ -108,16 +108,33 @@ public function testWizardStepNotEquialToLastProcessedStep()
$response->assertRedirect('/wizard/test/post-step-stub');
}

public function testWizardStepTriggerFromBack()
public function testWizardStepTriggerToBack()
{
$this->session([
'laravel_wizard.test' => [
'user-step-stub' => [
'name' => 'John',
],
'_last_index' => 1,
],
]);

$response = $this->get('/wizard/test/user-step-stub?trigger=back');
$response = $this->post('/wizard/test/post-step-stub?_trigger=back', [
'title' => 'Title',
'content' => 'Content.',
]);
$response->assertRedirect('/wizard/test/user-step-stub');

$this->assertEquals([
'user-step-stub' => [
'name' => 'John',
],
'post-step-stub' => [
'title' => 'Title',
'content' => 'Content.',
],
'_last_index' => 0,
], $this->app['session']->get('laravel_wizard.test'));
}

public function testWizardCacheDatabaseDriver()
Expand Down

0 comments on commit ff7b060

Please sign in to comment.