Skip to content

Commit

Permalink
fix leaderboard..?
Browse files Browse the repository at this point in the history
  • Loading branch information
docimin committed Nov 12, 2023
1 parent ed4aa61 commit 9c943ec
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 62 deletions.
14 changes: 3 additions & 11 deletions app/pat/leaderboard/page.client.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
"use client";
import { useEffect, useState } from "react";
import { getLeaderboardData } from "./page.server";

export const runtime = "edge";

export default function PatLeaderBoardClient() {
export default function PatLeaderBoardClient({ usersData }) {
const [userData, setUserData] = useState([]);

useEffect(() => {
const fetchData = async () => {
const data = await getLeaderboardData();
const data = usersData;
const sortedData = data.sort(
(a, b) => parseInt(b.count) - parseInt(a.count)
);
Expand All @@ -18,12 +15,7 @@ export default function PatLeaderBoardClient() {
};

fetchData();
}, []);

// If userData is empty, return a loading message or placeholder
if (userData.length === 0) {
return <div>Loading...</div>; // You can customize this loading indicator
}
}, [usersData]);

return (
<div className="px-4 sm:px-6 lg:px-8">
Expand Down
49 changes: 47 additions & 2 deletions app/pat/leaderboard/page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,56 @@ export const metadata = {
description: "Headpat clicker Leaderboard für Headpawties!",
};

export default function PatLeaderBoard() {
export default async function PatLeaderBoard() {
const response = await fetch(
`${process.env.NEXT_PUBLIC_DOMAIN}/api/fun/pats?populate=*`,
{
method: "GET",
}
);
const data = await response.json();
const usersData = [];

for (const item of data.data) {
const userId = item.attributes.users_permissions_user.data.id;
const userResponse = await fetch(
`${process.env.NEXT_PUBLIC_DOMAIN}/api/user/getUserData/${userId}?populate=avatar`,
{
method: "GET",
}
);
const userData = await userResponse.json();

const displayName =
userData.data.attributes.displayname ||
item.attributes.users_permissions_user.data.attributes.username;

usersData.push({
id: item.id,
count: item.attributes.count,
userId: userId,
username: item.attributes.users_permissions_user.data.attributes.username,
displayName: displayName,
image:
userData.data?.attributes?.avatar?.data?.attributes?.url ||
"/logos/logo-64.webp",
lastclicked: new Date(item.attributes.updatedAt).toLocaleDateString(
"en-GB",
{
day: "2-digit",
month: "2-digit",
year: "numeric",
hour: "2-digit",
minute: "2-digit",
}
),
});
}

return (
<>
<Header />
<Client />
<Client usersData={usersData} />
<Footer />
</>
);
Expand Down
49 changes: 0 additions & 49 deletions app/pat/leaderboard/page.server.jsx

This file was deleted.

0 comments on commit 9c943ec

Please sign in to comment.