forked from eatvio/eatvio-infra
98 lines
3.6 KiB
PHP
98 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Livewire\Profile;
|
|
|
|
use Livewire\Component;
|
|
use App\Models\User;
|
|
use App\Models\UserDetailz;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class EditDetailzForm extends Component
|
|
{
|
|
public User $user;
|
|
public UserDetailz $detailz;
|
|
public $userDetailzAttributes;
|
|
public $userAttributes;
|
|
public $currentPassword;
|
|
public $newPassword;
|
|
public $newPasswordConfirm;
|
|
public $validatedData = [];
|
|
|
|
protected $rules = [
|
|
'detailz.first_name' => 'required|string|min:3',
|
|
'detailz.last_name' => 'required|string|min:3',
|
|
'detailz.gender' => 'required|in:male,female',
|
|
'user.email' => 'required|email',
|
|
'detailz.birthdate' => 'required|date',
|
|
'detailz.country' => 'required|string|min:3',
|
|
'detailz.city' => 'required|string|min:3',
|
|
'detailz.lang' => 'required|string|min:3',
|
|
'detailz.weight' => 'required|integer|min:65',
|
|
'detailz.chest_circumference' => 'required|integer|min:20',
|
|
'detailz.waist_circumference' => 'required|integer|min:20',
|
|
'detailz.hip_circumference' => 'required|integer|min:20',
|
|
'detailz.blood_pressure_up' => 'required|integer|min:20',
|
|
'detailz.blood_pressure_down' => 'required|integer|min:20',
|
|
'detailz.has_hypo' => 'required|boolean',
|
|
'detailz.has_hyper' => 'required|boolean',
|
|
'detailz.about_alergy' => 'nullable',
|
|
'detailz.about_taking_pils' => 'nullable',
|
|
'detailz.about_sport_hobbies' => 'nullable',
|
|
'detailz.favorite_foods' => 'nullable',
|
|
'detailz.unfavorite_foods' => 'nullable',
|
|
'detailz.additional_info' => 'nullable',
|
|
];
|
|
|
|
public function mount(User $user)
|
|
{
|
|
$this->detailz = $user->detailz;
|
|
}
|
|
|
|
public function updated($name, $value)
|
|
{
|
|
/* if(preg_match('/password/xi', $name)) { */
|
|
/* $this->updatePassword(); */
|
|
/* } */
|
|
/* dump($name, $value); */
|
|
}
|
|
|
|
public function updatePassword()
|
|
{
|
|
$validatedData = Validator::make(
|
|
['currentPassword' => $this->currentPassword, 'newPassword' => $this->newPassword, 'newPasswordConfirm' => $this->newPasswordConfirm],
|
|
['currentPassword' => 'required|string|min:6', 'newPassword' => 'required|string|min:6', 'newPasswordConfirm' => 'required|string|min:6'],
|
|
['required' => 'The :attribute field is required']
|
|
)->after(function ($validator) {
|
|
if (! isset($this->currentPassword) || ! Hash::check($this->currentPassword, $this->user->password)) {
|
|
$validator->errors()->add('currentPassword', __('The provided password does not match your current password.'));
|
|
}
|
|
if ($this->newPassword !== $this->newPasswordConfirm) {
|
|
$validator->errors()->add('newPasswordConfirm', __('The password and confirm password fields do not match.'));
|
|
}
|
|
})->validate();
|
|
/* dd($validatedData); */
|
|
$this->user->forceFill([
|
|
'password' => Hash::make($validatedData['newPassword']),
|
|
])->save();
|
|
$this->validatedData = $validatedData;
|
|
$this->reset(['currentPassword', 'newPassword', 'newPasswordConfirm']);
|
|
/* dd($this->currentPassword, $this->newPassword, $this->newPasswordConfirm, $validatedData); */
|
|
}
|
|
|
|
public function refineDetailz()
|
|
{
|
|
$validated = $this->validate();
|
|
$this->user->fill($validated['user']);
|
|
$this->detailz->fill($validated['detailz']);
|
|
|
|
$this->user->save();
|
|
$this->detailz->save();
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.profile.edit-detailz-form');
|
|
}
|
|
}
|