-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from ethanl21/leaderboard
Leaderboard
- Loading branch information
Showing
4 changed files
with
164 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { getDownloadURL, ref } from "firebase/storage"; | ||
import { useEffect, useState } from "react"; | ||
import { ImageProps } from "react-bootstrap"; | ||
import { storage } from "../config/firebase"; | ||
import Image from "react-bootstrap/Image"; | ||
|
||
export const FirebaseImage = ({ ...props }: ImageProps) => { | ||
const [imageSrc, setImageSrc] = useState(""); | ||
|
||
useEffect(() => { | ||
getDownloadURL(ref(storage, props.src)).then((val) => { | ||
setImageSrc(val); | ||
}); | ||
}, [props.src]); | ||
|
||
return <Image {...props} src={imageSrc} />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import Button from "react-bootstrap/Button"; | ||
import Table from "react-bootstrap/Table"; | ||
import { | ||
RatingItem, | ||
getHighestRatedItems, | ||
getLowestRatedItems, | ||
} from "../tasks/getRatingItems"; | ||
import { auth as fAuth } from "../config/firebase"; | ||
import { useEffect, useState } from "react"; | ||
import { nanoid } from "nanoid"; | ||
import { useAuthState } from "react-firebase-hooks/auth"; | ||
import { | ||
BsFillHandThumbsDownFill, | ||
BsFillHandThumbsUpFill, | ||
} from "react-icons/bs"; | ||
import { FirebaseImage } from "./FirebaseImage"; | ||
import Container from "react-bootstrap/Container"; | ||
|
||
const getTableBody = (items: Array<RatingItem>) => { | ||
return items.map((val, idx) => { | ||
return ( | ||
<tr key={nanoid()}> | ||
<td>{idx + 1}</td> | ||
<td>{val.name}</td> | ||
<td> | ||
<FirebaseImage src={val.image} thumbnail width="100" /> | ||
</td> | ||
<td>{val.averageRating} ★</td> | ||
</tr> | ||
); | ||
}); | ||
}; | ||
|
||
export const Leaderboard = () => { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const [auth, authLoading, authError] = useAuthState(fAuth); | ||
const [leaderboardItems, setLeaderboardItems] = useState<Array<RatingItem>>( | ||
[], | ||
); | ||
const [leaderboardSort, setLeaderboardSort] = useState("desc"); | ||
|
||
useEffect(() => { | ||
if (auth) { | ||
if (leaderboardSort === "desc") { | ||
getHighestRatedItems(10).then((items) => { | ||
setLeaderboardItems(items); | ||
}); | ||
} else { | ||
getLowestRatedItems(10).then((items) => { | ||
setLeaderboardItems(items); | ||
}); | ||
} | ||
} | ||
}, [auth, leaderboardSort]); | ||
|
||
return ( | ||
<> | ||
<h1> | ||
{leaderboardSort === "desc" ? "Highest" : "Lowest"} Rated Items | ||
<Button | ||
onClick={() => { | ||
leaderboardSort === "desc" | ||
? setLeaderboardSort("asc") | ||
: setLeaderboardSort("desc"); | ||
}} | ||
> | ||
{leaderboardSort === "desc" ? ( | ||
<BsFillHandThumbsUpFill /> | ||
) : ( | ||
<BsFillHandThumbsDownFill /> | ||
)} | ||
</Button> | ||
</h1> | ||
<Container> | ||
<Table striped bordered hover responsive> | ||
<thead> | ||
<tr> | ||
<td>#</td> | ||
<td>Name</td> | ||
<td>Image</td> | ||
<td>Average Rating</td> | ||
</tr> | ||
</thead> | ||
<tbody>{leaderboardItems && getTableBody(leaderboardItems)}</tbody> | ||
</Table> | ||
</Container> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters