/ app / Http / Livewire / Documents / Version / Remove.php
Remove.php
 1  <?php
 2  
 3  namespace App\Http\Livewire\Documents\Version;
 4  
 5  use App\Http\Services\DocumentService;
 6  use App\Models\Revision;
 7  use Exception;
 8  use Illuminate\Auth\Access\AuthorizationException;
 9  use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
10  use Livewire\Component;
11  use Throwable;
12  use WireUi\Traits\Actions;
13  
14  class Remove extends Component
15  {
16      use AuthorizesRequests, Actions;
17  
18      public int $entityId = 0;
19  
20      public Revision $revision;
21  
22      public function mount()
23      {
24          $this->revision = Revision::withTrashed()->find($this->entityId);
25  
26          $this->authorize('delete', $this->revision);
27      }
28  
29      /**
30       * @throws AuthorizationException|Throwable
31       */
32      public function remove(): void
33      {
34          $this->authorize('delete', $this->revision);
35  
36          try {
37              if ($this->revision->trashed()) {
38                  DocumentService::hardDeleteRevision($this->entityId);
39              } else {
40                  DocumentService::removeRevision($this->entityId);
41              }
42  
43              // UI Trick for delaying spinner
44              custom_sleep();
45  
46              $this->emitUp('closePanel');
47              $this->emitTo(Index::class, 'refresh');
48  
49              $this->notification()->success(
50                  __('documents.notifications.remove.success.title'),
51                  __('documents.notifications.remove.success.message')
52              );
53          } catch (Exception $e) {
54              $this->notification()->error(
55                  'Error',
56                  $e->getMessage()
57              );
58          }
59      }
60  
61      public function render()
62      {
63          return view('livewire.documents.document.remove');
64      }
65  }