Remove.php
1 <?php 2 3 namespace App\Http\Livewire\Documents\Document; 4 5 use App\Http\Services\DocumentService; 6 use App\Models\Document; 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 use App\Http\Livewire\Documents\Document\Index as DocumentsIndex; 14 use App\Http\Livewire\Documents\Pending\Index as PendingDocumentsIndex; 15 use App\Http\Livewire\Dossiers\Dossier\Document\Index as DossierDocumentsIndex; 16 17 class Remove extends Component 18 { 19 use AuthorizesRequests, Actions; 20 21 public int $entityId = 0; 22 23 public function mount() 24 { 25 $this->authorize('delete', Document::find($this->entityId)); 26 } 27 28 /** 29 * @throws AuthorizationException|Throwable 30 */ 31 public function remove(): void 32 { 33 $this->authorize('delete', Document::find($this->entityId)); 34 35 try { 36 DocumentService::remove($this->entityId); 37 38 // UI Trick for delaying spinner 39 if (config('app.env') !== 'testing') { 40 sleep(1); 41 } 42 43 $this->emitUp('closePanel'); 44 $this->emitTo(DocumentsIndex::class, 'refresh'); 45 $this->emitTo(PendingDocumentsIndex::class, 'refresh'); 46 $this->emitTo(DossierDocumentsIndex::class, 'refresh'); 47 48 $this->notification()->success( 49 __('documents.notifications.remove.success.title'), 50 __('documents.notifications.remove.success.message') 51 ); 52 } catch (Exception $e) { 53 $this->notification()->error( 54 'Error', 55 $e->getMessage() 56 ); 57 } 58 } 59 60 public function render() 61 { 62 return view('livewire.documents.document.remove'); 63 } 64 }