17
loading...
This website collects cookies to deliver better user experience
Note: Important information regarding the current API state
$
(dollar sign).$show.astro
.---
section) is where we can define the behavior for this collection of pages.---
const { collection } = Astro.props;
---
createCollection
function, which can create a collection dynamically.--------
const { collection } = Astro.props;
export async function createCollection() {
// TODO
}
--------
export async function createCollection() {
const remoteData = await fetch('https://kitsu.io/api/edge/anime?sort=-averageRating');
const remoteJson = await remoteData.json();
const allData = remoteJson.data;
return {
// TODO
};
}
allData
containing an array of all our shows.routes
. The routes will define the total collection of routes for this collection type.return {
routes: allData.map((show, i) => {
const params = {name: show.attributes.canonicalTitle, index: show.id};
return params;
}),
};
show.id
is the unique ID in the external API, so we'll be using that.permalink: ({ params }) => `/show/${params.index}`,
show/4333
where 4333 is the id of that show in their API.async data({ params }) {
const show = allData.filter(show => show.id == params.index);
const episodes = await fetch(`https://kitsu.io/api/edge/episodes?filter[mediaId]=${params.index}}`).then(response => response.json());
show[0] = {...show[0], ...{episodes: episodes.data}};
return show;
},
Note: The return of the data must always be an array!
pageSize: Infinity,
---
section.<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Anime show: {collection.params?.name}</title>
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<link rel="stylesheet" href="/style/global.css" />
<link rel="stylesheet" href="/style/home.css" />
<style lang="scss">
.container {
margin: 4rem;
display: grid;
@media (max-width: 650px) {
margin: 2rem;
}
}
a,
p {
margin-bottom: 0.5rem;
}
h1 {
margin-bottom: 1rem;
}
</style>
</head>
<body>
<div class="container">
<a href="/">← Go back</a>
<h1>{collection.params?.name}</h1>
<p>These are all episodes for this show:</p>
<ul class="list">
{collection.data[0]?.episodes.map((item) =>
<li>{item.attributes.number} - {item.attributes.canonicalTitle}</li>
)}
</ul>
</div>
</body>
</html>
show/{id}
so we can leverage that in our existing homepage layout.Card.astro
file. Add the ID to the props of the card since we'll need that to link.--------
export interface Props {
id: number;
title: string;
image: string;
episodes: number;
score: float;
href: string
}
const { title, image, episodes, score, href, id } = Astro.props;
--------
<a href={`/show/${id}`}>Read more →</a>
index.astro
file.<Card id="{item.id}" ... />
17