FabioFratini 9 місяців тому
батько
коміт
3dc747ee16
2 змінених файлів з 34 додано та 9 видалено
  1. 26 5
      app/Http/Livewire/User.php
  2. 8 4
      resources/views/livewire/user.blade.php

+ 26 - 5
app/Http/Livewire/User.php

@@ -8,7 +8,7 @@ use Livewire\Component;
 class User extends Component
 {
     public $records, $name,$cognome, $email, $password, $oldPassword, $level, $enabled, $dataId, $update = false, $add = false;
-
+    public $userExists = false;
     protected $rules = [
         'name' => 'required',
         'cognome' => 'required',
@@ -45,6 +45,8 @@ class User extends Component
         $this->resetFields();
         $this->add = true;
         $this->update = false;
+        $this->enabled = true;
+        $this->userExists = false;
     }
 
     public function store()
@@ -56,7 +58,23 @@ class User extends Component
             'level' => $this->level,
             'enabled' => $this->enabled
         ]);
-        $this->validate();
+        $rules = [
+            'name' => 'required',
+            'cognome' => 'required',
+            'email' => 'required|email|unique:users,email',
+            'password' => 'required|min:6'
+        ];
+
+        $messages = [
+            'name.required' => 'Il nome è obbligatorio',
+            'cognome.required' => 'Il cognome è obbligatorio',
+            'email.required' => 'La mail è obbligatoria',
+            'email.email' => 'La mail deve essere un indirizzo valido',
+            'email.unique' => 'Questa mail è già stata utilizzata',
+            'password.required' => 'La password è obbligatoria',
+            'password.min' => 'La password deve essere di almeno 6 caratteri'
+        ];
+        $this->validate($rules, $messages);
         Log::info('User store', [
             'name' => $this->name,
             'cognome' => $this->cognome,
@@ -80,7 +98,7 @@ class User extends Component
                 'level' => $this->level,
                 'enabled' => $this->enabled
             ]);
-            session()->flash('success','Dato creato');
+            session()->flash('success','Utente creato');
             $this->resetFields();
             $this->add = false;
         } catch (\Exception $ex) {
@@ -97,11 +115,12 @@ class User extends Component
                 $this->name = $user->name;
                 $this->cognome = $user->cognome;
                 $this->email = $user->email;
-                $this->password = $user->password;
                 $this->level = $user->level;
                 $this->dataId = $user->id;
                 $this->update = true;
                 $this->add = false;
+                $this->enabled = $user->enabled;
+                $this->userExists = true;
             }
             Log::info('User edit', [
                 'name' => $this->name,
@@ -144,9 +163,11 @@ class User extends Component
 
     public function cancel()
     {
+        $this->resetFields();
         $this->add = false;
         $this->update = false;
-        $this->resetFields();
+        $this->userExists = false;
+        $this->enabled = false;
     }
 
     public function delete($id)

+ 8 - 4
resources/views/livewire/user.blade.php

@@ -21,6 +21,7 @@
             <table class="table tablesaw tableHead tablesaw-stack" id="tablesaw-350" width="100%">
                 <thead>
                     <tr>
+                        <th scope="col">Cognome</th>
                         <th scope="col">Nome</th>
                         <th scope="col">Email</th>
                         <th scope="col">Livello</th>
@@ -31,6 +32,7 @@
                 <tbody id="checkall-target">
                     @foreach($records as $record)
                         <tr>
+                            <td>{{$record->cognome}}</td>
                             <td>{{$record->name}}</td>
                             <td>{{$record->email}}</td>
                             <td>{{$record->level == 0 ? 'Admin' : ($record->level == 1 ? 'Worker' : 'Istruttore')}}</td>
@@ -97,8 +99,10 @@
                             <div class="col-md-6 ">
                                 <div class="form--item">
                                     <label for="password" class="form-label">Password</label>
-                                    <input class="form-control js-keyupTitle @error('password') is-invalid @enderror" type="text" id="password" placeholder="Password" wire:model="password" type="password">
-                                    <small>Lasciare vuota per NON modificarla</small>
+                                    <input class="form-control js-keyupTitle @error('password') is-invalid @enderror" type="password" id="password" placeholder="Password" wire:model="password" type="password" {{ $userExists ? 'disabled' : '' }}>
+                                    @if($update)
+                                        <small>Lasciare vuota per NON modificarla</small>
+                                    @endif
                                     @error('password')
                                         <div class="invalid-feedback">{{ $message }}</div>
                                     @enderror
@@ -122,7 +126,7 @@
                             <div class="col-md-6">
                                 <div class="form--item">
                                     <label for="enabled" class="form-label">Abilitato</label>
-                                    <input class="form-check-input form-control" style="width:22px; height:22px;" type="checkbox" id="enabled" wire:model="enabled">
+                                    <input class="form-check-input form-control" style="width:22px; height:22px;" type="checkbox" id="enabled" wire:model="enabled" {{ $userExists ? '' : '' }}>
                                 </div>
                             </div>
                         </div>
@@ -132,7 +136,7 @@
                         <div class="form--item">
                             <button type="button" class="btn--ui lightGrey" wire:click="cancel()">Annulla</button>
                             @if($add)
-                                <button type="submit" class="btn--ui" wire:click.prevent="store()">Salva</button>
+                                <button type="submit" class="btn--ui" wire:click.prevent="store()">Invita</button>
                             @endif
                             @if($update)
                                 <button type="submit" class="btn--ui" wire:click.prevent="update()">Salva</button>