Merge count and assets API endpoint, and fix pagnation after page transition

This commit is contained in:
rift 2024-08-16 16:27:48 -05:00
parent 1979bae370
commit 5ae0aad1d6
2 changed files with 9 additions and 18 deletions

View file

@ -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). // 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. // 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) => { app.get("/api/catalog-assets", async (request, reply) => {
try { try {
const page = parseInt(request.query.page, 10) || 1; // default to page 1 const page = parseInt(request.query.page, 10) || 1; // default to page 1
const totalItems = await catalog_assets.count();
if (page < 1) { if (page < 1) {
reply.status(400).send({ error: "Page must be a positive number!" }); reply.status(400).send({ error: "Page must be a positive number!" });
return; return;
@ -95,18 +98,7 @@ app.get("/api/catalog-assets", async (request, reply) => {
return acc; return acc;
}, {}); }, {});
reply.send({ assets }); reply.send({ assets, pages: Math.ceil(totalItems / 20) });
} 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) });
} catch (error) { } catch (error) {
reply.status(500).send({ error: "There was an error" }); reply.status(500).send({ error: "There was an error" });
} }

View file

@ -4,7 +4,7 @@ import CatalogCard from "../../components/catalog/CatalogCard.svelte";
const { page } = Astro.params; 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 assets_json = await response.json();
const next_page = parseInt(page!) + 1; const next_page = parseInt(page!) + 1;
@ -68,6 +68,7 @@ const last_page = assets_json.pages;
type="number" type="number"
id="pagnation_input" id="pagnation_input"
placeholder="..." placeholder="..."
transition:persist
/> />
{/* The last page. If the user is on this page, don't show it. */} {/* 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>
</div> </div>
</Layout> </Layout>
<script> <script transition:persist is:inline>
document document
.getElementById("pagnation_input")! .getElementById("pagnation_input")
.addEventListener("keyup", function (event) { .addEventListener("keyup", function (event) {
if (event.key === "Enter") { if (event.key === "Enter") {
window.location.href = ( window.location.href = document.getElementById("pagnation_input").value;
document.getElementById("pagnation_input")! as HTMLInputElement
).value;
} }
}); });
</script> </script>