Skip to content

Commit ed3fd06

Browse files
committed
feat(node): implement nested node routes and dynamic data rendering in Vue
1 parent db45b8d commit ed3fd06

4 files changed

Lines changed: 327 additions & 0 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Models\Node;
6+
use App\Models\Resource;
7+
use App\Models\Subject;
8+
use Illuminate\Http\Request;
9+
use Inertia\Inertia;
10+
11+
class NodeController extends Controller
12+
{
13+
//
14+
public function show(Subject $subject, $path)
15+
{
16+
$slugs = explode('/', trim($path, '/'));
17+
18+
$node = null;
19+
$parent = null;
20+
21+
foreach ($slugs as $slug) {
22+
23+
$query = Node::where('subject_id', $subject->id)
24+
->where('slug', $slug);
25+
26+
if ($parent) {
27+
$query->where('parent_id', $parent->id);
28+
} else {
29+
$query->whereNull('parent_id');
30+
}
31+
32+
$node = $query->firstOrFail();
33+
$parent = $node;
34+
}
35+
36+
$nodes = Node::where('parent_id', $node->id)
37+
->orderBy('sort_order')
38+
->withCount('children')
39+
->get(['id', 'name', 'slug']);
40+
41+
$resources = $node->resources()->get();
42+
return Inertia::render('Node', [
43+
'subject' => $subject,
44+
'nodes' => $nodes,
45+
'breadcrumb' => $this->buildBreadcrumb($node),
46+
'resources' => $resources,
47+
]);
48+
}
49+
50+
private function buildBreadcrumb($node)
51+
{
52+
$breadcrumb = [];
53+
54+
while ($node) {
55+
array_unshift($breadcrumb, [
56+
'name' => $node->name,
57+
'slug' => $node->slug,
58+
]);
59+
60+
$node = $node->parent;
61+
}
62+
63+
return $breadcrumb;
64+
}
65+
}

‎app/Http/Controllers/SubjectController.php‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Models\Node;
6+
use App\Models\Resource;
57
use App\Models\Subject;
68
use Inertia\Inertia;
79

@@ -15,4 +17,20 @@ public function index()
1517
'subjects' => $subjects
1618
]);
1719
}
20+
21+
public function show(Subject $subject)
22+
{
23+
$nodes = Node::where('subject_id', $subject->id)
24+
->whereNull('parent_id')
25+
->orderBy('sort_order')
26+
->withCount('children')
27+
->get(['id', 'name', 'slug']);
28+
29+
return Inertia::render('Node', [
30+
'subject' => $subject,
31+
'nodes' => $nodes,
32+
'breadcrumb' => [],
33+
'resources' => [],
34+
]);
35+
}
1836
}

