Skip to content

Commit

Permalink
add actions for builder and packages pages as well
Browse files Browse the repository at this point in the history
  • Loading branch information
ammakr committed Dec 5, 2024
1 parent faad035 commit ec9afbc
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 35 deletions.
71 changes: 61 additions & 10 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ import { defineAction } from "astro:actions";
import { z } from "astro:schema";

let bearerToken = import.meta.env.STRAPI_WRITE_TOKEN;
let strapiURL = import.meta.env.STRAPI_URL;

const getDownloadCounts = async () => {
try {
const response = await fetch(
`https://cms.athenaos.org/api/download-counts-list`,
{
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${bearerToken}`,
},
}
);
const response = await fetch(`${strapiURL}/api/download-counts-list`, {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${bearerToken}`,
},
});
const result = await response.json();
return result;
} catch (error) {
Expand All @@ -34,7 +32,7 @@ const increaseDownloadCount = async (id: string) => {
let countId = data.find((item) => item.download_type === id)?.id;

const response = await fetch(
`https://cms.athenaos.org/api/download-count/${countId}/increment`,
`${strapiURL}/api/download-count/${countId}/increment`,
{
method: "POST",
headers: {
Expand All @@ -50,7 +48,36 @@ const increaseDownloadCount = async (id: string) => {
}
};

async function getBuilderData(input: any) {
const response = await fetch(
`${strapiURL}/api/builders?sort[1]=package_name:asc${input.searchString}${input.paginationString}${input.filterString}`,
{
headers: {
Authorization: `Bearer ${bearerToken}`,
"Content-Type": "application/json",
},
}
);

return await response.json();
}

async function getPackagesData(input: any) {
const response = await fetch(
`${strapiURL}/api/packages?sort[1]=package_name:asc${input.paginationString}${input.categoryString}${input.searchString}`,
{
headers: {
Authorization: `Bearer ${bearerToken}`,
"Content-Type": "application/json",
},
}
);

return await response.json();
}

export const server = {
// download counts
getDownloadCount: defineAction({
handler: async () => {
return await getDownloadCounts();
Expand All @@ -64,4 +91,28 @@ export const server = {
return await increaseDownloadCount(input.id);
},
}),

// builder
getBuilderData: defineAction({
input: z.object({
searchString: z.string(),
paginationString: z.string(),
filterString: z.string(),
}),
handler: async (input) => {
return await getBuilderData(input);
},
}),

// packages
getPackagesData: defineAction({
input: z.object({
paginationString: z.string(),
searchString: z.string(),
categoryString: z.string(),
}),
handler: async (input) => {
return await getPackagesData(input);
},
}),
};
29 changes: 15 additions & 14 deletions src/components/builder-table.astro
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ let architectures = ["x86_64", "i686", "ARMv6h", "ARMv7h", "AArch64"];
let statuses;
if (builderData)
statuses = architectures.map((str) => ({ [str]: getUniqueValues(str) }));
let logURL = "https://logs.athenaos.org/";
---

<SearchPackages placeholder="Search Builder" />
Expand Down Expand Up @@ -139,7 +137,11 @@ let logURL = "https://logs.athenaos.org/";
</div>
</div>

<script define:vars={{ strapiURL, bearerToken, logURL, architectures }}>
<script>
import { actions } from "astro:actions";

let logURL = "https://logs.athenaos.org/";
let architectures = ["x86_64", "i686", "ARMv6h", "ARMv7h", "AArch64"];
const dataRows = document.getElementById("dataRows");
const sentinel = document.getElementById("sentinel");
let currentPage = 1;
Expand Down Expand Up @@ -170,17 +172,16 @@ let logURL = "https://logs.athenaos.org/";
? `&filters[status_${filterInput.platformName}][$eq]=${filterInput.value}`
: "";

const response = await fetch(
`${strapiURL}/api/builders?sort[1]=package_name:asc${searchString}${paginationString}${filterString}`,
{
headers: {
Authorization: `Bearer ${bearerToken}`,
"Content-Type": "application/json",
},
}
);
try {
const apiResponse = await response.json();
let response = await actions.getBuilderData({
searchString,
paginationString,
filterString,
});

let apiResponse = await response.data;
let error = await response.error;

newRemoveLoadingSpinners();

if (apiResponse.data.length === 0) {
Expand Down Expand Up @@ -214,7 +215,7 @@ let logURL = "https://logs.athenaos.org/";
renderTable(builderData);
currentPage++;
} catch (e) {
showErrorMessage();
howErrorMessage();
console.log(e);
} finally {
loading = false;
Expand Down
20 changes: 9 additions & 11 deletions src/components/package-table.astro
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ let categoriesUnique: string[] = [...new Set(categoriesList.sort())];
</div>
</div>

<script define:vars={{ strapiURL, bearerToken }}>
<script>
import { actions } from "astro:actions";

let currentPage = 1;
let pageSize = 100;
let categoryInput = null;
Expand All @@ -135,17 +137,13 @@ let categoriesUnique: string[] = [...new Set(categoriesList.sort())];
: `&pagination[page]=1&pagination[pageSize]=${pageSize}`;

try {
const response = await fetch(
`${strapiURL}/api/packages?sort[1]=package_name:asc${paginationString}${categoryString}${searchString}`,
{
headers: {
Authorization: `Bearer ${bearerToken}`,
"Content-Type": "application/json",
},
}
);
let response = await actions.getPackagesData({
searchString,
paginationString,
categoryString,
});

const apiResponse = await response.json();
const apiResponse = await response.data;
newRemoveLoadingSpinners();

if (apiResponse.data.length === 0) {
Expand Down

0 comments on commit ec9afbc

Please sign in to comment.