Skip to content

Commit 4bf97a0

Browse files
committed
feat: add notice feature, controlled by super admin
1 parent cf6dd55 commit 4bf97a0

9 files changed

Lines changed: 469 additions & 1 deletion

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Admin;
4+
5+
use App\Http\Controllers\Controller;
6+
use App\Models\Notice;
7+
use Illuminate\Http\Request;
8+
use Inertia\Inertia;
9+
10+
class NoticeController extends Controller
11+
{
12+
public function edit()
13+
{
14+
return Inertia::render('admin/NoticeEdit', [
15+
'notice' => Notice::singleton(),
16+
]);
17+
}
18+
19+
public function update(Request $request)
20+
{
21+
$data = $request->validate([
22+
'title' => ['required', 'string', 'max:255'],
23+
'message' => ['nullable', 'string'],
24+
'image' => ['nullable', 'string', 'max:500'],
25+
'show_button' => ['required', 'boolean'],
26+
'button_title' => ['nullable', 'required_if:show_button,true', 'string', 'max:100'],
27+
'button_link' => ['nullable', 'required_if:show_button,true', 'string', 'max:500'],
28+
'is_active' => ['required', 'boolean'],
29+
]);
30+
31+
// Clear button fields when show_button is off
32+
if (!$data['show_button']) {
33+
$data['button_title'] = null;
34+
$data['button_link'] = null;
35+
}
36+
37+
Notice::singleton()->update($data);
38+
39+
return redirect()->route('admin.notice.edit')->with('success', 'Notice updated.');
40+
}
41+
}

‎app/Http/Controllers/SubjectController.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Http\Controllers;
44

55
use App\Models\Node;
6+
use App\Models\Notice;
67
use App\Models\Resource;
78
use App\Models\Subject;
89
use Inertia\Inertia;
@@ -12,12 +13,14 @@ class SubjectController extends Controller
1213
public function index()
1314
{
1415
$subjects = Subject::orderBy('sort_order', 'asc')->withCount('nodes')->get();
16+
$notice = Notice::activeForDisplay();
1517

1618
return Inertia::render('Home', [
1719
'subjects' => $subjects,
1820
'subjectCount' => $subjects->count(),
1921
'resourceCount' => Resource::count(),
2022
'siteTraffic' => 500,
23+
'notice' => $notice,
2124
]);
2225
}
2326

