Merge count and assets API endpoint, and fix pagnation after page transition
This commit is contained in:
parent
1979bae370
commit
5ae0aad1d6
2 changed files with 9 additions and 18 deletions
16
server.js
16
server.js
|
|
@ -62,11 +62,14 @@ app.get("/api", function (request, reply) {
|
|||
});
|
||||
|
||||
// This API returns a list of the assets in the database (SW plugins and themes).
|
||||
// It also returns the number of pages in the database.
|
||||
// It can take a `?page=x` argument to display a different page, with a limit of 20 assets per page.
|
||||
app.get("/api/catalog-assets", async (request, reply) => {
|
||||
try {
|
||||
const page = parseInt(request.query.page, 10) || 1; // default to page 1
|
||||
|
||||
const totalItems = await catalog_assets.count();
|
||||
|
||||
if (page < 1) {
|
||||
reply.status(400).send({ error: "Page must be a positive number!" });
|
||||
return;
|
||||
|
|
@ -95,18 +98,7 @@ app.get("/api/catalog-assets", async (request, reply) => {
|
|||
return acc;
|
||||
}, {});
|
||||
|
||||
reply.send({ assets });
|
||||
} catch (error) {
|
||||
reply.status(500).send({ error: "There was an error" });
|
||||
}
|
||||
});
|
||||
|
||||
// This API returns the total number of pages in the database.
|
||||
app.get("/api/catalog-pages", async (request, reply) => {
|
||||
try {
|
||||
const totalItems = await catalog_assets.count();
|
||||
|
||||
reply.send({ pages: Math.ceil(totalItems / 20) });
|
||||
reply.send({ assets, pages: Math.ceil(totalItems / 20) });
|
||||
} catch (error) {
|
||||
reply.status(500).send({ error: "There was an error" });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import CatalogCard from "../../components/catalog/CatalogCard.svelte";
|
|||
|
||||
const { page } = Astro.params;
|
||||
|
||||
const response = await fetch(new URL("/api/catalog-pages/", Astro.url));
|
||||
const response = await fetch(new URL("/api/catalog-assets/", Astro.url));
|
||||
const assets_json = await response.json();
|
||||
|
||||
const next_page = parseInt(page!) + 1;
|
||||
|
|
@ -68,6 +68,7 @@ const last_page = assets_json.pages;
|
|||
type="number"
|
||||
id="pagnation_input"
|
||||
placeholder="..."
|
||||
transition:persist
|
||||
/>
|
||||
{/* The last page. If the user is on this page, don't show it. */}
|
||||
{
|
||||
|
|
@ -83,14 +84,12 @@ const last_page = assets_json.pages;
|
|||
</div>
|
||||
</div>
|
||||
</Layout>
|
||||
<script>
|
||||
<script transition:persist is:inline>
|
||||
document
|
||||
.getElementById("pagnation_input")!
|
||||
.getElementById("pagnation_input")
|
||||
.addEventListener("keyup", function (event) {
|
||||
if (event.key === "Enter") {
|
||||
window.location.href = (
|
||||
document.getElementById("pagnation_input")! as HTMLInputElement
|
||||
).value;
|
||||
window.location.href = document.getElementById("pagnation_input").value;
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue