Update.php
1 <?php 2 3 namespace App\Http\Livewire\Entities; 4 5 use App\Enums\EntityTypeEnum; 6 use App\Http\Services\EntityService; 7 use App\Models\EntityType; 8 use Exception; 9 use Illuminate\Foundation\Auth\Access\AuthorizesRequests; 10 use Illuminate\Validation\Rule; 11 use Livewire\Component; 12 use Livewire\WithFileUploads; 13 use WireUi\Traits\Actions; 14 use Illuminate\Support\Facades\Storage; 15 16 class Update extends Component 17 { 18 use AuthorizesRequests, Actions, WithFileUploads; 19 20 public $entityId; 21 22 public array $entity = [ 23 'entity_type_id' => null, 24 'name' => '', 25 'legal_name' => '', 26 'code' => '', 27 'nif' => '', 28 'notification_number' => '', 29 'authorization_number' => '', 30 ]; 31 32 public array $contactInfo = [ 33 'address' => '', 34 'city' => '', 35 'postal_code' => '', 36 'phone' => '', 37 'email' => '', 38 'country_id' => '', 39 ]; 40 41 public $logo; 42 43 public function rules() 44 { 45 return [ 46 'entity.entity_type_id' => [ 47 'required', 48 Rule::unique('entities', 'entity_type_id') 49 ->where('entity_type_id', EntityTypeEnum::cab()) 50 ->ignore($this->entityId) 51 ], 52 'contactInfo.country_id' => 'required', 53 'entity.name' => 'required|min:2', 54 'entity.code' => 'nullable', 55 'entity.nif' => 'nullable', 56 'entity.notification_number' => 'nullable|string|max:255', 57 'entity.authorization_number' => 'nullable|string|max:255', 58 'contactInfo.city' => 'nullable', 59 'contactInfo.postal_code' => 'nullable', 60 'contactInfo.address' => 'nullable', 61 'contactInfo.phone' => 'nullable|string|max:255', 62 'contactInfo.email' => 'nullable|email', 63 'logo' => 'nullable|image|mimes:jpeg,png,jpg,gif,webp|max:20480|dimensions:max_width=500,max_height=500', 64 ]; 65 } 66 67 public function updated($propertyName) 68 { 69 $this->validateOnly($propertyName); 70 } 71 72 public function fetchData() 73 { 74 $entity = EntityService::find($this->entityId); 75 76 $this->entity = [ 77 'entity_type_id' => $entity['entity_type_id'], 78 'name' => $entity['name'], 79 'legal_name' => $entity['legal_name'], 80 'code' => $entity['code'], 81 'nif' => $entity['nif'], 82 ]; 83 84 $this->contactInfo = [ 85 'country_id' => $entity->contactInfo->country_id ?? '', 86 'city' => $entity->contactInfo->city ?? '', 87 'postal_code' => $entity->contactInfo->postal_code ?? '', 88 'address' => $entity->contactInfo->address ?? '', 89 'phone' => $entity->contactInfo->phone ?? '', 90 'email' => $entity->contactInfo->email ?? '' 91 ]; 92 } 93 94 public function mount() 95 { 96 $this->authorize('can_write_external_entities'); 97 $this->fetchData(); 98 } 99 100 public function getTypesProperty() 101 { 102 return EntityType::get() 103 ->map(function ($entityType) { 104 return [ 105 'id' => $entityType->id, 106 'name' => __('entities.type.' . $entityType->name), 107 ]; 108 }); 109 } 110 111 public function update() 112 { 113 $this->authorize('can_write_external_entities'); 114 $this->validate(); 115 116 try { 117 118 EntityService::update($this->entity, $this->contactInfo, $this->entityId, $this->logo); 119 120 // UI Trick for delaying spinner 121 if (config('app.env') !== 'testing') { 122 sleep(1); 123 } 124 125 $this->emitUp('closePanel'); 126 $this->emitTo('entities.index', 'refresh'); 127 $this->emitTo('entities.view', 'refresh'); 128 129 $this->notification()->success( 130 __('entities.notifications.update.success.title'), 131 __('entities.notifications.update.success.message') 132 ); 133 } catch (Exception $e) { 134 $this->notification()->error( 135 'Error', 136 $e->getMessage() 137 ); 138 } 139 } 140 141 public function render() 142 { 143 return view('livewire.entities.update'); 144 } 145 }