<?php
|
|
|
|
namespace App\Http\Livewire\Auth\Register;
|
|
|
|
use App\Providers\RouteServiceProvider;
|
|
use App\User;
|
|
use App\UserCategory;
|
|
use App\UserNature;
|
|
use App\UserType;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Str;
|
|
use Livewire\Component;
|
|
|
|
class Individual extends Component
|
|
{
|
|
/** @var string */
|
|
public $name = '';
|
|
|
|
/** @var string */
|
|
public $birthday = '';
|
|
|
|
/** @var array */
|
|
public $document = [
|
|
'type' => '',
|
|
'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;
|
|
|
|
public function updated($field)
|
|
{
|
|
$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:users'],
|
|
'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 user
|
|
}
|
|
}
|
|
}
|
|
|
|
public function register()
|
|
{
|
|
$this->validate([
|
|
'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:users'],
|
|
'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'],
|
|
]);
|
|
|
|
$user = new User([
|
|
'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,
|
|
]);
|
|
|
|
$user->user_category_id = UserCategory::where('key', 'individual')->first()->id;
|
|
$user->user_nature_id = UserNature::where('key', 'individual')->first()->id;
|
|
$user->user_type_id = UserType::where('key', 'individual')->first()->id;
|
|
|
|
$user->save();
|
|
|
|
Auth::login($user, true);
|
|
|
|
redirect(route('home'));
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.auth.register.individual');
|
|
}
|
|
}
|