forked from eatvio/eatvio-infra
121 lines
2.8 KiB
PHP
121 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Diet;
|
|
|
|
use App\Models\Diet;
|
|
use Illuminate\Contracts\View\Factory as ViewFactory;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Livewire\Component;
|
|
use Livewire\WithFileUploads;
|
|
|
|
class Edit extends Component
|
|
{
|
|
use WithFileUploads;
|
|
|
|
public Diet $diet;
|
|
|
|
public string $name = '';
|
|
|
|
public UploadedFile|string $image = '';
|
|
|
|
public int|string $duration = '7';
|
|
|
|
public string $state = 'draft';
|
|
|
|
public array $type = [];
|
|
|
|
public string $details = '';
|
|
|
|
public array $days = [];
|
|
|
|
public string $copyedDay;
|
|
|
|
public array $copyedDayData;
|
|
|
|
public function mount(Diet $diet): void
|
|
{
|
|
$this->name = $diet->name;
|
|
$this->image = $diet->image;
|
|
$this->duration = $diet->duration;
|
|
$this->state = $diet->state;
|
|
$this->type = $diet->type;
|
|
$this->details = $diet->details;
|
|
$this->days = $diet->daily_schedule;
|
|
}
|
|
|
|
public function render(): View|ViewFactory
|
|
{
|
|
return view('livewire.diet.edit');
|
|
}
|
|
|
|
public function dayUpdated(string $dayIndex, array $data): void
|
|
{
|
|
$this->days[$dayIndex] = $data;
|
|
}
|
|
|
|
public function showPreview(): void
|
|
{
|
|
$this->state = 'draft';
|
|
$this->publish();
|
|
}
|
|
|
|
public function addToDrafts(): void
|
|
{
|
|
$this->state = 'draft';
|
|
$this->publish();
|
|
}
|
|
|
|
public function makeCopy(string $day): void
|
|
{
|
|
/* dd($day); */
|
|
$this->copyedDay = $day;
|
|
}
|
|
|
|
public function makePaste(string $copyToDay): void
|
|
{
|
|
$data = $this->days[$this->copyedDay];
|
|
$this->dispatch(
|
|
'takePaste',
|
|
day: $copyToDay,
|
|
data: $data
|
|
)->to('diet.day-edit');
|
|
}
|
|
|
|
public function publish(): void
|
|
{
|
|
$this->validate();
|
|
|
|
$diet = $this->diet;
|
|
|
|
$diet->name = $this->name;
|
|
/* $diet->image = $this->image->store('diets', 'public'); */
|
|
$diet->image = ! is_string($this->image) ? $this->image->store('diets') : $this->image;
|
|
$diet->duration = $this->duration;
|
|
$diet->state = $this->state;
|
|
$diet->type = $this->type;
|
|
$diet->details = $this->details;
|
|
$diet->daily_schedule = $this->days;
|
|
$diet->save();
|
|
|
|
$this->redirectRoute('diet.show', $diet);
|
|
}
|
|
|
|
protected function getListeners(): array
|
|
{
|
|
return ['dayUpdated', 'makeCopy', 'makePaste'];
|
|
}
|
|
|
|
protected function rules(): array
|
|
{
|
|
return [
|
|
'name' => 'required|min:3',
|
|
/* 'image' => 'required|image|max:2048', */
|
|
'duration' => 'required|numeric|min:7',
|
|
'state' => 'required|in:active,draft,archived',
|
|
'type' => ['required', 'array:meat,veg,corb,alko', 'min:1'],
|
|
'details' => 'required|min:3',
|
|
];
|
|
}
|
|
}
|