Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove StepExecutorTrait::jsStepSubmit exception handling #2062

Merged
merged 2 commits into from
Jun 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 25 additions & 42 deletions src/UserAction/StepExecutorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
use Atk4\Core\Factory;
use Atk4\Data\Model;
use Atk4\Data\Model\UserAction;
use Atk4\Data\ValidationException;
use Atk4\Ui\Button;
use Atk4\Ui\Form;
use Atk4\Ui\Js\JsBlock;
use Atk4\Ui\Js\JsExpressionable;
use Atk4\Ui\Js\JsFunction;
use Atk4\Ui\Loader;
use Atk4\Ui\Message;
use Atk4\Ui\View;

trait StepExecutorTrait
Expand Down Expand Up @@ -177,14 +175,12 @@ protected function doFields(View $page): void
$this->jsSetSubmitButton($page, $form, $this->step);
$this->jsSetPreviousHandler($page, $this->step);

if (!$form->hookHasCallbacks(Form::HOOK_SUBMIT)) {
$form->onSubmit(function (Form $form) {
// collect fields defined in Model\UserAction
$this->setActionDataFromModel('fields', $form->model, $this->action->fields);
$form->onSubmit(function (Form $form) {
// collect fields defined in Model\UserAction
$this->setActionDataFromModel('fields', $form->model, $this->action->fields);

return $this->jsStepSubmit($this->step);
});
}
return $this->jsStepSubmit($this->step);
});
}

protected function doPreview(View $page): void
Expand Down Expand Up @@ -407,42 +403,29 @@ protected function jsSetSubmitButton(View $view, Form $form, string $step): void

/**
* Get proper JS after submitting a form in a step.
*
* @return JsBlock|View
*/
protected function jsStepSubmit(string $step)
protected function jsStepSubmit(string $step): JsBlock
{
try {
if (count($this->steps) === 1) {
// collect argument and execute action
$return = $this->action->execute(...$this->getActionArgs($this->getActionData('args')));
$js = $this->jsGetExecute($return, $this->action->getEntity()->getId());
} else {
// store data and setup reload
$js = new JsBlock([
$this->loader->jsAddStoreData($this->actionData, true),
$this->loader->jsLoad(
[
'step' => $this->isLastStep($step) ? 'final' : $this->getNextStep($step),
$this->name => $this->action->getEntity()->getId(),
],
['method' => 'POST'],
$this->loader->name
),
]);
}

return $js;
} catch (ValidationException $e) {
throw $e; // handled by Form::onSubmit() method
} catch (\Throwable $e) {
$msg = new Message(['Error executing ' . $this->action->caption, 'type' => 'error', 'class.red' => true]);
$msg->setApp($this->getApp());
$msg->invokeInit();
$msg->text->content = $this->getApp()->renderExceptionHtml($e);

return $msg;
if (count($this->steps) === 1) {
// collect argument and execute action
$return = $this->action->execute(...$this->getActionArgs($this->getActionData('args')));
$js = $this->jsGetExecute($return, $this->action->getEntity()->getId());
} else {
// store data and setup reload
$js = new JsBlock([
$this->loader->jsAddStoreData($this->actionData, true),
$this->loader->jsLoad(
[
'step' => $this->isLastStep($step) ? 'final' : $this->getNextStep($step),
$this->name => $this->action->getEntity()->getId(),
],
['method' => 'POST'],
$this->loader->name
),
]);
}

return $js;
}

protected function getActionData(string $step): array
Expand Down