From e06524fb165992d207147a7413deec09618bfd01 Mon Sep 17 00:00:00 2001 From: Bram Suurd <78373894+BramSuurdje@users.noreply.github.com> Date: Sun, 19 May 2024 19:06:19 +0200 Subject: [PATCH] Update components to display most popular scripts --- components/LatestScripts.tsx | 11 ++-- components/MostPopulairScripts.tsx | 85 ++++++++++++++++++++++++++++++ components/Script.tsx | 6 ++- lib/types.ts | 1 + 4 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 components/MostPopulairScripts.tsx diff --git a/components/LatestScripts.tsx b/components/LatestScripts.tsx index 36f99d3..34dd32a 100644 --- a/components/LatestScripts.tsx +++ b/components/LatestScripts.tsx @@ -20,6 +20,7 @@ function LatestScripts() { async function getLatestScripts() { const res = await pb.collection("proxmox_scripts").getList(1, 3, { sort: "-created", + requestKey: "latest_scripts", }); setLatestScripts(res.items as unknown as Script[]); @@ -31,19 +32,23 @@ function LatestScripts() { return (
-

Newest Scripts

+

+ Newest Scripts +

{latestScripts.map((item) => (
-

{item.title}{" "}{item.item_type}

+

+ {item.title} {item.item_type} +

Date added: {extractDate(item.created)} diff --git a/components/MostPopulairScripts.tsx b/components/MostPopulairScripts.tsx new file mode 100644 index 0000000..f8c07d6 --- /dev/null +++ b/components/MostPopulairScripts.tsx @@ -0,0 +1,85 @@ +import { + Card, + CardContent, + CardDescription, + CardFooter, + CardHeader, + CardTitle, +} from "@/components/ui/card"; +import { pb } from "@/lib/pocketbase"; +import { useEffect, useState } from "react"; +import { Script } from "@/lib/types"; +import Link from "next/link"; +import Image from "next/image"; +import { Button } from "./ui/button"; +import { extractDate } from "@/lib/time"; + +export default function MostPopulairScripts() { + const [latestScripts, setLatestScripts] = useState([]); + + async function getLatestScripts() { + try { + const res = await pb.collection("proxmox_scripts").getList(1, 3, { + sort: "created", + filter: "isMostPopulair = true", + }); + setLatestScripts(res.items as unknown as Script[]); + } catch (error) { + console.error("Error fetching scripts:", error); + } + } + + + + useEffect(() => { + getLatestScripts(); + }, []); + + return ( +

+

+ Most Populair Scripts +

+
+ {latestScripts.map((item) => ( + + + +
+ +
+

+ {item.title} {item.item_type} +

+
+

+ Date added: {extractDate(item.created)} +

+
+ + + {item.description} + + + + + +
+ ))} +
+
+ ); +} diff --git a/components/Script.tsx b/components/Script.tsx index 8579857..c07dfa5 100644 --- a/components/Script.tsx +++ b/components/Script.tsx @@ -12,6 +12,7 @@ import { Script } from "@/lib/types"; import { useSearchParams } from "next/navigation"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import LatestScripts from "./LatestScripts"; +import MostPopulairScripts from "./MostPopulairScripts"; function ScriptItem() { const [item, setItem] = useState