/ app / Http / Livewire / Documents / Version / Update.php
Update.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 Illuminate\Foundation\Auth\Access\AuthorizesRequests;
 8  use Livewire\Component;
 9  use WireUi\Traits\Actions;
10  
11  class Update extends Component
12  {
13      use AuthorizesRequests, Actions;
14  
15      public int $entityId = 0;
16      public ?Revision $revision = null;
17      public $name = null;
18      public $description = null;
19  
20      public function mount()
21      {
22          $this->revision = Revision::find($this->entityId);
23          $this->authorize('update', $this->revision->document);
24          $this->name = $this->revision->name;
25          $this->description = $this->revision->description;
26      }
27  
28      public function update()
29      {
30          $this->authorize('update', $this->revision->document);
31          $this->validate([
32              'name' => 'required|string|max:255',
33              'description' => 'nullable|string|max:255',
34          ]);
35  
36          try {
37              DocumentService::updateRevision($this->entityId, $this->name, $this->description);
38  
39              // UI Trick for delaying spinner
40              if (config('app.env') !== 'testing') {
41                  sleep(1);
42              }
43  
44              $this->emitUp('closePanel');
45              $this->emit('refresh');
46  
47              $this->notification()->success(
48                  __('documents.notifications.update.success.title'),
49                  __('documents.notifications.update.success.message')
50              );
51          } catch (\Exception $e) {
52              $this->notification()->error(
53                  'Error',
54                  $e->getMessage()
55              );
56          }
57      }
58  
59      public function render()
60      {
61          return view('livewire.documents.version.update');
62      }
63  }