forked from eatvio/eatvio-infra
175 lines
4.6 KiB
PHP
175 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Registration;
|
|
|
|
use App\Actions\Fortify\CreateNewUser;
|
|
use App\Services\RegistrationRuleSet;
|
|
use Illuminate\Auth\Events\Registered;
|
|
use Illuminate\Contracts\View\Factory as ViewFactory;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Support\Str;
|
|
use Livewire\Component;
|
|
use Livewire\Features\SupportRedirects\Redirector;
|
|
|
|
class Step1 extends Component
|
|
{
|
|
public string $step = 'step1';
|
|
|
|
public string $first_name = '';
|
|
|
|
public string $last_name = '';
|
|
|
|
public string $email = '';
|
|
|
|
public string $password = '';
|
|
|
|
public string $password_confirmation = '';
|
|
|
|
public string $birthdate = '';
|
|
|
|
public string $gender = 'male';
|
|
|
|
public string $country = 'ru';
|
|
|
|
public string $city = 'msk';
|
|
|
|
public string $lang = 'ru';
|
|
|
|
public int|string $weight = '';
|
|
|
|
public int|string $height = '';
|
|
|
|
public int|string $chest_circumference = '';
|
|
|
|
public int|string $waist_circumference = '';
|
|
|
|
public int|string $hip_circumference = '';
|
|
|
|
public int|string $blood_pressure_up = '';
|
|
|
|
public int|string $blood_pressure_down = '';
|
|
|
|
public int|string $has_hypo = 0;
|
|
|
|
public int|string $has_hyper = 0;
|
|
|
|
public string $about_alergy = '';
|
|
|
|
public string $about_taking_pils = '';
|
|
|
|
public string $about_sport_hobbies = '';
|
|
|
|
public string $favorite_foods = '';
|
|
|
|
public string $unfavorite_foods = '';
|
|
|
|
public string $additional_info = '';
|
|
|
|
public function mount(string $step): void
|
|
{
|
|
$this->step = $step;
|
|
collect(session()->get('registrationdata', []))
|
|
->each(fn ($item, $key) => $this->{$key} = $item);
|
|
}
|
|
|
|
public function render(): View|ViewFactory
|
|
{
|
|
return view(
|
|
'livewire.registration.'.$this->step,
|
|
['step' => $this->step]
|
|
)->extends('layouts.register');
|
|
}
|
|
|
|
public function nextStep(): RedirectResponse|Redirector
|
|
{
|
|
$validatedData = Validator::make(
|
|
$this->getValidateData($this->step),
|
|
$this->getValidateRules($this->step),
|
|
)->validate();
|
|
|
|
$registrationdata = session()->get('registrationdata', []);
|
|
$mergetdRegistrationData = array_merge($validatedData, $registrationdata);
|
|
|
|
session()->put('registrationdata', $mergetdRegistrationData);
|
|
|
|
return redirect()
|
|
->route(
|
|
'register.step1',
|
|
[
|
|
'step' => $this->getNextStep(),
|
|
]
|
|
);
|
|
}
|
|
|
|
public function skipStep(): RedirectResponse|Redirector
|
|
{
|
|
$validatedData = $this->getValidateData($this->step);
|
|
$registrationdata = session()->get('registrationdata', []);
|
|
$mergetdRegistrationData = array_merge($validatedData, $registrationdata);
|
|
|
|
session()->put('registrationdata', $mergetdRegistrationData);
|
|
|
|
return redirect()
|
|
->route(
|
|
'register.step1',
|
|
[
|
|
'step' => $this->getNextStep(),
|
|
]
|
|
);
|
|
}
|
|
|
|
public function goBack(): RedirectResponse|Redirector
|
|
{
|
|
$nextStep = 'step'.((int) Str::replaceFirst('step', '', $this->step) - 1);
|
|
|
|
return redirect()
|
|
->route(
|
|
'register.step1',
|
|
[
|
|
'step' => $nextStep,
|
|
]
|
|
);
|
|
}
|
|
|
|
public function finishRegistration(): RedirectResponse|Redirector
|
|
{
|
|
$curData = $this->getValidateData($this->step);
|
|
$curRules = $this->getValidateRules($this->step);
|
|
$validatedData = Validator::make(
|
|
$curData,
|
|
$curRules,
|
|
)->validate();
|
|
|
|
$registrationdata = session()->get('registrationdata', []);
|
|
$mergedRegistrationData = array_merge($validatedData, $registrationdata);
|
|
$mergedRegistrationData['password_confirmation'] = $mergedRegistrationData['password'];
|
|
$mergedRegistrationData['terms'] = 'yes';
|
|
|
|
$user = (new CreateNewUser())->create($mergedRegistrationData);
|
|
|
|
event(new Registered($user));
|
|
|
|
Auth::login($user);
|
|
|
|
return redirect()->route('profile.show', $user);
|
|
}
|
|
|
|
private function getValidateRules(string $key): array
|
|
{
|
|
return RegistrationRuleSet::getValidateRules($key);
|
|
}
|
|
|
|
private function getValidateData(string $key): array
|
|
{
|
|
return RegistrationRuleSet::getValidateData($key, $this);
|
|
}
|
|
|
|
private function getNextStep(): string
|
|
{
|
|
return 'step'.((int) Str::replaceFirst('step', '', $this->step) + 1);
|
|
}
|
|
}
|