Skip to content

Commit

Permalink
added pagination, removed Season 2 stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
jyorien committed Jan 10, 2024
1 parent 328ef5f commit 9a87dd9
Showing 1 changed file with 57 additions and 14 deletions.
71 changes: 57 additions & 14 deletions apps/web/pages/challenges/leaderboard/index.tsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,75 @@
import LeaderboardEntry from "@/features/challenges/components/LeaderboardEntry"
import { Box, Flex, Select, Spacer, Text } from "@chakra-ui/react"
import { Box, Button, Flex, Select, Spacer, Text } from "@chakra-ui/react"
import { useState } from "react"
import { Pagination } from "react-bootstrap"

// placeholder for API Response
let currentSeasonData: LeaderboardData[] = [
{ uuid: 1000, userId: "alex200", points: 100 },
{ uuid: 1001, userId: "johndoe010", points: 200 },
{ uuid: 1002, userId: "janedoe345", points: 350 },
]

const seasonTwoData: LeaderboardData[] = [
{ uuid: 1000, userId: "harvey562", points: 209 },
{ uuid: 1001, userId: "pam391", points: 540 },
{ uuid: 1002, userId: "linus903", points: 250 },
{ uuid: 1002, userId: "fwqdqw", points: 350 },
{ uuid: 1003, userId: "cwcqw", points: 350 },
{ uuid: 1004, userId: "dqwdq", points: 350 },
{ uuid: 1005, userId: "wqcqw", points: 350 },
{ uuid: 1006, userId: "cwqc", points: 350 },
]

interface LeaderboardData {
uuid: number,
userId: string,
points: number
}
const Leaderboard = () => {
const [currentDisplayedData, setCurrentDisplayedData] = useState(currentSeasonData)

function paginateData(data: LeaderboardData[], numItemsPerPage: number) : LeaderboardData[][] {
const data2D : LeaderboardData[][] = []
for (var i = 0; i < data.length; i += numItemsPerPage) {
data2D.push(data.slice(i, i + numItemsPerPage))
}
return data2D
}

interface PaginationButtonProps {
onClick : () => void
variant?: string
text: string
}
const PaginationButton = ({onClick, variant = 'primary-blue', text} : PaginationButtonProps) => {

return <Button onClick={onClick} variant={variant} size={['sm','md']} _hover={{bg: variant}} mx={4}>{text}</Button>
}

const numOfItemsPerPage = 3

const numOfPages = Math.ceil(currentSeasonData.length / numOfItemsPerPage)
const paginatedCurrentSeasonData = paginateData(currentSeasonData, numOfItemsPerPage)

const Leaderboard = () => {
const [currentPage, setCurrentPage] = useState(0)

const [currentDisplayedData, setCurrentDisplayedData] = useState(paginatedCurrentSeasonData[currentPage])
function handleDataChange(event: React.ChangeEvent<HTMLSelectElement>) {
// fetch and update leaderboard
setCurrentPage(0)
switch (event.target.value) {
case 'season-id-1':
setCurrentDisplayedData(currentSeasonData)
break
case 'season-id-2':
setCurrentDisplayedData(seasonTwoData)
setCurrentDisplayedData(paginatedCurrentSeasonData[currentPage])
break
}

}

function goToPage(index: number) {
let toSet = index
if (index < 0) toSet = 0
else if (index > numOfPages-1) toSet = numOfPages - 1
console.log(`toSet: ${toSet}`)
setCurrentPage(toSet)
setCurrentDisplayedData(paginatedCurrentSeasonData[toSet])
}

return (<Flex
minH="100vh"
pt={24}
flexDirection="column"
justifyContent="center"
alignItems="center">
Expand All @@ -49,7 +82,17 @@ const Leaderboard = () => {
return <LeaderboardEntry key={item.uuid} index={index+1} name={item.userId} points={item.points}/>
})}
</Box>
<Flex justifyContent='center' w="60vw" align="center" py={8}>
<PaginationButton onClick={() =>{goToPage(currentPage - 1)}} text="<"/>

{Array.from(Array(numOfPages).keys()).map((index) => <PaginationButton onClick={() => {goToPage(index)}} variant={currentPage == index ? 'primary-black' : 'primary-blue'} text={(index+1).toString()}/>)}

<PaginationButton onClick={() =>{goToPage(currentPage + 1)}} text=">"/>

</Flex>
</Flex>)
}



export default Leaderboard

0 comments on commit 9a87dd9

Please sign in to comment.