diff --git a/app/Http/Livewire/Associates/Auth/Register.php b/app/Http/Livewire/Associates/Auth/Register.php index a252a51..71cf3ad 100644 --- a/app/Http/Livewire/Associates/Auth/Register.php +++ b/app/Http/Livewire/Associates/Auth/Register.php @@ -65,63 +65,9 @@ class Register extends Component /** @var int */ public $contribution = null; - public function updated($field) + protected function rules($partial = false) { - $this->validateOnly($field, [ - 'address.city' => ['string'], - 'address.complement' => ['string'], - 'address.country' => ['string'], - 'address.neighbourhood' => ['string'], - 'address.number' => ['string'], - 'address.postcode' => ['string'], - 'address.state' => ['string'], - 'address.street' => ['string'], - 'birthday' => ['date_format:d/m/Y'], - 'contribution' => ['numeric'], - 'discussion' => ['string', 'in:all,daily,occasional'], - 'document.number' => ['string'], - 'document.type' => ['string', 'in:cpf,identity,passport'], - 'email' => ['email', 'unique:associates'], - 'profile.bike_activities' => ['string'], - 'profile.bike_use' => ['string'], - 'profile.comments' => ['string'], - 'profile.expectation' => ['string'], - 'profile.gender' => ['string', 'in:male,female,other'], - 'profile.occupation' => ['string'], - 'profile.org_participation' => ['string'], - 'profile.phone' => ['string'], - 'profile.scholarity' => ['string', 'in:primary-school,high-school,bachelor,master,phd'], - 'profile.secondary_emails' => ['string'], - 'profile.social' => ['string'], - 'profile.ucb_comments' => ['string'], - 'profile.website' => ['string'], - ]); - - if ($this->address['country'] === 'BR' && $field === 'address.postcode') { - $postcode = Str::slug($this->address['postcode'], ''); - - try { - $response = Http::timeout(5)->get("https://viacep.com.br/ws/$postcode/json"); - - if ($response->ok()) { - $address = $response->json(); - - $this->address['city'] = $response['localidade']; - $this->address['neighbourhood'] = $response['bairro']; - $this->address['state'] = $response['uf']; - $this->address['street'] = $response['logradouro']; - - $this->dispatchBrowserEvent('address-autofilled'); - } - } catch (\Illuminate\Http\Client\ConnectionException $exception) { - // TODO: show error to associate - } - } - } - - public function register() - { - $this->validate([ + $rules = [ 'address.city' => ['required', 'string'], 'address.complement' => ['nullable', 'string'], 'address.country' => ['required', 'string'], @@ -150,7 +96,49 @@ class Register extends Component 'profile.social' => ['nullable', 'string'], 'profile.ucb_comments' => ['nullable', 'string'], 'profile.website' => ['nullable', 'string'], - ]); + ]; + + if ($partial) { + $rules = collect($rules)->map(function ($rule, $index) { + return str_replace(['required', 'accepted'], 'nullable', $rule); + })->toArray(); + } + + return $rules; + } + + public function updated($field) + { + $this->validateOnly($field, collect($this->rules(true))); + + if ($this->address['country'] === 'BR' && $field === 'address.postcode') { + $postcode = Str::slug($this->address['postcode'], ''); + + try { + $response = Http::timeout(5)->get("https://viacep.com.br/ws/$postcode/json"); + + if ($response->ok()) { + $address = $response->json(); + + // TODO: Improve this error handling + if (! isset($address['erro'])) { + $this->address['city'] = $response['localidade']; + $this->address['neighbourhood'] = $response['bairro']; + $this->address['state'] = $response['uf']; + $this->address['street'] = $response['logradouro']; + + $this->dispatchBrowserEvent('address-autofilled'); + } + } + } catch (\Illuminate\Http\Client\ConnectionException $exception) { + // TODO: show error to associate + } + } + } + + public function register() + { + $this->validate($this->rules()); $associate = new Associate([ 'address' => $this->address,