/ app / Http / Livewire / Documents / Templates / Edit.php
Edit.php
 1  <?php
 2  
 3  namespace App\Http\Livewire\Documents\Templates;
 4  
 5  use App\Models\Role;
 6  use App\Models\Template;
 7  use Exception;
 8  use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
 9  use Livewire\Component;
10  use WireUi\Traits\Actions;
11  
12  class Edit extends Component
13  {
14  
15      use AuthorizesRequests, Actions;
16  
17      public Template $template;
18      public $approverRole;
19      public $reviewerRole;
20  
21      public function mount(int $entityId)
22      {
23          $this->template = Template::findOrFail($entityId);
24          $this->approverRole = $this->template->approverRole?->id;
25          $this->reviewerRole = $this->template->reviewerRole?->id;
26  
27      }
28  
29      public function roles()
30      {
31          return Role::whereIn('id', config('app.usableRoles'))->get()->map(function (Role $role) {
32              $role->name = __('roles.' . $role->name);
33              return $role;
34          });
35      }
36  
37      public function save()
38      {
39          if (auth()->user()->can('can_write_templates')) {
40              try {
41  
42                  $this->template->reviewer_role_id = $this->reviewerRole;
43                  $this->template->approver_role_id = $this->approverRole;
44                  $this->template->save();
45  
46                  // UI Trick for delaying spinner
47                  sleep(1);
48  
49                  $this->emitUp('closePanel');
50  
51                  $this->notification()->success(
52                      __('documents.notifications.update.success.title'),
53                      __('documents.notifications.update.success.message')
54                  );
55  
56                  $this->emitTo(Index::class, 'refresh');
57              } catch (Exception $e) {
58                  error_log($e);
59  
60                  $this->notification()->error(
61                      'Error',
62                      $e->getMessage()
63                  );
64              }
65          }
66      }
67  
68      public function render()
69      {
70          return view('livewire.documents.templates.edit');
71      }
72  }