Skip to content

Commit

Permalink
#146 - external schedule link (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztofrewak authored Sep 17, 2024
1 parent 3d67dec commit 53ae281
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/Http/Controllers/Dashboard/SettingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function update(SettingRequest $request, FilesystemManager $filesystem):
if ($settings->logo) {
$filesystem->disk("public")->delete($settings->logo);
}

$file = $request->file("logo");
$fileName = $file->getClientOriginalName();
$path = "/logo";
Expand Down
18 changes: 18 additions & 0 deletions app/Http/Middleware/HandleInertiaRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace Keating\Http\Middleware;

use Closure;
use Illuminate\Cache\CacheManager;
use Illuminate\Http\Request;
use Inertia\Middleware;
use Keating\Models\Setting;

class HandleInertiaRequests extends Middleware
{
Expand All @@ -15,6 +17,7 @@ public function share(Request $request): array
return array_merge(parent::share($request), [
"auth" => $this->getAuthData($request),
"flash" => $this->getFlashData($request),
"settings" => $this->getSettingsData($request),
]);
}

Expand All @@ -33,4 +36,19 @@ protected function getFlashData(Request $request): Closure
"info" => $request->session()->get("info"),
];
}

protected function getSettingsData(Request $request): Closure
{
/** @var CacheManager $cache */
$cache = app("cache");

return fn(): array => [
"scheduleLink" => $cache->get("scheduleLink", function () use ($cache): ?string {
$link = Setting::query()->first()?->schedule_link;
$cache->put("scheduleLink", $link);

return $link;
}),
];
}
}
2 changes: 2 additions & 0 deletions app/Http/Requests/SettingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function rules(): array
"teacher_titles" => ["required", "max:255"],
"university_name" => ["required", "max:255"],
"department_name" => ["required", "max:255"],
"schedule_link" => ["nullable", "url"],
"primary_color" => ["required", "regex:/^#([A-Fa-f0-9]{6})$/"],
"secondary_color" => ["required", "regex:/^#([A-Fa-f0-9]{6})$/"],
"logo" => ["nullable", "image", "max:1024"],
Expand All @@ -30,6 +31,7 @@ public function getData(): array
"teacher_titles" => $this->input("teacher_titles"),
"university_name" => $this->input("university_name"),
"department_name" => $this->input("department_name"),
"schedule_link" => $this->input("schedule_link"),
"primary_color" => $this->input("primary_color"),
"secondary_color" => $this->input("secondary_color"),
];
Expand Down
5 changes: 5 additions & 0 deletions app/Models/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
* @property string $teacher_titles
* @property string $university_name
* @property string $department_name
* @property string $schedule_link
* @property string $primary_color
* @property string $secondary_color
* @property string $logo
* @property Carbon $created_at
* @property Carbon $updated_at
*/
Expand All @@ -33,6 +37,7 @@ class Setting extends Model
"teacher_titles",
"university_name",
"department_name",
"schedule_link",
"primary_color",
"secondary_color",
"logo",
Expand Down
1 change: 1 addition & 0 deletions app/Observers/SettingObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public function __construct(
public function saved(): void
{
$this->cache->forget("pageTitle");
$this->cache->forget("scheduleLink");
}
}
1 change: 1 addition & 0 deletions database/factories/SettingFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function definition(): array
"teacher_email" => fake()->email,
"department_name" => "Zakład Informatyki, Wydział Nauk Technicznych i Ekonomicznych",
"university_name" => "Collegium Witelona Uczelnia Państwowa",
"schedule_link" => fake()->url(),
"primary_color" => "#000000",
"secondary_color" => "#ffffff",
];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
public function up(): void
{
Schema::table("settings", function (Blueprint $table): void {
$table->string("schedule_link")->nullable()->default(null);
});
}