@@ -28,7 +31,7 @@ public function show(Subject $subject)
2831
->orderBy('sort_order')
2932
->withCount(['children', 'resources'])
3033
->get(['id', 'name', 'slug']);
31-
34+
3235
return Inertia::render('Node', [
3336
'subject' => $subject,
3437
'nodes' => $nodes,

‎app/Models/Notice.php‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Notice extends Model
8+
{
9+
protected $casts = ['show_button' => 'boolean', 'is_active' => 'boolean'];
10+
protected $guarded = [];
11+
public static function singleton(): self
12+
{
13+
return static::firstOrCreate([], [
14+
'title' => null,
15+
'message' => null,
16+
'image' => null,
17+
'show_button' => false,
18+
'button_title' => null,
19+
'button_link' => null,
20+
'is_active' => false,
21+
]);
22+
}
23+
24+
public static function activeForDisplay(): ?self
25+
{
26+
return static::where('is_active', true)->first();
27+
}
28+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::create('notices', function (Blueprint $table) {
15+
$table->id();
16+
$table->string('title', 255)->nullable();
17+
$table->text('message')->nullable();
18+
$table->string('image', 500)->nullable();
19+
$table->boolean('show_button')->default(false);
20+
$table->string('button_title', 100)->nullable();
21+
$table->string('button_link', 500)->nullable();
22+
$table->boolean('is_active')->default(false);
23+
$table->timestamps();
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*/
30+
public function down(): void
31+
{
32+
Schema::dropIfExists('notices');
33+
}
34+
};
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<script setup lang="ts">
2+
import { X } from 'lucide-vue-next';
3+
import { onMounted, ref, watch } from 'vue';
4+
5+
const props = defineProps({
6+
notice: { type: Object, default: null },
7+
});
8+
9+
const isVisible = ref(false);
10+
11+
const getStorageKey = () => {
12+
return props.notice?.updated_at
13+
? `session_notice_${props.notice.updated_at}`
14+
: 'session_notice_dismissed';
15+
};
16+
17+
const checkVisibility = () => {
18+
if (!props.notice) return false;
19+
20+
21+
return sessionStorage.getItem(getStorageKey()) !== 'true';
22+
};
23+
24+
25+
onMounted(() => {
26+
isVisible.value = checkVisibility();
27+
});
28+
29+
30+
watch(() => props.notice, () => {
31+
isVisible.value = checkVisibility();
32+
});
33+
34+
const close = () => {
35+
if (props.notice) {
36+
sessionStorage.setItem(getStorageKey(), 'true');
37+
}
38+
isVisible.value = false;
39+
};
40+
</script>
41+
42+
<template>
43+
<Teleport to="body">
44+
<Transition
45+
enter-active-class="transition duration-300 ease-out"
46+
enter-from-class="opacity-0 scale-95"
47+
enter-to-class="opacity-100 scale-100"
48+
leave-active-class="transition duration-200 ease-in"
49+
leave-from-class="opacity-100 scale-100"
50+
leave-to-class="opacity-0 scale-95"
51+
>
52+
<div
53+
v-if="isVisible && notice"
54+
class="fixed inset-0 z-50 flex items-center justify-center p-4"
55+
>
56+
<div
57+
class="absolute inset-0 bg-slate-950/20 backdrop-blur-sm"
58+
@click="close"
59+
></div>
60+
61+
<div
62+
class="relative w-full max-w-md overflow-hidden rounded-3xl bg-white shadow-xl ring-1 ring-slate-900/5"
63+
role="alertdialog"
64+
>
65+
<button
66+
@click="close"
67+
class="absolute top-4 right-4 z-10 rounded-full p-2 transition bg-slate-100 text-slate-600"
68+
aria-label="Close notice"
69+
>
70+
<X class="h-5 w-5" />
71+
</button>
72+
73+
<img
74+
v-if="notice.image"
75+
:src="notice.image"
76+
class="h-40 w-full object-cover"
77+
alt=""
78+
/>
79+
80+
<div class="p-6">
81+
<h2 v-if="notice.title" class="text-lg font-semibold text-slate-900">
82+
{{ notice.title }}
83+
</h2>
84+
<p v-if="notice.message" class="mt-2 text-sm text-slate-600 leading-relaxed whitespace-pre-line">
85+
{{ notice.message }}
86+
</p>
87+
88+
<div class="mt-6 flex gap-3">
89+
<a
90+
v-if="notice.show_button && notice.button_link"
91+
:href="notice.button_link"
92+
target="_blank"
93+
rel="noopener noreferrer"
94+
class="flex-1 rounded-xl bg-slate-900 px-4 py-2.5 text-center text-sm font-medium text-white transition hover:bg-slate-800"
95+
>
96+
{{ notice.button_title || 'Learn more' }}
97+
</a>
98+
99+
</div>
100+
</div>
101+
</div>
102+
</div>
103+
</Transition>
104+
</Teleport>
105+
</template>

‎resources/js/layouts/AdminLayout.vue‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
LayoutDashboard,
44
Users,
55
BookOpen,
6+
Bell,
67
} from 'lucide-vue-next';
78
import { ref } from 'vue';
89
import DesktopSidebar from '@/components/admin/DesktopSidebar.vue';
@@ -15,6 +16,7 @@ const isMobileSidebarOpen = ref(false);
1516
const navigation = [
1617
{ name: 'Dashboard', to: '/admin', icon: LayoutDashboard },
1718
{ name: 'Manage Contents', to: '/admin/subjects', icon: BookOpen },
19+
{ name: 'Site Notice', to: '/admin/notice', icon: Bell },
1820
{ name: 'Users', to: '/admin/users', icon: Users },
1921
];
2022

‎resources/js/pages/Home.vue‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script setup lang="ts">
22
import { ref, computed } from 'vue';
33
import HomeHeader from '@/components/HomeHeader.vue';
4+
import NoticeDialog from '@/components/NoticeDialog.vue';
45
import RepositoryStas from '@/components/RepositoryStas.vue';
56
import SubjectCard from '@/components/SubjectCard.vue';
67
@@ -9,6 +10,7 @@ const props = defineProps({
910
subjectCount : Number,
1011
resourceCount : Number,
1112
siteTraffic : Number,
13+
notice: Object,
1214
});
1315
1416
const subjects = props.subjects;
@@ -22,6 +24,8 @@ const filteredSubjects = computed(() => {
2224
</script>
2325

2426
<template>
27+
<NoticeDialog v-if="notice" :notice="notice" />
28+
2529
<HomeHeader v-model="searchQuery" />
2630

2731
<main class="mx-auto max-w-7xl px-4 pb-20 sm:px-6">

0 commit comments

Comments
 (0)