Skip to content

Commit

Permalink
Fix CardDeck action /w entity (#1845)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Voříšek <mvorisek@mvorisek.cz>
  • Loading branch information
mkrecek234 and mvorisek authored Aug 31, 2022
1 parent a8a08cf commit 2f7182a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
32 changes: 26 additions & 6 deletions demos/init-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,40 @@ public function atomic(\Closure $fx)
return $res;
}

protected function wrapUserActionCallbackPreventModification(Model\UserAction $action, \Closure $outputCallback): void
{
$originalCallback = $action->callback;
$action->callback = function (Model $model, ...$args) use ($action, $originalCallback, $outputCallback) {
if ($model->isEntity()) {
$action = $action->getActionForEntity($model);
}

$callbackBackup = $action->callback;
try {
$action->callback = $originalCallback;
$action->execute(...$args);
} finally {
$action->callback = $callbackBackup;
}

return $outputCallback($model, ...$args);
};
}

protected function initPreventModification(): void
{
$this->getUserAction('add')->callback = function (Model $model) {
$this->wrapUserActionCallbackPreventModification($this->getUserAction('add'), function (Model $model) {
return 'Form Submit! Data are not save in demo mode.';
};
});

$this->getUserAction('edit')->callback = function (Model $model) {
$this->wrapUserActionCallbackPreventModification($this->getUserAction('edit'), function (Model $model) {
return 'Form Submit! Data are not save in demo mode.';
};
});

$this->getUserAction('delete')->confirmation = 'Please go ahead. Demo mode does not really delete data.';
$this->getUserAction('delete')->callback = function (Model $model) {
$this->wrapUserActionCallbackPreventModification($this->getUserAction('delete'), function (Model $model) {
return 'Only simulating delete when in demo mode.';
};
});
}
}

Expand Down
27 changes: 14 additions & 13 deletions src/CardDeck.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,26 +223,30 @@ protected function initActionExecutor(Model\UserAction $action): ExecutorInterfa
*
* @return array|object
*/
protected function jsExecute($return, Model\UserAction $action = null)
protected function jsExecute($return, Model\UserAction $action)
{
if (is_string($return)) {
return $this->getNotifier($return, $action);
return $this->getNotifier($action, $return);
} elseif (is_array($return) || $return instanceof JsExpressionable) {
return $return;
} elseif ($return instanceof Model) {
if ($return->isEntity()) {
$action = $action->getActionForEntity($return);
}

$msg = $return->isLoaded() ? $this->saveMsg : ($action->appliesTo === Model\UserAction::APPLIES_TO_SINGLE_RECORD ? $this->deleteMsg : $this->defaultMsg);

return $this->jsModelReturn($action, $msg);
}

return $this->getNotifier($this->defaultMsg, $action);
return $this->getNotifier($action, $this->defaultMsg);
}

/**
* Return jsNotifier object.
* Override this method for setting notifier based on action or model value.
*/
protected function getNotifier(string $msg = null, Model\UserAction $action = null): object
protected function getNotifier(Model\UserAction $action, string $msg = null): object
{
$notifier = Factory::factory($this->notifyDefault);
if ($msg) {
Expand All @@ -255,11 +259,11 @@ protected function getNotifier(string $msg = null, Model\UserAction $action = nu
/**
* Js expression return when action afterHook executor return a Model.
*/
protected function jsModelReturn(Model\UserAction $action = null, string $msg = 'Done!'): array
protected function jsModelReturn(Model\UserAction $action, string $msg = 'Done!'): array
{
$js = [];
$js[] = $this->getNotifier($msg, $action);
if ($action->getModel()->isLoaded() && $card = $this->findCard($action->getModel())) {
$js[] = $this->getNotifier($action, $msg);
if ($action->getEntity()->isLoaded() && $card = $this->findCard($action->getEntity())) {
$js[] = $card->jsReload($this->getReloadArgs());
} else {
$js[] = $this->container->jsReload($this->getReloadArgs());
Expand All @@ -281,21 +285,18 @@ protected function jsModelReturn(Model\UserAction $action = null, string $msg =
*
* @return mixed
*/
protected function findCard(Model $model)
protected function findCard(Model $entity)
{
$mapResults = function ($a) use ($model) {
return $a[$model->idField];
};
$deck = [];
foreach ($this->cardHolder->elements as $element) {
if ($element instanceof $this->card) {
$deck[$element->model->getId()] = $element;
}
}

if (in_array($model->getId(), array_map($mapResults, $model->export([$model->idField])), true)) {
if ($entity->getModel()->tryLoad($entity->getId()) !== null) {
// might be in result set but not in deck, for example when adding a card.
return $deck[$model->getId()] ?? null;
return $deck[$entity->getId()] ?? null;
}

return null;
Expand Down

0 comments on commit 2f7182a

Please sign in to comment.