forked from eatvio/eatvio-infra
248 lines
7.9 KiB
PHP
248 lines
7.9 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Registration;
|
|
|
|
use App\Actions\Fortify\CreateNewUser;
|
|
use App\Rules\MinWords;
|
|
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 Illuminate\Validation\Rule;
|
|
use Illuminate\Validation\Rules\Password;
|
|
use Livewire\Component;
|
|
use Livewire\Features\SupportRedirects\Redirector;
|
|
|
|
class Step1 extends Component
|
|
{
|
|
public string $step;
|
|
|
|
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 string $weight = '';
|
|
|
|
public string $height = '';
|
|
|
|
public string $chest_circumference = '';
|
|
|
|
public string $waist_circumference = '';
|
|
|
|
public string $hip_circumference = '';
|
|
|
|
public string $blood_pressure_up = '';
|
|
|
|
public 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 finishRegistretion(): RedirectResponse|Redirector
|
|
{
|
|
$validatedData = Validator::make(
|
|
$this->getValidateData($this->step),
|
|
$this->getValidateRules($this->step),
|
|
)->validate();
|
|
|
|
$registrationdata = session()->get('registrationdata', []);
|
|
$mergedRegistrationData = array_merge($validatedData, $registrationdata);
|
|
$mergedRegistrationData['password_confirmation'] = $mergedRegistrationData['password'];
|
|
$mergedRegistrationData['terms'] = 'yes';
|
|
|
|
$user = (new CreateNewUser())->create($mergedRegistrationData);
|
|
/* LaravelLocalization::setLocale(); */
|
|
event(new Registered($user));
|
|
|
|
Auth::login($user);
|
|
|
|
return redirect()->route('profile.show', $user->id);
|
|
}
|
|
|
|
private function getValidateRules(string $key): array
|
|
{
|
|
$rules = [
|
|
'step1' => [
|
|
'first_name' => 'required|min:3',
|
|
'last_name' => 'required|min:3',
|
|
'password' => ['required', 'confirmed', Password::min(8)],
|
|
'email' => ['required', 'email', 'unique:users,email'],
|
|
'birthdate' => 'required|date',
|
|
'gender' => ['required', Rule::in(['male', 'female'])],
|
|
],
|
|
'step2' => [
|
|
'country' => 'sometimes|required|min:2',
|
|
'city' => 'sometimes|required|min:2',
|
|
'lang' => 'sometimes|required|min:2',
|
|
'weight' => 'sometimes|required|numeric',
|
|
'height' => 'sometimes|required|numeric',
|
|
],
|
|
'step3' => [
|
|
'chest_circumference' => 'sometimes|required|numeric',
|
|
'waist_circumference' => 'sometimes|required|numeric',
|
|
'hip_circumference' => 'sometimes|required|numeric',
|
|
],
|
|
'step4' => [
|
|
'blood_pressure_up' => 'sometimes|required|numeric',
|
|
'blood_pressure_down' => 'sometimes|required|numeric',
|
|
'has_hypo' => ['sometimes', 'required', Rule::in([0, 1])],
|
|
'has_hyper' => ['sometimes', 'required', Rule::in([0, 1])],
|
|
],
|
|
'step5' => ['about_alergy' => ['sometimes', 'nullable', new MinWords(1)]],
|
|
'step6' => ['about_taking_pils' => ['sometimes', 'nullable', new MinWords(1)]],
|
|
'step7' => ['about_sport_hobbies' => ['sometimes', 'nullable', new MinWords(1)]],
|
|
'step8' => [
|
|
'favorite_foods' => ['sometimes', 'nullable', new MinWords(1)],
|
|
'unfavorite_foods' => ['sometimes', 'nullable', new MinWords(1)],
|
|
],
|
|
'step9' => ['additional_info' => ['nullable', new MinWords(1)]],
|
|
];
|
|
|
|
return $rules[$key];
|
|
}
|
|
|
|
private function getValidateData(string $key): array
|
|
{
|
|
$data = [
|
|
'step1' => [
|
|
'first_name' => $this->first_name,
|
|
'last_name' => $this->last_name,
|
|
'email' => $this->email,
|
|
'password' => $this->password,
|
|
'password_confirmation' => $this->password_confirmation,
|
|
'birthdate' => $this->birthdate,
|
|
'gender' => $this->gender,
|
|
],
|
|
'step2' => [
|
|
'country' => $this->country,
|
|
'city' => $this->city,
|
|
'lang' => $this->lang,
|
|
'weight' => $this->weight,
|
|
'height' => $this->height,
|
|
],
|
|
'step3' => [
|
|
'chest_circumference' => $this->chest_circumference,
|
|
'waist_circumference' => $this->waist_circumference,
|
|
'hip_circumference' => $this->hip_circumference,
|
|
],
|
|
'step4' => [
|
|
'blood_pressure_up' => $this->blood_pressure_up,
|
|
'blood_pressure_down' => $this->blood_pressure_down,
|
|
'has_hypo' => $this->has_hypo,
|
|
'has_hyper' => $this->has_hyper,
|
|
],
|
|
'step5' => ['about_alergy' => $this->about_alergy],
|
|
'step6' => ['about_taking_pils' => $this->about_taking_pils],
|
|
'step7' => ['about_sport_hobbies' => $this->about_sport_hobbies],
|
|
'step8' => [
|
|
'favorite_foods' => $this->favorite_foods,
|
|
'unfavorite_foods' => $this->unfavorite_foods,
|
|
],
|
|
'step9' => ['additional_info' => $this->additional_info],
|
|
];
|
|
|
|
return $data[$key];
|
|
}
|
|
|
|
private function getNextStep(): string
|
|
{
|
|
return 'step'.((int) Str::replaceFirst('step', '', $this->step) + 1);
|
|
}
|
|
}
|