public function down(): void
{
Schema::table("settings", function (Blueprint $table): void {
$table->dropColumn("schedule_link");
});
}
};
1 change: 1 addition & 0 deletions lang/pl/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
"teacher_titles" => "tytuły/stopnie naukowe nauczyciela",
"university_name" => "nazwa uczelni",
"department_name" => "nazwa wydziału",
"schedule_link" => "link do planu zajęć",
"title" => "tytuł",
"content" => "treść",
"value" => "opis",
Expand Down
10 changes: 9 additions & 1 deletion resources/js/Layouts/PublicLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { ref } from 'vue'
import { Link } from '@inertiajs/inertia-vue3'
import { Dialog, DialogPanel } from '@headlessui/vue'
import { Bars3Icon, EllipsisHorizontalIcon, XMarkIcon } from '@heroicons/vue/24/outline'
import { Bars3Icon, EllipsisHorizontalIcon, XMarkIcon, ArrowTopRightOnSquareIcon } from '@heroicons/vue/24/outline'
const navigation = [
{ name: 'Strona główna', href: '/' },
Expand Down Expand Up @@ -30,6 +30,10 @@ const mobileMenuOpen = ref(false)
<Link v-for="item in navigation" :key="item.name" :href="item.href" class="text-sm font-semibold leading-6 text-gray-900">
{{ item.name }}
</Link>
<a v-if="$page.props.settings.scheduleLink" :href="$page.props.settings.scheduleLink" target="_blank" class="flex items-center gap-2 text-sm font-semibold leading-6 text-gray-900">
Plan zajęć
<ArrowTopRightOnSquareIcon class="size-5" />
</a>
</div>
<div class="hidden lg:flex lg:flex-1 lg:justify-end">
<Link v-if="$page.props.auth.user" href="/dashboard" class="text-sm font-semibold leading-6 text-gray-900">
Expand Down Expand Up @@ -57,6 +61,10 @@ const mobileMenuOpen = ref(false)
<Link v-for="item in navigation" :key="item.name" :href="item.href" class="-mx-3 block rounded-lg px-3 py-2 text-base font-semibold leading-7 text-gray-900 hover:bg-gray-50">
{{ item.name }}
</Link>
<a v-if="$page.props.settings.scheduleLink" :href="$page.props.settings.scheduleLink" target="_blank" class="-mx-3 flex items-center gap-2 rounded-lg px-3 py-2 text-base font-semibold leading-7 text-gray-900 hover:bg-gray-50">
Plan zajęć
<ArrowTopRightOnSquareIcon class="size-5" />
</a>
</div>
<div class="py-6">
<Link v-if="$page.props.auth.user" href="/dashboard" class="-mx-3 block rounded-lg px-3 py-2.5 text-base font-semibold leading-7 text-gray-900 hover:bg-gray-50">
Expand Down
8 changes: 8 additions & 0 deletions resources/js/Pages/Dashboard/Setting/Edit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const form = useForm({
teacher_titles: props.settings.teacher_titles,
university_name: props.settings.university_name,
department_name: props.settings.department_name,
schedule_link: props.settings.schedule_link,
primary_color: props.settings.primary_color,
secondary_color: props.settings.secondary_color,
logo: null,
Expand Down Expand Up @@ -100,6 +101,13 @@ function onFileSelected(event) {
<TextInput id="department_name" v-model="form.department_name" :error="form.errors.department_name" autocomplete="off" />
<FormError :error="form.errors.department_name" />
</FormGroup>
<FormGroup>
<FormLabel for="department_name">
Link do planu zajęć
</FormLabel>
<TextInput id="schedule_link" v-model="form.schedule_link" :error="form.errors.schedule_link" autocomplete="off" />
<FormError :error="form.errors.schedule_link" />
</FormGroup>
<FormGroup>
<FormLabel for="primary_color">
Kolor główny
Expand Down
3 changes: 0 additions & 3 deletions tests/CreatesApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

trait CreatesApplication
{
/**
* Creates the application.
*/
public function createApplication(): Application
{
$app = require __DIR__ . "/../bootstrap/app.php";
Expand Down
60 changes: 60 additions & 0 deletions tests/Feature/SettingsScheduleLinkTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Inertia\Testing\AssertableInertia;
use Keating\Models\Setting;
use Keating\Models\User;
use Tests\TestCase;

class SettingsScheduleLinkTest extends TestCase
{
use RefreshDatabase;

protected function setUp(): void
{
parent::setUp();

$this->user = User::factory()->create();
$this->actingAs($this->user);
}

public function testMainPageWhenNoScheduleLinkInSetting(): void
{
Setting::factory()->create([
"teacher_name" => "Ty Doe",
"teacher_email" => "ty.doe@exmple.com",
"teacher_titles" => "dr inż.",
"university_name" => "CWUP",
"department_name" => "IT department",
"schedule_link" => null,
"primary_color" => "#000000",
"secondary_color" => "#ffffff",
]);

$this->get("/")->assertInertia(function (AssertableInertia $page): void {
$page->has("settings")->where("settings.scheduleLink", null);
});
}

public function testMainPageWhenScheduleLinkInSetting(): void
{
Setting::factory()->create([
"teacher_name" => "Ty Doe",
"teacher_email" => "ty.doe@exmple.com",
"teacher_titles" => "dr inż.",
"university_name" => "CWUP",
"department_name" => "IT department",
"schedule_link" => "https://google.com/",
"primary_color" => "#000000",
"secondary_color" => "#ffffff",
]);

$this->get("/")->assertInertia(function (AssertableInertia $page): void {
$page->has("settings")->where("settings.scheduleLink", "https://google.com/");
});
}
}
17 changes: 17 additions & 0 deletions tests/Feature/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protected function setUp(): void
"teacher_titles" => "dr inż.",
"university_name" => "CWUP",
"department_name" => "IT department",
"schedule_link" => "https://google.com/",
"primary_color" => "#000000",
"secondary_color" => "#ffffff",
]);
Expand All @@ -41,6 +42,7 @@ public function testSettingsCanBeUpdated(): void
"teacher_titles" => "dr inż.",
"university_name" => "CWUP",
"department_name" => "IT department",
"schedule_link" => "https://google.com/",
"primary_color" => "#000000",
"secondary_color" => "#ffffff",
"logo" => null,
Expand All @@ -52,6 +54,7 @@ public function testSettingsCanBeUpdated(): void
"teacher_titles" => "dr",
"university_name" => "SWPS",
"department_name" => "Psychology department",
"schedule_link" => "https://google.com/",
"primary_color" => "#11ffff",
"secondary_color" => "#110000",
])->assertSessionHasNoErrors();
Expand All @@ -62,6 +65,7 @@ public function testSettingsCanBeUpdated(): void
"teacher_titles" => "dr",
"university_name" => "SWPS",
"department_name" => "Psychology department",
"schedule_link" => "https://google.com/",
"primary_color" => "#11ffff",
"secondary_color" => "#110000",
"logo" => null,
Expand All @@ -76,6 +80,7 @@ public function testSettingsCannotBeUpdatedWithInvalidData(): void
"teacher_titles" => "dr inż.",
"university_name" => "CWUP",
"department_name" => "IT department",
"schedule_link" => "https://google.com/",
"primary_color" => "#000000",
"secondary_color" => "#ffffff",
"logo" => null,
Expand All @@ -87,6 +92,7 @@ public function testSettingsCannotBeUpdatedWithInvalidData(): void
"teacher_titles" => Str::random(256),
"university_name" => Str::random(256),
"department_name" => Str::random(256),
"schedule_link" => "test",
"primary_color" => "000000",
"secondary_color" => "ffffff",
])->assertSessionHasErrors()
Expand All @@ -96,6 +102,7 @@ public function testSettingsCannotBeUpdatedWithInvalidData(): void
"teacher_titles",
"university_name",
"department_name",
"schedule_link",
"primary_color",
"secondary_color",
]);
Expand All @@ -106,6 +113,7 @@ public function testSettingsCannotBeUpdatedWithInvalidData(): void
"teacher_titles" => "dr inż.",
"university_name" => "CWUP",
"department_name" => "IT department",
"schedule_link" => "https://google.com/",
"primary_color" => "#000000",
"secondary_color" => "#ffffff",
"logo" => null,
Expand All @@ -120,6 +128,7 @@ public function testSettingsCannotBeUpdatedWithEmptyData(): void
"teacher_titles" => "dr inż.",
"university_name" => "CWUP",
"department_name" => "IT department",
"schedule_link" => "https://google.com/",
"primary_color" => "#000000",
"secondary_color" => "#ffffff",
"logo" => null,
Expand Down Expand Up @@ -150,6 +159,7 @@ public function testSettingsCannotBeUpdatedWithEmptyData(): void
"teacher_titles" => "dr inż.",
"university_name" => "CWUP",
"department_name" => "IT department",
"schedule_link" => "https://google.com/",
"primary_color" => "#000000",
"secondary_color" => "#ffffff",
"logo" => null,
Expand All @@ -165,6 +175,7 @@ public function testLogoCanBeUploadedAndRemoved(): void
"teacher_titles" => "dr inż.",
"university_name" => "CWUP",
"department_name" => "IT department",
"schedule_link" => "https://google.com/",
"primary_color" => "#000000",
"secondary_color" => "#ffffff",
"logo" => null,
Expand All @@ -177,6 +188,7 @@ public function testLogoCanBeUploadedAndRemoved(): void
"teacher_titles" => "dr",
"university_name" => "SWPS",
"department_name" => "Psychology department",
"schedule_link" => "https://google.com/",
"primary_color" => "#11ffff",
"secondary_color" => "#110000",
"logo" => $logo,
Expand All @@ -188,6 +200,7 @@ public function testLogoCanBeUploadedAndRemoved(): void
"teacher_titles" => "dr",
"university_name" => "SWPS",
"department_name" => "Psychology department",
"schedule_link" => "https://google.com/",
"primary_color" => "#11ffff",
"secondary_color" => "#110000",
"logo" => "logo/logo.png",
Expand All @@ -203,6 +216,7 @@ public function testLogoCanBeUploadedAndRemoved(): void
"teacher_titles" => "dr",
"university_name" => "SWPS",
"department_name" => "Psychology department",
"schedule_link" => "https://google.com/",
"primary_color" => "#11ffff",
"secondary_color" => "#110000",
"logo" => null,
Expand All @@ -219,6 +233,7 @@ public function testSettingsCannotBeUpdatedWithTooBigLogoFile(): void
"teacher_titles" => "dr inż.",
"university_name" => "CWUP",
"department_name" => "IT department",
"schedule_link" => "https://google.com/",
"primary_color" => "#000000",
"secondary_color" => "#ffffff",
"logo" => null,
Expand All @@ -231,6 +246,7 @@ public function testSettingsCannotBeUpdatedWithTooBigLogoFile(): void
"teacher_titles" => "dr",
"university_name" => "SWPS",
"department_name" => "Psychology department",
"schedule_link" => "https://google.com/",
"primary_color" => "#11ffff",
"secondary_color" => "#110000",
"logo" => $logo,
Expand All @@ -245,6 +261,7 @@ public function testSettingsCannotBeUpdatedWithTooBigLogoFile(): void
"teacher_titles" => "dr inż.",
"university_name" => "CWUP",
"department_name" => "IT department",
"schedule_link" => "https://google.com/",
"primary_color" => "#000000",
"secondary_color" => "#ffffff",
"logo" => null,
Expand Down

0 comments on commit 53ae281

Please sign in to comment.