#23 - wip
This commit is contained in:
51
app/Helpers/YearPeriodRetriever.php
Normal file
51
app/Helpers/YearPeriodRetriever.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Helpers;
|
||||
|
||||
use Toby\Models\YearPeriod;
|
||||
|
||||
class YearPeriodRetriever
|
||||
{
|
||||
public const SESSION_KEY = "selected_year_period";
|
||||
|
||||
public function selected(): YearPeriod
|
||||
{
|
||||
/** @var YearPeriod $yearPeriod */
|
||||
$yearPeriod = YearPeriod::query()->find(session()->get(static::SESSION_KEY));
|
||||
|
||||
return $yearPeriod !== null ? $yearPeriod : $this->current();
|
||||
}
|
||||
|
||||
public function current(): YearPeriod
|
||||
{
|
||||
return YearPeriod::current();
|
||||
}
|
||||
|
||||
public function links(): array
|
||||
{
|
||||
$current = $this->selected();
|
||||
|
||||
$years = YearPeriod::query()->whereIn("year", $this->offset($current->year))->get();
|
||||
$navigation = $years->map(fn(YearPeriod $yearPeriod) => $this->toNavigation($yearPeriod));
|
||||
|
||||
return [
|
||||
"current" => $current->year,
|
||||
"navigation" => $navigation->toArray(),
|
||||
];
|
||||
}
|
||||
|
||||
protected function offset(int $year): array
|
||||
{
|
||||
return range($year - 2, $year + 2);
|
||||
}
|
||||
|
||||
protected function toNavigation(YearPeriod $yearPeriod): array
|
||||
{
|
||||
return [
|
||||
"year" => $yearPeriod->year,
|
||||
"link" => route("year-periods.select", $yearPeriod->id),
|
||||
];
|
||||
}
|
||||
}
|
@@ -1,40 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Inertia\Response;
|
||||
use Toby\Http\Resources\VacationLimitResource;
|
||||
use Toby\Http\Resources\YearPeriodResource;
|
||||
use Toby\Models\YearPeriod;
|
||||
|
||||
class VacationDaysController extends Controller
|
||||
{
|
||||
public function edit(Request $request): Response
|
||||
{
|
||||
$year = $request->query("year", Carbon::now()->year);
|
||||
|
||||
/** @var YearPeriod $yearPeriod */
|
||||
$yearPeriod = YearPeriod::query()->where("year", $year)->firstOrFail();
|
||||
$previousYearPeriod = YearPeriod::query()->where("year", $year - 1)->first();
|
||||
$nextYearPeriod = YearPeriod::query()->where("year", $year + 1)->first();
|
||||
|
||||
|
||||
return inertia("VacationDays", [
|
||||
"vacationLimits" => VacationLimitResource::collection($yearPeriod->vacationLimits()->with("user")->get()),
|
||||
"yearPeriods" => [
|
||||
"prev" => $previousYearPeriod ? new YearPeriodResource($previousYearPeriod) : null,
|
||||
"current" => new YearPeriodResource($yearPeriod),
|
||||
"next" => $nextYearPeriod ? new YearPeriodResource($nextYearPeriod) : null,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request)
|
||||
{
|
||||
dump($request->get("items"));
|
||||
}
|
||||
}
|
35
app/Http/Controllers/VacationLimitController.php
Normal file
35
app/Http/Controllers/VacationLimitController.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Support\Arr;
|
||||
use Inertia\Response;
|
||||
use Toby\Http\Requests\VacationLimitRequest;
|
||||
use Toby\Http\Resources\VacationLimitResource;
|
||||
use Toby\Models\VacationLimit;
|
||||
|
||||
class VacationLimitController extends Controller
|
||||
{
|
||||
public function edit(): Response
|
||||
{
|
||||
return inertia("VacationLimits", [
|
||||
"limits" => VacationLimitResource::collection(VacationLimit::query()->with("user")->get()),
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(VacationLimitRequest $request): RedirectResponse
|
||||
{
|
||||
foreach ($request->data() as $data) {
|
||||
$limit = VacationLimit::query()->find($data["id"]);
|
||||
|
||||
$limit->update(Arr::only($data, ["has_vacation", "days"]));
|
||||
}
|
||||
|
||||
return redirect()
|
||||
->back()
|
||||
->with("success", __("Vacation limits have been updated"));
|
||||
}
|
||||
}
|
@@ -6,10 +6,15 @@ namespace Toby\Http\Middleware;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Middleware;
|
||||
use Toby\Helpers\YearPeriodRetriever;
|
||||
use Toby\Http\Resources\UserResource;
|
||||
|
||||
class HandleInertiaRequests extends Middleware
|
||||
{
|
||||
public function __construct(protected YearPeriodRetriever $yearPeriodRetriever)
|
||||
{
|
||||
}
|
||||
|
||||
public function share(Request $request): array
|
||||
{
|
||||
$user = $request->user();
|
||||
@@ -22,6 +27,7 @@ class HandleInertiaRequests extends Middleware
|
||||
"success" => $request->session()->get("success"),
|
||||
"error" => $request->session()->get("error"),
|
||||
],
|
||||
"years" => fn() => $user ? $this->yearPeriodRetriever->links() : [],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
30
app/Http/Requests/VacationLimitRequest.php
Normal file
30
app/Http/Requests/VacationLimitRequest.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class VacationLimitRequest extends FormRequest
|
||||
{
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
"items" => ["required", "array"],
|
||||
"items.*.id" => ["required", "exists:vacation_limits,id"],
|
||||
"items.*.hasVacation" => ["required", "boolean"],
|
||||
"items.*.days" => ["exclude_if:items.*.hasVacation,false", "required", "integer", "min:0"],
|
||||
];
|
||||
}
|
||||
|
||||
public function data(): Collection
|
||||
{
|
||||
return $this->collect("items")->map(fn(array $item): array => [
|
||||
"id" => $item["id"],
|
||||
"has_vacation" => $item["hasVacation"],
|
||||
"days" => $item["days"],
|
||||
]);
|
||||
}
|
||||
}
|
@@ -8,11 +8,12 @@ use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class VacationLimitResource extends JsonResource
|
||||
{
|
||||
public static $wrap = null;
|
||||
public static $wrap = false;
|
||||
|
||||
public function toArray($request): array
|
||||
{
|
||||
return [
|
||||
"id" => $this->id,
|
||||
"user" => new UserResource($this->user),
|
||||
"hasVacation" => $this->has_vacation,
|
||||
"days" => $this->days,
|
||||
|
@@ -1,20 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class YearPeriodResource extends JsonResource
|
||||
{
|
||||
public static $wrap = false;
|
||||
|
||||
public function toArray($request): array
|
||||
{
|
||||
return [
|
||||
"id" => $this->id,
|
||||
"year" => $this->year,
|
||||
];
|
||||
}
|
||||
}
|
@@ -19,6 +19,8 @@ class VacationLimit extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $guarded = [];
|
||||
|
||||
protected $casts = [
|
||||
"has_vacation" => "boolean",
|
||||
];
|
||||
|
@@ -7,7 +7,9 @@ namespace Toby\Providers;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Toby\Models\User;
|
||||
use Toby\Models\VacationLimit;
|
||||
use Toby\Observers\UserObserver;
|
||||
use Toby\Scopes\SelectedYearPeriodScope;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
@@ -16,5 +18,7 @@ class AppServiceProvider extends ServiceProvider
|
||||
User::observe(UserObserver::class);
|
||||
|
||||
Carbon::macro("toDisplayString", fn() => $this->translatedFormat("j F Y"));
|
||||
|
||||
VacationLimit::addGlobalScope($this->app->make(SelectedYearPeriodScope::class));
|
||||
}
|
||||
}
|
||||
|
22
app/Scopes/SelectedYearPeriodScope.php
Normal file
22
app/Scopes/SelectedYearPeriodScope.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Scopes;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Scope;
|
||||
use Toby\Helpers\YearPeriodRetriever;
|
||||
|
||||
class SelectedYearPeriodScope implements Scope
|
||||
{
|
||||
public function __construct(protected YearPeriodRetriever $yearPeriodRetriever)
|
||||
{
|
||||
}
|
||||
|
||||
public function apply(Builder $builder, Model $model): Builder
|
||||
{
|
||||
return $builder->where("year_period_id", $this->yearPeriodRetriever->selected()->id);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user