<?php
|
|
|
|
namespace App\Http\Livewire\Associates\Auth;
|
|
|
|
use App\Providers\RouteServiceProvider;
|
|
use App\Associate;
|
|
use App\AssociateCategory;
|
|
use App\AssociateNature;
|
|
use App\AssociateType;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Str;
|
|
use Livewire\Component;
|
|
|
|
class Register extends Component
|
|
{
|
|
/** @var string */
|
|
public $name = '';
|
|
|
|
/** @var string */
|
|
public $birthday = '';
|
|
|
|
/** @var array */
|
|
public $document = [
|
|
'type' => 'cpf',
|
|
'number' => '',
|
|
];
|
|
|
|
/** @var array */
|
|
public $address = [
|
|
'city' => '',
|
|
'complement' => '',
|
|
'country' => 'BR',
|
|
'neighbourhood' => '',
|
|
'number' => '',
|
|
'postcode' => '',
|
|
'state' => '',
|
|
'street' => '',
|
|
];
|
|
|
|
/** @var array */
|
|
public $profile = [
|
|
'gender' => '',
|
|
'occupation' => '',
|
|
'scholarity' => '',
|
|
'phone' => '',
|
|
'secondary_emails' => '',
|
|
'website' => '',
|
|
'social' => '',
|
|
'expectation' => '',
|
|
'bike_use' => '',
|
|
'org_participation' => '',
|
|
'bike_activities' => '',
|
|
'comments' => '',
|
|
'ucb_comments' => '',
|
|
];
|
|
|
|
/** @var string */
|
|
public $email = '';
|
|
|
|
/** @var string */
|
|
public $discussion = '';
|
|
|
|
/** @var int */
|
|
public $contribution = null;
|
|
|
|
/** @var string */
|
|
public $form;
|
|
|
|
protected function rules($partial = false)
|
|
{
|
|
$rules = [
|
|
'address.city' => ['required', 'string'],
|
|
'address.complement' => ['nullable', 'string'],
|
|
'address.country' => ['required', 'string'],
|
|
'address.neighbourhood' => ['required', 'string'],
|
|
'address.number' => ['required', 'string'],
|
|
'address.postcode' => ['required', 'string'],
|
|
'address.state' => ['required', 'string'],
|
|
'address.street' => ['required', 'string'],
|
|
'birthday' => ['required', 'date_format:d/m/Y'],
|
|
'contribution' => ['required', 'numeric'],
|
|
'discussion' => ['required', 'string', 'in:all,daily,occasional'],
|
|
'document.number' => ['required', 'string'],
|
|
'document.type' => ['required', 'string', 'in:cpf,identity,passport'],
|
|
'email' => ['required', 'email', 'unique:associates'],
|
|
'name' => ['required'],
|
|
'profile.bike_activities' => ['nullable', 'string'],
|
|
'profile.bike_use' => ['nullable', 'string'],
|
|
'profile.comments' => ['nullable', 'string'],
|
|
'profile.expectation' => ['nullable', 'string'],
|
|
'profile.gender' => ['required', 'string', 'in:male,female,other'],
|
|
'profile.occupation' => ['required', 'string'],
|
|
'profile.org_participation' => ['nullable', 'string'],
|
|
'profile.phone' => ['required', 'string'],
|
|
'profile.scholarity' => ['required', 'string', 'in:primary-school,high-school,bachelor,master,phd'],
|
|
'profile.secondary_emails' => ['nullable', 'string'],
|
|
'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 mount($form = 'individual')
|
|
{
|
|
$this->form = $form;
|
|
}
|
|
|
|
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,
|
|
'birthday' => Carbon::createFromFormat('d/m/Y', $this->birthday),
|
|
'contribution' => $this->contribution,
|
|
'discussion' => $this->discussion,
|
|
'document' => $this->document,
|
|
'email' => $this->email,
|
|
'name' => $this->name,
|
|
'profile' => $this->profile,
|
|
]);
|
|
|
|
$associate->associate_category_id = AssociateCategory::where('key', 'individual')->first()->id;
|
|
$associate->associate_nature_id = AssociateNature::where('key', 'individual')->first()->id;
|
|
$associate->associate_type_id = AssociateType::where('key', 'individual')->first()->id;
|
|
|
|
$associate->save();
|
|
|
|
Auth::login($associate, true);
|
|
|
|
redirect(route('home'));
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.associates.auth.register');
|
|
}
|
|
}
|