‎resources/js/pages/Node.vue‎

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
<script setup>
2+
import { computed } from 'vue';
3+
import { Link } from '@inertiajs/vue3';
4+
import {
5+
Home as HomeIcon,
6+
ChevronRight as ChevronRightIcon,
7+
Folder as FolderIcon,
8+
FileImage as ImageIcon,
9+
File as GenericFileIcon,
10+
} from 'lucide-vue-next';
11+
12+
const props = defineProps({
13+
subject: Object,
14+
nodes: Array,
15+
breadcrumb: Array,
16+
resources: Array ,
17+
});
18+
19+
20+
const breadcrumbs = computed(() => {
21+
const subjectPath = props.subject?.slug ? `/${props.subject.slug}` : '';
22+
let path = '';
23+
24+
return (props.breadcrumb || []).map((crumb) => {
25+
path += `/${crumb.slug}`;
26+
27+
return {
28+
name: crumb.name,
29+
link: `${subjectPath}${path}`,
30+
};
31+
});
32+
});
33+
34+
const totalItemsCount = computed(() => {
35+
const nodesCount = props.nodes?.length || 0;
36+
const resourcesCount = props.resources.length;
37+
return nodesCount + resourcesCount;
38+
});
39+
40+
41+
</script>
42+
43+
<template>
44+
<div
45+
class="mx-auto flex min-h-[calc(100vh-10rem)] w-full max-w-4xl flex-col px-4 py-8 font-sans text-slate-700 selection:bg-indigo-50 sm:px-6 md:py-12"
46+
>
47+
<nav
48+
class="no-scrollbar mb-6 flex items-center space-x-2 overflow-x-auto text-xs font-semibold tracking-wider whitespace-nowrap text-slate-400 uppercase"
49+
>
50+
<Link
51+
href="/"
52+
class="mr-0.5 flex items-center rounded-lg p-1 transition-colors hover:bg-slate-100/80 hover:text-indigo-600"
53+
>
54+
<HomeIcon class="h-4 w-4 stroke-[2.2]" />
55+
</Link>
56+
57+
<ChevronRightIcon
58+
class="h-3.5 w-3.5 shrink-0 stroke-[2.5] text-slate-300"
59+
/>
60+
61+
<Link
62+
:href="`/${props.subject?.slug || ''}`"
63+
class="px-1 whitespace-nowrap transition-colors hover:text-indigo-600"
64+
>
65+
{{ props.subject?.name }}
66+
</Link>
67+
68+
<template
69+
v-for="(crumb, index) in breadcrumbs"
70+
:key="`${crumb.name}-${index}`"
71+
>
72+
<ChevronRightIcon
73+
class="h-3.5 w-3.5 shrink-0 stroke-[2.5] text-slate-300"
74+
/>
75+
76+
<Link
77+
:href="crumb.link"
78+
class="px-1 whitespace-nowrap transition-colors hover:text-indigo-600"
79+
>
80+
{{ crumb.name }}
81+
</Link>
82+
</template>
83+
</nav>
84+
85+
<header class="mb-8">
86+
<h1
87+
class="text-3xl font-black tracking-tight text-slate-950 sm:text-4xl"
88+
>
89+
{{ props.subject?.name }}
90+
</h1>
91+
<p class="mt-1.5 text-sm font-semibold text-slate-400">
92+
{{ totalItemsCount }} Items Total
93+
</p>
94+
</header>
95+
96+
<div
97+
class="flex flex-1 flex-col overflow-hidden rounded-xl border border-slate-200 bg-white shadow-sm"
98+
>
99+
<div
100+
class="flex items-center justify-between border-b border-slate-100 bg-slate-50 px-5 py-3.5 text-xs font-bold tracking-wider text-slate-400 uppercase sm:px-6"
101+
>
102+
<span>Name</span>
103+
<span class="hidden sm:inline">Type</span>
104+
</div>
105+
106+
<div class="flex-1 divide-y divide-slate-100">
107+
<template v-if="totalItemsCount > 0">
108+
<Link
109+
v-for="node in props.nodes"
110+
:key="`node-${node.id}`"
111+
:href="`${$page.url}/${node.slug}`"
112+
class="group relative flex cursor-pointer touch-manipulation items-center justify-between bg-white px-5 py-4.5 transition-all duration-200 hover:bg-slate-50/40 active:scale-[0.995] sm:px-6 sm:active:scale-100"
113+
>
114+
<div class="flex min-w-0 items-center gap-4">
115+
<div
116+
class="flex h-11 w-11 shrink-0 items-center justify-center rounded-xl border border-slate-200/40 bg-slate-100 text-slate-500 transition-colors duration-200 group-hover:border-indigo-100 group-hover:bg-indigo-50 group-hover:text-indigo-600"
117+
>
118+
<FolderIcon class="h-5 w-5 stroke-[2.2]" />
119+
</div>
120+
121+
<div class="min-w-0">
122+
<span
123+
class="block truncate text-base font-bold text-slate-900 transition-colors group-hover:text-indigo-600"
124+
>
125+
{{ node.slug }}
126+
</span>
127+
<span
128+
class="mt-0.5 inline-block text-xs font-semibold text-slate-400 sm:hidden"
129+
>
130+
{{ node.children_count || 0 }} Materials
131+
</span>
132+
</div>
133+
</div>
134+
135+
<div class="flex shrink-0 items-center gap-4 pl-3">
136+
<span
137+
class="hidden rounded-md border border-slate-200/60 bg-slate-50 px-2.5 py-1 text-xs font-bold text-slate-500 transition-colors group-hover:border-indigo-100/80 group-hover:bg-indigo-50/60 group-hover:text-indigo-600 sm:inline-block"
138+
>
139+
{{ node.children_count || 0 }} Materials
140+
</span>
141+
142+
<svg
143+
xmlns="http://www.w3.org/2000/svg"
144+
class="h-4 w-4 text-slate-400 transition-transform duration-200 group-hover:translate-x-1 group-hover:text-indigo-600"
145+
viewBox="0 0 20 20"
146+
fill="currentColor"
147+
>
148+
<path
149+
fill-rule="evenodd"
150+
d="M10.293 3.293a1 1 0 011.414 0l6 6a1 1 0 010 1.414l-6 6a1 1 0 01-1.414-1.414L14.586 11H3a1 1 0 110-2h11.586l-4.293-4.293a1 1 0 010-1.414z"
151+
clip-rule="evenodd"
152+
/>
153+
</svg>
154+
</div>
155+
</Link>
156+
157+
<Link
158+
v-for="resource in resources"
159+
:key="`resource-${resource.id}`"
160+
:href="`/resources/${resource.id}`"
161+
target="_blank"
162+
class="group relative flex cursor-pointer touch-manipulation items-center justify-between bg-white px-5 py-4.5 transition-all duration-200 hover:bg-slate-50/40 active:scale-[0.995] sm:px-6 sm:active:scale-100"
163+
>
164+
<div class="flex min-w-0 items-center gap-4">
165+
<div
166+
class="flex h-11 w-11 shrink-0 items-center justify-center rounded-xl border border-amber-200/40 bg-amber-50 text-amber-600 transition-colors duration-200 group-hover:border-amber-200 group-hover:bg-amber-100/70"
167+
>
168+
<ImageIcon
169+
v-if="
170+
resource.resource_type == 'image'
171+
"
172+
class="h-5 w-5 stroke-[2.2]"
173+
/>
174+
<GenericFileIcon
175+
v-else
176+
class="h-5 w-5 stroke-[2.2]"
177+
/>
178+
</div>
179+
180+
<div class="min-w-0">
181+
<span
182+
class="block truncate text-base font-bold text-slate-900 transition-colors group-hover:text-amber-700"
183+
>
184+
{{ resource.title }}
185+
</span>
186+
<span
187+
class="mt-0.5 inline-block text-xs font-semibold text-amber-600 sm:hidden"
188+
>
189+
{{ resource.resource_type }}
190+
</span>
191+
</div>
192+
</div>
193+
194+
<div class="flex shrink-0 items-center gap-4 pl-3">
195+
<span
196+
class="hidden rounded-md border border-amber-200/60 bg-amber-50/60 px-2.5 py-1 text-xs font-bold text-amber-600 sm:inline-block"
197+
>
198+
{{ resource.resource_type }}
199+
</span>
200+
201+
<svg
202+
xmlns="http://www.w3.org/2000/svg"
203+
class="h-4 w-4 text-slate-400 transition-transform duration-200 group-hover:translate-x-0.5 group-hover:-translate-y-0.5 group-hover:text-amber-600"
204+
fill="none"
205+
viewBox="0 0 24 24"
206+
stroke="currentColor"
207+
stroke-width="2.5"
208+
>
209+
<path
210+
stroke-linecap="round"
211+
stroke-linejoin="round"
212+
d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14"
213+
/>
214+
</svg>
215+
</div>
216+
</Link>
217+
</template>
218+
219+
<div
220+
v-else
221+
class="flex flex-col items-center justify-center px-4 py-14 text-center"
222+
>
223+
<div
224+
class="flex h-12 w-12 items-center justify-center rounded-2xl border border-slate-100 bg-slate-50 text-slate-400"
225+
>
226+
<FolderIcon class="h-6 w-6 stroke-[1.8]" />
227+
</div>
228+
<h3 class="mt-4 text-sm font-bold text-slate-900">
229+
No items available
230+
</h3>
231+
<p
232+
class="mt-1 max-w-xs text-xs leading-relaxed font-medium text-slate-400"
233+
>
234+
There are currently no files or sub-folders located
235+
inside this section.
236+
</p>
237+
</div>
238+
</div>
239+
</div>
240+
</div>
241+
</template>

‎routes/web.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22

3+
use App\Http\Controllers\NodeController;
34
use App\Http\Controllers\SubjectController;
45
use Illuminate\Support\Facades\Route;
56

67
Route::get('/', [SubjectController::class, 'index']);
8+
Route::get('/{subject:slug}', [SubjectController::class, 'show']);
9+
Route::get('/{subject:slug}/{path}', [NodeController::class, 'show'])->where('path', '.*');

0 commit comments

Comments
 (0)