Skip to content

Commit

Permalink
#377 - feat: added changing year from calendar buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilpiech97 committed Jul 17, 2024
1 parent 51f61ed commit 6673202
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
32 changes: 30 additions & 2 deletions app/Http/Controllers/VacationCalendarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

namespace Toby\Http\Controllers;

use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Inertia\Response;
use Toby\Domain\CalendarGenerator;
use Toby\Enums\Month;
use Toby\Helpers\YearPeriodRetriever;
use Toby\Http\Resources\SimpleUserResource;
use Toby\Models\User;
use Toby\Models\YearPeriod;

class VacationCalendarController extends Controller
{
Expand All @@ -20,12 +23,25 @@ public function index(
YearPeriodRetriever $yearPeriodRetriever,
CalendarGenerator $calendarGenerator,
?string $month = null,
): Response {
?int $year = null,
): Response|RedirectResponse {
if ($year !== null) {
return $this->changeYearPeriod($request, $month, $year);
}

$month = Month::fromNameOrCurrent((string)$month);
$currentUser = $request->user();
$withTrashedUsers = $currentUser->canSeeInactiveUsers();

$yearPeriod = $yearPeriodRetriever->selected();
$previousYearPeriod = YearPeriod::query()
->where("year", "<", $yearPeriod->year)
->orderBy("year", "desc")
->first();
$nextYearPeriod = YearPeriod::query()
->where("year", ">", $yearPeriod->year)
->orderBy("year")
->first();
$carbonMonth = Carbon::create($yearPeriod->year, $month->toCarbonNumber());

$users = User::query()
Expand All @@ -42,9 +58,21 @@ public function index(
return inertia("Calendar", [
"calendar" => $calendar,
"current" => Month::current(),
"selected" => $month->value,
"selectedMonth" => $month->value,
"users" => SimpleUserResource::collection($users),
"withBlockedUsers" => $withTrashedUsers,
"previousYearPeriod" => $previousYearPeriod,
"nextYearPeriod" => $nextYearPeriod,
]);
}

private function changeYearPeriod(Request $request, string $month, int $year): RedirectResponse
{
$yearPeriod = YearPeriod::query()->where("year", $year)->firstOrFail();
$request->session()->put(YearPeriodRetriever::SESSION_KEY, $yearPeriod->id);
Cache::forget("selected_year_period");

return redirect()->route("calendar", ["month" => $month])
->with("info", __("Year period changed."));
}
}
1 change: 1 addition & 0 deletions lang/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"Holiday updated.": "Dzień wolny zaktualizowany.",
"Holiday deleted.": "Dzień wolny usunięty.",
"Selected year period changed.": "Wybrany rok zmieniony.",
"Year period changed.": "Zmieniono rok.",
"Vacation limits updated.": "Limity urlopów zaktualizowane.",
"Request created.": "Wniosek utworzony.",
"Request accepted.": "Wniosek zaakceptowany.",
Expand Down
28 changes: 23 additions & 5 deletions resources/js/Pages/Calendar.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup>
import { ChevronLeftIcon, ChevronRightIcon, ChevronUpDownIcon } from '@heroicons/vue/24/solid'
import { ChevronLeftIcon, ChevronRightIcon, ChevronDoubleRightIcon, ChevronDoubleLeftIcon, ChevronUpDownIcon } from '@heroicons/vue/24/solid'
import { computed, ref, watch } from 'vue'
import { useMonthInfo } from '@/Composables/monthInfo.js'
import VacationTypeCalendarIcon from '@/Shared/VacationTypeCalendarIcon.vue'
Expand All @@ -14,8 +14,10 @@ const props = defineProps({
auth: Object,
calendar: Object,
current: String,
selected: String,
selectedMonth: String,
years: Object,
previousYearPeriod: Object,
nextYearPeriod: Object,
})
let activeElement = ref(undefined)
Expand All @@ -25,7 +27,7 @@ const { getMonths, findMonth } = useMonthInfo()
const months = getMonths()
const form = useForm({
selectedMonth: months.find(month => month.value === props.selected),
selectedMonth: months.find(month => month.value === props.selectedMonth),
})
watch(() => form.selectedMonth, (value) => {
Expand All @@ -34,7 +36,7 @@ watch(() => form.selectedMonth, (value) => {
}
})
const selectedMonth = computed(() => findMonth(props.selected))
const selectedMonth = computed(() => findMonth(props.selectedMonth))
const previousMonth = computed(() => months[months.indexOf(selectedMonth.value) - 1])
const nextMonth = computed(() => months[months.indexOf(selectedMonth.value) + 1])
Expand Down Expand Up @@ -77,6 +79,14 @@ function linkVacationRequest(user) {
>
<ChevronLeftIcon class="w-5 h-5" />
</InertiaLink>
<InertiaLink
v-else-if="previousYearPeriod"
:href="`/calendar/${months[11].value}/${previousYearPeriod.year}`"
as="button"
class="flex focus:relative justify-center items-center p-2 text-gray-400 hover:text-gray-500 bg-white rounded-l-md border border-r-0 border-gray-300 md:px-2 md:w-9 md:hover:bg-gray-50"
>
<ChevronDoubleLeftIcon class="w-5 h-5" />
</InertiaLink>
<span
v-else
class="flex justify-center items-center text-gray-400 bg-gray-100 rounded-l-md border border-r-0 border-gray-300 md:px-2 md:w-9"
Expand Down Expand Up @@ -134,9 +144,17 @@ function linkVacationRequest(user) {
>
<ChevronRightIcon class="w-5 h-5" />
</InertiaLink>
<InertiaLink
v-else-if="nextYearPeriod"
:href="`/calendar/${months[0].value}/${nextYearPeriod.year}`"
as="button"
class="flex focus:relative justify-center items-center p-2 text-gray-400 hover:text-gray-500 bg-white rounded-r-md border border-l-0 border-gray-300 focus:outline-blumilk-500 md:px-2 md:w-9 md:hover:bg-gray-50"
>
<ChevronDoubleRightIcon class="w-5 h-5" />
</InertiaLink>
<span
v-else
class="flex justify-center items-center p-2 text-gray-400 bg-gray-100 rounded-r-md border border-l-0 border-gray-300 md:px-2 md:w-9"
class="flex justify-center items-center text-gray-400 bg-gray-100 rounded-r-md border border-l-0 border-gray-300 md:px-2 md:w-9"
>
<ChevronRightIcon class="w-5 h-5" />
</span>
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
->whereNumber("yearPeriod")
->name("year-periods.select");

Route::get("/calendar/{month?}", [VacationCalendarController::class, "index"])
Route::get("/calendar/{month?}/{year?}", [VacationCalendarController::class, "index"])
->name("calendar");

Route::prefix("/vacation")->as("vacation.")->group(function (): void {
Expand Down

0 comments on commit 6673202

Please sign in to comment.