Skip to content

Commit

Permalink
Improve feedback when action fails
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed Sep 24, 2024
1 parent faadc16 commit 8b0c3b8
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
16 changes: 15 additions & 1 deletion src/Actions/DeleteModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,21 @@ public function bypassesDirtyWarning(): bool

public function run($items, $values)
{
$items->each->delete();
$failures = $items->reject(fn ($model) => $model->delete());
$total = $items->count();

if ($failures->isNotEmpty()) {
$success = $total - $failures->count();
if ($total === 1) {
throw new \Exception(__('Item could not be deleted'));
} elseif ($success === 0) {
throw new \Exception(__('Items could not be deleted'));
} else {
throw new \Exception(__(':success/:total items were deleted', ['total' => $total, 'success' => $success]));
}
}

return trans_choice('Item deleted|Items deleted', $total);
}

public function redirect($items, $values)
Expand Down
16 changes: 15 additions & 1 deletion src/Actions/Publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ public function buttonText()

public function run($models, $values)
{
$models->each(fn ($model) => $model->published(true)->save());
$failures = $models->reject(fn ($model) => $model->published(true)->save());
$total = $models->count();

if ($failures->isNotEmpty()) {
$success = $total - $failures->count();
if ($total === 1) {
throw new \Exception(__('Model could not be published'));
} elseif ($success === 0) {
throw new \Exception(__('Model could not be published'));
} else {
throw new \Exception(__(':success/:total models were published', ['total' => $total, 'success' => $success]));
}
}

return trans_choice('Model published|Models published', $total);
}
}
16 changes: 15 additions & 1 deletion src/Actions/Unpublish.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ public function buttonText()

public function run($models, $values)
{
$models->each(fn ($model) => $model->published(false)->save());
$failures = $models->reject(fn ($model) => $model->published(false)->save());
$total = $models->count();

if ($failures->isNotEmpty()) {
$success = $total - $failures->count();
if ($total === 1) {
throw new \Exception(__('Model could not be unpublished'));
} elseif ($success === 0) {
throw new \Exception(__('Models could not be unpublished'));
} else {
throw new \Exception(__(':success/:total models were unpublished', ['total' => $total, 'success' => $success]));
}
}

return trans_choice('Model unpublished|Models unpublished', $total);
}
}
12 changes: 10 additions & 2 deletions src/Traits/HasRunwayResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,11 @@ public function publish($options = [])
}

if ($this->runwayResource()->hasPublishStates()) {
$this->published(true)->save();
$saved = $this->published(true)->save();

if (! $saved) {
return false;
}
}

return $this;
Expand All @@ -276,7 +280,11 @@ public function unpublish($options = [])
}

if ($this->runwayResource()->hasPublishStates()) {
$this->published(false)->save();
$saved = $this->published(false)->save();

if (! $saved) {
return false;
}
}

return $this;
Expand Down

0 comments on commit 8b0c3b8

Please sign in to comment.