generated from muhtadeetaron/nextjs-template
chore: refactor code for type safety
This commit is contained in:
@ -1,24 +1,29 @@
|
||||
"use client";
|
||||
|
||||
import React, { useState, useEffect } from "react";
|
||||
import React, { useState, useEffect, ReactNode } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import Image from "next/image";
|
||||
import Link from "next/link";
|
||||
import Header from "@/components/Header";
|
||||
import SlidingGallery from "@/components/SlidingGallery";
|
||||
import BackgroundWrapper from "@/components/BackgroundWrapper";
|
||||
import DestructibleAlert from "@/components/DestructibleAlert";
|
||||
import { ChevronRight } from "lucide-react"; // Using Lucide React for icons
|
||||
import styles from "@/css/Home.module.css";
|
||||
import facebookStyles from "@/css/SlidingGallery.module.css";
|
||||
import { API_URL } from "@/lib/auth";
|
||||
import { Avatar, AvatarFallback } from "@radix-ui/react-avatar";
|
||||
import { Avatar } from "@/components/ui/avatar";
|
||||
import { getLinkedViews } from "@/lib/gallery-views";
|
||||
import { getTopThree } from "@/lib/leaderboard";
|
||||
|
||||
interface LinkedView {
|
||||
id: string;
|
||||
content: ReactNode;
|
||||
}
|
||||
|
||||
const page = () => {
|
||||
const profileImg = "/images/static/avatar.jpg";
|
||||
const router = useRouter();
|
||||
const [boardData, setBoardData] = useState([]);
|
||||
const [boardError, setBoardError] = useState(null);
|
||||
const [linkedViews, setLinkedViews] = useState<LinkedView[]>();
|
||||
|
||||
const performanceData = [
|
||||
{ label: "Mock Test", progress: 20 },
|
||||
@ -31,38 +36,6 @@ const page = () => {
|
||||
{ label: "Chemistry", progress: 57 },
|
||||
];
|
||||
|
||||
const facebookViews = [
|
||||
{
|
||||
id: "1",
|
||||
content: (
|
||||
<Link
|
||||
href="https://www.facebook.com/share/g/15jdqESvWV/?mibextid=wwXIfr"
|
||||
className="w-full h-full block text-inherit box-border"
|
||||
>
|
||||
<div className="w-full h-full p-6 flex text-black bg-blue-50 rounded-4xl border-[0.5px] border-[#113768]/30">
|
||||
<div className="">
|
||||
<h3 className="text-2xl text-[#113768] font-black">
|
||||
Meet, Share, and Learn!
|
||||
</h3>
|
||||
<p className="font-bold text-sm text-[#113768] ">
|
||||
Join Facebook Community
|
||||
</p>
|
||||
</div>
|
||||
<div className={facebookStyles.logoView}>
|
||||
<Image
|
||||
src="/images/static/facebook-logo.png"
|
||||
alt="Facebook Logo"
|
||||
width={150}
|
||||
height={150}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</Link>
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
// Fetch function for leaderboard data
|
||||
useEffect(() => {
|
||||
let isMounted = true;
|
||||
async function fetchBoardData() {
|
||||
@ -77,32 +50,21 @@ const page = () => {
|
||||
if (isMounted) setBoardError(error.message || "An error occurred");
|
||||
}
|
||||
}
|
||||
fetchBoardData();
|
||||
const fetchedLinkedViews = getLinkedViews();
|
||||
setLinkedViews(fetchedLinkedViews);
|
||||
|
||||
return () => {
|
||||
isMounted = false;
|
||||
};
|
||||
}, []);
|
||||
|
||||
const getTopThree = (boardData) => {
|
||||
if (!boardData || boardData.length === 0) return [];
|
||||
return boardData
|
||||
.slice()
|
||||
.sort((a, b) => b.points - a.points)
|
||||
.slice(0, 3)
|
||||
.map((player, index) => ({
|
||||
...player,
|
||||
rank: index + 1,
|
||||
height: index === 0 ? 250 : index === 1 ? 200 : 170,
|
||||
}));
|
||||
};
|
||||
|
||||
return (
|
||||
<BackgroundWrapper>
|
||||
<div className={styles.container}>
|
||||
<Header displayTabTitle={null} displayUser image={profileImg} />
|
||||
<Header displayUser />
|
||||
<div className={styles.scrollContainer}>
|
||||
<div className={styles.contentWrapper}>
|
||||
<SlidingGallery views={facebookViews} height="23vh" />
|
||||
<SlidingGallery views={linkedViews} height="23vh" />
|
||||
<div className={styles.mainContent}>
|
||||
{/* Categories Section */}
|
||||
<div>
|
||||
|
||||
@ -5,13 +5,7 @@ import Link from "next/link";
|
||||
import { ReactNode } from "react";
|
||||
import { usePathname } from "next/navigation";
|
||||
import clsx from "clsx";
|
||||
import {
|
||||
House,
|
||||
LayoutGrid,
|
||||
Bookmark,
|
||||
CircleUser,
|
||||
Settings,
|
||||
} from "lucide-react";
|
||||
import { House, LayoutGrid, Bookmark, Settings } from "lucide-react";
|
||||
|
||||
const tabs = [
|
||||
{ name: "Home", href: "/home", component: <House size={30} /> },
|
||||
|
||||
@ -4,39 +4,17 @@ import BackgroundWrapper from "@/components/BackgroundWrapper";
|
||||
import Header from "@/components/Header";
|
||||
import { Avatar, AvatarFallback } from "@/components/ui/avatar";
|
||||
import { API_URL, getToken } from "@/lib/auth";
|
||||
import { BoardData, getLeaderboard, getUserData } from "@/lib/leaderboard";
|
||||
import { UserData } from "@/types/auth";
|
||||
import Image from "next/image";
|
||||
import React, { useEffect, useState } from "react";
|
||||
|
||||
const page = () => {
|
||||
const LeaderboardPage = () => {
|
||||
const [boardError, setBoardError] = useState<string | null>(null);
|
||||
const [boardData, setBoardData] = useState([]);
|
||||
const [userData, setUserData] = useState(null);
|
||||
const [boardData, setBoardData] = useState<BoardData[]>([]);
|
||||
const [userData, setUserData] = useState<UserData>();
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
async function fetchBoardData() {
|
||||
try {
|
||||
const boardResponse = await fetch(`${API_URL}/leaderboard`, {
|
||||
method: "GET",
|
||||
});
|
||||
|
||||
if (!boardResponse.ok) {
|
||||
throw new Error("Failed to fetch leaderboard data");
|
||||
}
|
||||
|
||||
const fetchedBoardData = await boardResponse.json();
|
||||
if (Array.isArray(fetchedBoardData) && fetchedBoardData.length > 0) {
|
||||
setBoardData(fetchedBoardData);
|
||||
} else {
|
||||
setBoardError("No leaderboard data available.");
|
||||
setBoardData([]);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
setBoardError("Something went wrong. Please try again.");
|
||||
setBoardData([]);
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
async function fetchUser() {
|
||||
try {
|
||||
@ -55,7 +33,31 @@ const page = () => {
|
||||
setUserData(fetchedUserData);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
setUserData(null);
|
||||
setUserData(undefined);
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchBoardData() {
|
||||
try {
|
||||
const boardResponse = await fetch(`${API_URL}/leaderboard`, {
|
||||
method: "GET",
|
||||
});
|
||||
|
||||
if (!boardResponse.ok) {
|
||||
throw new Error("Failed to fetch leaderboard data");
|
||||
}
|
||||
|
||||
const fetchedBoardData = await boardResponse.json();
|
||||
if (Array.isArray(fetchedBoardData) && fetchedBoardData.length > 0) {
|
||||
setBoardData(fetchedBoardData);
|
||||
} else {
|
||||
setBoardError("No leaderboard data available.");
|
||||
setBoardData([]);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
setBoardError("Something went wrong. Please try again.");
|
||||
setBoardData([]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -63,7 +65,7 @@ const page = () => {
|
||||
fetchBoardData();
|
||||
}, []);
|
||||
|
||||
const getTopThree = (boardData) => {
|
||||
const getTopThree = (boardData: BoardData[]) => {
|
||||
if (!boardData || !Array.isArray(boardData)) return [];
|
||||
const sortedData = boardData
|
||||
.filter((player) => player?.points !== undefined) // Ensure `points` exists
|
||||
@ -78,24 +80,10 @@ const page = () => {
|
||||
return [topThree[1], topThree[0], topThree[2]].filter(Boolean); // Handle missing players
|
||||
};
|
||||
|
||||
const getLeaderboard = (boardData) => {
|
||||
return boardData.slice().sort((a, b) => b.points - a.points);
|
||||
};
|
||||
|
||||
const getUserData = (boardData, name) => {
|
||||
if (!boardData || !Array.isArray(boardData)) return [];
|
||||
const sortedData = boardData
|
||||
.filter((player) => player?.name && player?.points !== undefined)
|
||||
.sort((a, b) => b.points - a.points);
|
||||
|
||||
const result = sortedData.find((player) => player.name === name);
|
||||
return result ? [{ ...result, rank: sortedData.indexOf(result) + 1 }] : [];
|
||||
};
|
||||
|
||||
return (
|
||||
<BackgroundWrapper>
|
||||
<section>
|
||||
<Header displaySubject={"Leaderboard"} displayTabTitle={null} />
|
||||
<Header displayTabTitle={"Leaderboard"} />
|
||||
<section className="flex flex-col mx-10 pt-10 space-y-4">
|
||||
<section className="flex justify-evenly items-end">
|
||||
{getTopThree(boardData).map((student, idx) =>
|
||||
@ -124,7 +112,7 @@ const page = () => {
|
||||
<div className="w-full border-[0.5px] border-[#c5dbf8] bg-[#c5dbf8]"></div>
|
||||
<section className="border-[1px] border-[#c0dafc] w-full rounded-3xl p-6 space-y-4 mb-20">
|
||||
<section>
|
||||
{getUserData(boardData, userData?.name).map((user, idx) => (
|
||||
{getUserData(boardData, userData!.name).map((user, idx) => (
|
||||
<div
|
||||
key={idx}
|
||||
className="flex bg-[#113768] rounded-[8] py-2 px-4 justify-between items-center"
|
||||
@ -179,4 +167,4 @@ const page = () => {
|
||||
);
|
||||
};
|
||||
|
||||
export default page;
|
||||
export default LeaderboardPage;
|
||||
|
||||
@ -6,15 +6,7 @@ import { getToken, API_URL } from "@/lib/auth";
|
||||
import { ChevronLeft, Edit2, Lock, Save } from "lucide-react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import React, { useEffect, useState } from "react";
|
||||
|
||||
interface UserData {
|
||||
name: string;
|
||||
institution: string;
|
||||
sscRoll: string;
|
||||
hscRoll: string;
|
||||
email: string;
|
||||
phone: string;
|
||||
}
|
||||
import { UserData } from "@/types/auth";
|
||||
|
||||
const ProfilePage = () => {
|
||||
const router = useRouter();
|
||||
|
||||
Reference in New Issue
Block a user