Add category sorting and fetching logic in data.ts

This commit is contained in:
Bram Suurd 2024-11-06 21:31:52 +01:00
parent d9a6b8bdd3
commit 70ae6ae8d1

26
frontend/src/lib/data.ts Normal file
View File

@ -0,0 +1,26 @@
import { Category } from "./types";
const sortCategories = (categories: Category[]) => {
const sortedCategories = categories.sort((a, b) => {
if (a.name === "Proxmox VE Tools") {
return -1;
} else if (b.name === "Proxmox VE Tools") {
return 1;
} else if (a.name === "Miscellaneous") {
return 1;
} else if (b.name === "Miscellaneous") {
return -1;
} else {
return a.name.localeCompare(b.name);
}
});
return sortedCategories;
};
export const fetchCategories = async () => {
const categories = await fetch(`api/categories`).then((response) =>
response.json(),
);
return sortCategories(categories);
};