-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: change from pagination to scroll on badgedex (#578)
- Loading branch information
Showing
4 changed files
with
117 additions
and
44 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,57 @@ | ||
import Link from "next/link"; | ||
import { ReactEventHandler, useState } from "react"; | ||
|
||
interface BadgeProps { | ||
name: string; | ||
id: string | number; | ||
avatar: string; | ||
tokens: string | number; | ||
owned: boolean; | ||
} | ||
|
||
export default function Badge({ name, id, avatar, tokens, owned }: BadgeProps) { | ||
const [badgeLoaded, setBadgeLoaded] = useState(false); | ||
const [fallbackRan, setFallbackRan] = useState(false) | ||
|
||
const imageOnError: ReactEventHandler<HTMLImageElement> = (e) => { | ||
// prevent infinite loop fallback | ||
if (fallbackRan) { | ||
setBadgeLoaded(true); | ||
return | ||
} | ||
|
||
setBadgeLoaded(false); | ||
e.currentTarget.src = "/images/badges/badge-not-found.svg"; | ||
setFallbackRan(true) | ||
}; | ||
|
||
export default function Badge({ name, id, avatar, tokens, owned }) { | ||
return ( | ||
<Link | ||
href={`/badge/${id}`} | ||
className={`h-full w-full ${owned ? "opacity-100" : "opacity-30"}`} | ||
> | ||
<div className="select-none"> | ||
<img src={avatar} alt={name}></img> | ||
<div className="flex justify-center items-center w-full aspect-square select-none"> | ||
|
||
{!badgeLoaded && <BadgeSkeleton />} | ||
|
||
<img | ||
src={avatar} | ||
alt={name} | ||
onLoad={() => setBadgeLoaded(true)} | ||
onError={imageOnError} | ||
/> | ||
</div> | ||
|
||
<div className="flex flex-col justify-items-center text-center font-iregular"> | ||
<div>{name}</div> | ||
<div>{tokens} 💰 </div> | ||
</div> | ||
</Link> | ||
); | ||
} | ||
|
||
const BadgeSkeleton = () => { | ||
return ( | ||
<div className="w-10/12 aspect-square rounded-full bg-gray-500 animate-pulse opacity-10"/> | ||
); | ||
}; |
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,35 @@ | ||
import { faArrowUp } from "@fortawesome/free-solid-svg-icons"; | ||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; | ||
import { useEffect, useState } from "react"; | ||
|
||
export default function GoToTop() { | ||
const [showButton, setShowButton] = useState(false); | ||
|
||
useEffect(() => { | ||
window.addEventListener("scroll", () => { | ||
if (window.scrollY > 400) { | ||
setShowButton(true); | ||
} else { | ||
setShowButton(false); | ||
} | ||
}); | ||
}, []); | ||
|
||
const goToTop = () => { | ||
window.scrollTo({ | ||
top: 0, | ||
behavior: "smooth", | ||
}); | ||
}; | ||
|
||
return ( | ||
<button | ||
className={`transition-translate fixed right-5 bottom-5 h-12 w-12 cursor-pointer rounded-full bg-white duration-300 ${ | ||
showButton ? "" : "translate-y-52" | ||
}`} | ||
onClick={goToTop} | ||
> | ||
<FontAwesomeIcon icon={faArrowUp} className="text-3xl text-black" /> | ||
</button> | ||
); | ||
} |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.