Files
eatvio-infra/app/Http/Livewire/MealForm.php
2022-06-02 12:02:58 +00:00

190 lines
5.3 KiB
PHP

<?php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\Diary;
use App\Models\Meal;
use App\Models\Portion;
class MealForm extends Component
{
public $time;
public $type = 0;
public $satiety = 3;
public $mood = 3;
public $product_name;
public $portions;
public $quantity;
public $pcs;
public $unit;
public $diaryId;
public Diary $diary;
public Meal $meal;
public $portion;
protected $rules = [
'product_name' => 'required|min:3',
'quantity' => 'required',
'portion' => 'required|min:1'
];
protected $listeners = ['food-intake-updated' => '$refresh'];
public function mount()
{
$this->diary = Diary::find($this->diaryId);
$curMeal = $this->diary->meals->where('type', 0)->first();
$this->time = $curMeal->time ?? now()->setTimezone('3')->format('H:i');
$this->satiety = $curMeal->satiety ?? 3;
$this->mood = $curMeal->mood ?? 3;
$curPortions = $this
->diary
->meals
->reduce(
fn($acc, $item) =>
[...$item->portions->map(
fn($i) =>
[...$i->toArray(), 'type' => $item->type])
,
...$acc
],
[]
);
$this->portions = $curPortions;
}
public function updatingType($value)
{
$curMeal = $this->diary->meals->where('type', $value)->first();
$this->satiety = $curMeal->satiety ?? 3;
$this->mood = $curMeal->mood ?? 3;
$this->time = $curMeal->time ?? now()->setTimezone('3')->format('H:i');
}
public function render()
{
$types = [0, 1, 2, 3, 4];
$typeNames = ['Завтрак', 'Обед', 'Ужин', 'Ланч', 'Перекусы'];
$typeCssIds = ['breakfast', 'dinner', 'supper', 'lunch', 'snacks'];
return view(
'livewire.meal-form',
[
'types' => $types,
'typeNames' => $typeNames,
'typeCssIds' => $typeCssIds,
'curMeal' => $this
->diary
->meals
->where('type', $this->type)
->first(),
]
);
}
public function setType(int $type): void
{
$this->type = $type;
}
public function addFoodIntake(Diary $diary, Meal $meal, Portion $portion)
{
$this->addPortionIfItFilled();
if (!$diary->exists) {
$curMeal = Meal::where(
[
'diary_id' => $this->diary->id,
'user_id' => auth()->id(),
'type' => $this->type,
]
)->first() ?? new Meal();
$curMeal->diary_id = $this->diary->id;
$curMeal->user_id = auth()->id();
$curMeal->time = $this->time;
$curMeal->type = $this->type;
$curMeal->satiety = $this->satiety;
$curMeal->mood = $this->mood;
$curMeal->save();
foreach($this->portions as $portion) {
if ($portion['type'] == $this->type) {
$portionData = [
'meal_id' => $curMeal->id,
'product_name' => $portion['product_name'],
'portion' => $portion['portion'],
'quantity' => $portion['quantity'],
];
$curPortion = Portion::where($portionData)->first() ?? new Portion();
$curPortion->meal_id = $curMeal->id;
$curPortion->product_name = $portion['product_name'];
$curPortion->portion = $portion['portion'];
$curPortion->quantity = $portion['quantity'];
$curPortion->save();
}
}
}
/* $this->emit('food-intake-updated'); */
$this->emitSelf('food-intake-updated');
}
public function addPortion()
{
$this->validate();
$data = [
'type' => $this->type,
'product_name' => $this->product_name,
'quantity' => $this->quantity,
'portion' => $this->portion
];
$newPortions = [...$this->portions, $data];
$this->portions = $newPortions;
$this->reset(['product_name', 'quantity', 'portion']);
}
public function editPortion($key, $item)
{
$filed = [
'product_name' => $item['product_name'],
'quantity' => $item['quantity'],
'portion' => $item['portion'],
];
$this->delPortion($key, $item);
$this->fill($filed);
}
private function addPortionIfItFilled()
{
if (mb_strlen($this->product_name) && $this->quantity && mb_strlen($this->portion)) {
$this->addPortion();
}
}
public function delPortion($key, $item)
{
if (isset($item['id'])) {
Portion::find($item['id'])->delete();
}
$filtered = array_filter(
$this->portions,
function ($i) use ($key) {
return $i != $key;
},
ARRAY_FILTER_USE_KEY
);
$this->portions = array_values($filtered);
}
}