Files
eatvio-infra/app/Livewire/Registration/Step1.php
2023-10-04 17:57:09 +03:00

225 lines
7.4 KiB
PHP

<?php
namespace App\Livewire\Registration;
use Livewire\Component;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rules\Password;
use Illuminate\Validation\Rule;
use Illuminate\Auth\Events\Registered;
use App\Rules\MinWords;
use App\Actions\Fortify\CreateNewUser;
// use Laravel\Fortify\Contracts\RegisterResponse;
// use Illuminate\Contracts\Auth\StatefulGuard;
// use App\Models\User;
// use App\Models\UserDetailz;
use Illuminate\Support\Facades\Auth;
// use \Mcamara\LaravelLocalization\Facades\LaravelLocalization;
class Step1 extends Component
{
public $step;
public $first_name;
public $last_name;
public $email;
public $password;
public $password_confirmation;
public $birthdate;
public $gender = 'male';
public $country = 'ru';
public $city = 'msk';
public $lang = 'ru';
public $weight;
public $height;
public $chest_circumference;
public $waist_circumference;
public $hip_circumference;
public $blood_pressure_up;
public $blood_pressure_down;
public $has_hypo = 0;
public $has_hyper = 0;
public $about_alergy;
public $about_taking_pils;
public $about_sport_hobbies;
public $favorite_foods;
public $unfavorite_foods;
public $additional_info;
public function mount($step)
{
$this->step = $step;
collect(session()->get('registrationdata', []))
->each(fn ($item, $key) => $this->{$key} = $item);
}
public function render()
{
return view('livewire.registration.' . $this->step, ['step' => $this->step])
->extends('layouts.register');
}
private function getValidateRules($key)
{
$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($key)
{
$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()
{
$nextStep = "step" . (int)Str::replaceFirst('step', '', $this->step) + 1;
return $nextStep;
}
private function getValidateMessages()
{
$data = ['required' => 'The :attribute field is required'];
return $data;
}
public function nextStep()
{
$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()
{
return redirect()
->route(
'register.step1',
[
'step' => $this->getNextStep()
]
);
}
public function goBack()
{
$nextStep = "step" . (int)Str::replaceFirst('step', '', $this->step) - 1;
return redirect()
->route(
'register.step1',
[
'step' => $nextStep
]
);
}
public function finishRegistretion()
{
$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);
}
}