Skip to content

Commit 46b41ad

Browse files
committed
fix: disable download button while loading
1 parent 5286a91 commit 46b41ad

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

client/src/components/ui/MemeCard.tsx

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import { Meme } from "@/types/Meme";
44
import { ClipboardCheck, Download, PencilLine, Share2 } from "lucide-react";
55

66
import { Card } from "@/components/ui/card";
7+
import { getMemeUrl } from "@/functions/memeUrl";
78
import { Link } from "@/i18n/navigation";
89
import { sendEvent } from "@/utils/googleAnalytics";
910
import { cn } from "@/utils/tailwind";
1011
import Image from "next/image";
1112
import { MouseEventHandler, useEffect, useState } from "react";
12-
import { getMemeUrl } from "@/functions/memeUrl";
13+
import Loader from "./loader";
1314

1415
function logDownloadEvent(meme: Meme) {
1516
sendEvent("meme_download", { meme_id: meme.id });
@@ -25,9 +26,11 @@ export default function MemeCard({
2526
loadPriority = false,
2627
}: {
2728
meme: Meme;
28-
variant?: variantType;
29-
loadPriority?: boolean;
29+
variant?: variantType;
30+
loadPriority?: boolean;
3031
}) {
32+
const [isDownloading, setIsDownloading] = useState(false);
33+
3134
const ShareIcon = <Share2 size={20} className="mx-auto" />;
3235
const ClipboardIcon = (
3336
<ClipboardCheck size={20} className="animate-fade-in-scale mx-auto" />
@@ -73,9 +76,12 @@ export default function MemeCard({
7376
});
7477
};
7578
const handleDownload: MouseEventHandler = async (e) => {
79+
e.stopPropagation();
80+
if (isDownloading) return;
81+
setIsDownloading(true);
82+
7683
// send analytics event
7784
logDownloadEvent(meme);
78-
e.stopPropagation();
7985
try {
8086
const response = await fetch(memeURL, {
8187
method: "GET",
@@ -95,6 +101,10 @@ export default function MemeCard({
95101
document.body.removeChild(a);
96102
} catch (error) {
97103
console.error("Error downloading meme:", error);
104+
} finally {
105+
setTimeout(() => {
106+
setIsDownloading(false);
107+
}, 1000);
98108
}
99109
};
100110
return (
@@ -115,7 +125,7 @@ export default function MemeCard({
115125
unoptimized={meme.media_url.endsWith(".gif")}
116126
priority={loadPriority}
117127
loading={loadPriority ? "eager" : "lazy"}
118-
fetchPriority= {loadPriority ? "high" : "auto"}
128+
fetchPriority={loadPriority ? "high" : "auto"}
119129
/>
120130
<div
121131
className={cn(
@@ -129,9 +139,14 @@ export default function MemeCard({
129139
<button
130140
id="download-meme-button"
131141
onClick={handleDownload}
142+
disabled={isDownloading}
132143
className="bg-primary border-transparent text-primary-foreground shadow rounded-full w-9 h-9 md:w-8 md:h-8"
133144
>
134-
<Download size={20} className="mx-auto" />
145+
{isDownloading ? (
146+
<Loader size={20} className="mx-auto"/>
147+
) : (
148+
<Download size={20} className="mx-auto" />
149+
)}
135150
</button>
136151
<label htmlFor="share-meme-button" className="sr-only">
137152
share meme
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Loader2 } from "lucide-react";
22

3-
export default function Loader() {
4-
return <Loader2 className="my-4 h-8 w-8 animate-spin mx-auto" />;
3+
interface LoaderProps extends React.ComponentProps<typeof Loader2> {}
4+
5+
export default function Loader(props: LoaderProps) {
6+
return <Loader2 className="my-4 h-8 w-8 animate-spin mx-auto" {...props}/>;
57
}

0 commit comments

Comments
 (0)