Sistema de controles da União de Ciclistas do Brasil
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 

181 lines
6.4 KiB

<?php
namespace App\Http\Livewire\Associates\Auth\Register;
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 Individual 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;
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: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([
'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'],
]);
$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.individual');
}
}