Skip to content

Commit f0ad19d

Browse files
committed
the actual commit
1 parent 7be31fd commit f0ad19d

File tree

9 files changed

+6643
-32
lines changed

9 files changed

+6643
-32
lines changed

package-lock.json

Lines changed: 6522 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/data/dinosaurs.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"dinosaurs": [
3+
{
4+
"name": "Tyrannosaurus Rex",
5+
"description": "A massive carnivorous dinosaur with powerful jaws and tiny arms."
6+
},
7+
{
8+
"name": "Brachiosaurus",
9+
"description": "A huge herbivorous dinosaur with a very long neck."
10+
},
11+
{
12+
"name": "Velociraptor",
13+
"description": "A small but fierce predator that hunted in packs."
14+
}
15+
]
16+
}

src/routes/[name]/index.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { component$ } from "@builder.io/qwik";
2+
import { Link, routeLoader$ } from "@builder.io/qwik-city";
3+
import type { Dino } from "~/types";
4+
import { loadDinoData } from "~/utils/loadData";
5+
6+
export const useDinosaurDetails = routeLoader$(({ params }) => {
7+
const dinosaurs = loadDinoData();
8+
const dinosaur = dinosaurs.find(
9+
(dino) => dino.name.toLowerCase() === params.name.toLowerCase()
10+
);
11+
12+
if (!dinosaur) {
13+
throw new Error("Dinosaur not found");
14+
}
15+
16+
return dinosaur;
17+
});
18+
19+
export default component$(() => {
20+
const dinosaurSignal = useDinosaurDetails();
21+
22+
return (
23+
<div class="container mx-auto p-4">
24+
<h1 class="text-3xl font-bold mb-4">{dinosaurSignal.value.name}</h1>
25+
<p class="mb-4">{dinosaurSignal.value.description}</p>
26+
<Link href="/" class="text-blue-600 hover:underline">
27+
Back to all dinosaurs
28+
</Link>
29+
</div>
30+
);
31+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { RequestHandler } from "@builder.io/qwik-city";
2+
import { loadDinoData } from "~/utils/loadData";
3+
4+
export const onGet: RequestHandler = async ({ params, json }) => {
5+
const { name } = params;
6+
const dinosaurs = loadDinoData();
7+
8+
if (!name) {
9+
json(400, { error: "No dinosaur name provided." });
10+
return;
11+
}
12+
13+
const dinosaur = dinosaurs.find(
14+
(dino) => dino.name.toLowerCase() === name.toLowerCase()
15+
);
16+
17+
if (!dinosaur) {
18+
json(404, { error: "No dinosaur found." });
19+
return;
20+
}
21+
22+
json(200, dinosaur);
23+
};

src/routes/api/dinosaurs/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { RequestHandler } from "@builder.io/qwik-city";
2+
import { loadDinoData } from "~/utils/loadData";
3+
4+
export const onGet: RequestHandler = async ({ json }) => {
5+
const dinosaurs = loadDinoData();
6+
json(200, dinosaurs);
7+
};

src/routes/index.tsx

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
import { component$ } from "@builder.io/qwik";
2-
import type { DocumentHead } from "@builder.io/qwik-city";
2+
import { Link, routeLoader$ } from "@builder.io/qwik-city";
3+
import type { Dino } from "~/types";
4+
import { loadDinoData } from "~/utils/loadData";
5+
6+
export const useDinosaurs = routeLoader$(() => {
7+
return loadDinoData();
8+
});
39

410
export default component$(() => {
11+
const dinosaursSignal = useDinosaurs();
12+
513
return (
6-
<>
7-
<h1>Hi 👋</h1>
8-
<div>
9-
Can't wait to see what you build with qwik!
10-
<br />
11-
Happy coding.
12-
</div>
13-
</>
14+
<div class="container mx-auto p-4">
15+
<h1 class="text-3xl font-bold mb-4">Welcome to the Dinosaur app</h1>
16+
<p class="mb-4">Click on a dinosaur below to learn more.</p>
17+
<ul class="space-y-2">
18+
{dinosaursSignal.value.map((dinosaur) => (
19+
<li key={dinosaur.name}>
20+
<Link
21+
href={`/${dinosaur.name.toLowerCase()}`}
22+
class="text-blue-600 hover:underline"
23+
>
24+
{dinosaur.name}
25+
</Link>
26+
</li>
27+
))}
28+
</ul>
29+
</div>
1430
);
1531
});
16-
17-
export const head: DocumentHead = {
18-
title: "Welcome to Qwik",
19-
meta: [
20-
{
21-
name: "description",
22-
content: "Qwik site description",
23-
},
24-
],
25-
};

src/routes/layout.tsx

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
import { component$, Slot } from "@builder.io/qwik";
2-
import type { RequestHandler } from "@builder.io/qwik-city";
3-
4-
export const onGet: RequestHandler = async ({ cacheControl }) => {
5-
// Control caching for this request for best performance and to reduce hosting costs:
6-
// https://qwik.dev/docs/caching/
7-
cacheControl({
8-
// Always serve a cached response by default, up to a week stale
9-
staleWhileRevalidate: 60 * 60 * 24 * 7,
10-
// Max once every 5 seconds, revalidate on the server to get a fresh version of this page
11-
maxAge: 5,
12-
});
13-
};
142

153
export default component$(() => {
16-
return <Slot />;
4+
return (
5+
<>
6+
<main>
7+
<Slot />
8+
</main>
9+
</>
10+
);
1711
});

src/types.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export type Dino = {
2+
name: string;
3+
description: string;
4+
};

src/utils/loadData.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
4+
export function loadDinoData() {
5+
const filePath = path.join(process.cwd(), "src", "data", "dinosaurs.json");
6+
const fileContent = fs.readFileSync(filePath, "utf-8");
7+
return JSON.parse(fileContent).dinosaurs;
8+
}

0 commit comments

Comments
 (0)