Skip to content

Commit

Permalink
[5.x] Improve feedback when action fails (#10264)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
simonerd and jasonvarga authored Sep 23, 2024
1 parent 1a1aa56 commit 6db65ff
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 11 deletions.
4 changes: 3 additions & 1 deletion resources/js/components/data-list/HasActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export default {
this.$events.$emit('clear-selections');
this.$events.$emit('reset-action-modals');

if (response.message !== false) {
if (response.success === false) {
this.$toast.error(response.message || __("Action failed"));
} else {
this.$toast.success(response.message || __("Action completed"));
}

Expand Down
1 change: 1 addition & 0 deletions resources/lang/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"A valid blueprint is required.": "Ein gültiger Blueprint ist erforderlich.",
"Above": "Oberhalb",
"Action completed": "Aktion abgeschlossen",
"Action failed": "Aktion fehlgeschlagen",
"Activate Account": "Account aktivieren",
"Activation URL": "Aktivierungs-URL",
"Active": "Aktiv",
Expand Down
1 change: 1 addition & 0 deletions resources/lang/de_CH.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"A valid blueprint is required.": "Ein gültiger Blueprint ist erforderlich.",
"Above": "Oberhalb",
"Action completed": "Aktion abgeschlossen",
"Action failed": "Aktion fehlgeschlagen",
"Activate Account": "Account aktivieren",
"Activation URL": "Aktivierungs-URL",
"Active": "Aktiv",
Expand Down
1 change: 1 addition & 0 deletions resources/lang/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"A valid blueprint is required.": "Un Blueprint valide est exigé.",
"Above": "Au dessus",
"Action completed": "Action terminée",
"Action failed": "Action échouée",
"Activate Account": "Activer le compte",
"Activation URL": "URL d'activation",
"Active": "actif",
Expand Down
1 change: 1 addition & 0 deletions resources/lang/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"A valid blueprint is required.": "Een geldige blueprint is vereist.",
"Above": "Boven",
"Action completed": "Actie voltooid",
"Action failed": "Actie gefaald",
"Activate Account": "Activeer Account",
"Activation URL": "Activatie-url",
"Active": "Actief",
Expand Down
16 changes: 15 additions & 1 deletion src/Actions/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,21 @@ public function bypassesDirtyWarning(): bool

public function run($items, $values)
{
$items->each->delete();
$failures = $items->reject(fn ($entry) => $entry->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/DeleteMultisiteEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,21 @@ public function run($items, $values)
$items->each->deleteDescendants();
}

$items->each->delete();
$failures = $items->reject(fn ($entry) => $entry->delete());
$total = $items->count();

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

return trans_choice('Entry deleted|Entries deleted', $total);
}

private function canChangeBehavior(): bool
Expand Down
18 changes: 15 additions & 3 deletions src/Actions/Publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,20 @@ public function buttonText()

public function run($entries, $values)
{
$entries->each(function ($entry) {
$entry->publish(['user' => User::current()]);
});
$failures = $entries->reject(fn ($entry) => $entry->publish(['user' => User::current()]));
$total = $entries->count();

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

return trans_choice('Entry published|Entries published', $total);
}
}
18 changes: 15 additions & 3 deletions src/Actions/Unpublish.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,20 @@ public function buttonText()

public function run($entries, $values)
{
$entries->each(function ($entry) {
$entry->unpublish(['user' => User::current()]);
});
$failures = $entries->reject(fn ($entry) => $entry->unpublish(['user' => User::current()]));
$total = $entries->count();

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

return trans_choice('Entry unpublished|Entries unpublished', $total);
}
}
6 changes: 5 additions & 1 deletion src/Data/Publishable.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ public function publish($options = [])
return $this->publishWorkingCopy($options);
}

$this->published(true)->save();
$saved = $this->published(true)->save();

if (! $saved) {
return false;
}

return $this;
}
Expand Down
10 changes: 9 additions & 1 deletion src/Http/Controllers/CP/ActionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Statamic\Http\Controllers\CP;

use Exception;
use Illuminate\Http\Request;
use Statamic\Facades\Action;
use Statamic\Facades\User;
Expand Down Expand Up @@ -35,8 +36,14 @@ public function run(Request $request)
abort_unless($unauthorized->isEmpty(), 403, __('You are not authorized to run this action.'));

$values = $action->fields()->addValues($request->all())->process()->values()->all();
$successful = true;

$response = $action->run($items, $values);
try {
$response = $action->run($items, $values);
} catch (Exception $e) {
$response = empty($msg = $e->getMessage()) ? __('Action failed') : $msg;
$successful = false;
}

if ($redirect = $action->redirect($items, $values)) {
return [
Expand All @@ -52,6 +59,7 @@ public function run(Request $request)
}

$response = $response ?: [];
$response['success'] = $successful;

if (Arr::get($context, 'view') === 'form') {
$response['data'] = $this->getItemData($items->first(), $context);
Expand Down

0 comments on commit 6db65ff

Please sign in to comment.