Skip to content

Commit 1634721

Browse files
authored
Merge pull request #478 from hngprojects/auth-fixes
fix: update export button visibility logic and adjust save button con…
2 parents 19a1eb2 + 952e9c4 commit 1634721

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

src/app/(dashboard)/dashboard/details/page.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,15 @@ const AddDetails = () => {
136136
filter={filter}
137137
/>
138138
</div>
139-
<div className="md:my-10 py-3 bg-[#F0F3F5] rounded-lg md:p-10 max-w-[699px] w-full max-md:w-[90%] flex items-center justify-center">
139+
<div className="md:my-10 py-3 bg-[#F0F3F5] rounded-lg md:p-10 max-w-[699px] w-full max-md:w-[90%] flex items-center justify-center " >
140140
<Image
141141
src={pageAdData.final_url? pageAdData.final_url : pageAdData.image_url}
142142
height={310}
143143
width={335}
144144
alt={"ad Image"}
145-
className="w-full h-auto rounded-lg"
145+
className="w-full h-auto rounded-lg bg-transparent"
146146
unoptimized
147+
id='containerRef'
147148
/>
148149
</div>
149150
</section>

src/domains/external/components/common/desktop-ad-preview-navigation.tsx

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,31 +71,44 @@ export const DesktopAdPreviewNavigation: React.FC<
7171
(type === "community" && !isPublished))) ||
7272
(type === "community" && !isPublished);
7373

74+
7475
const downloadImage = async (format: "png" | "jpg") => {
76+
setIsDownloading(true);
7577
try {
7678
const element = document.getElementById("containerRef");
79+
// console.log("Element found:", element);
80+
7781
if (element) {
78-
html2canvas(element, { useCORS: true, allowTaint: true })
79-
.then((canvas) => {
80-
const link = document.createElement("a");
81-
link.download = `${imageName}.${format}`;
82-
link.href = canvas.toDataURL(`image/${format}`);
83-
link.click();
84-
})
85-
.catch((error) => {
86-
console.error("Error generating canvas:", error);
87-
toast.error("Failed to generate image for download");
88-
});
82+
const canvas = await html2canvas(element, {
83+
useCORS: true,
84+
allowTaint: true,
85+
logging: true,
86+
});
87+
88+
// console.log("Canvas created:", canvas);
89+
90+
const link = document.createElement("a");
91+
const fileName = `${imageName}.${format}`;
92+
link.download = fileName;
93+
link.href = canvas.toDataURL(`image/${format}`);
94+
// console.log("Data URL created:", link.href.substring(0, 50) + "...");
95+
96+
document.body.appendChild(link);
97+
link.click();
98+
document.body.removeChild(link);
99+
100+
} else {
101+
// console.error("Element with ID 'containerRef' not found");
102+
toast.error("Could not find the ad content to download");
89103
}
90104
} catch (error) {
91-
console.error("Error downloading image:", error);
105+
console.error("Error in downloadImage:", error);
92106
toast.error("Failed to download image");
93107
} finally {
94108
setIsDownloading(false);
95109
setIsExportDropdownOpen(false);
96110
}
97111
};
98-
99112
React.useEffect(() => {
100113
const handleClickOutside = (event: MouseEvent) => {
101114
if (
@@ -250,7 +263,7 @@ export const DesktopAdPreviewNavigation: React.FC<
250263
</button>
251264

252265
<div className="flex gap-4">
253-
{(showSaveButton || queryType == "community") && (
266+
{(showSaveButton || queryType !== "community") && (
254267
<div className="relative" ref={saveDropdownRef}>
255268
<button
256269
onClick={() => setIsSaveDropdownOpen(!isSaveDropdownOpen)}
@@ -294,7 +307,7 @@ export const DesktopAdPreviewNavigation: React.FC<
294307
</div>
295308
)}
296309

297-
{type === "image-form" && status === "completed" && (
310+
{status === "completed" && (
298311
<ShareModal
299312
adUrl={`https://genz.ad/stand-alone/${imageId}`}
300313
imageUrl={generatedImageUrl}
@@ -309,8 +322,7 @@ export const DesktopAdPreviewNavigation: React.FC<
309322
</ShareModal>
310323
)}
311324

312-
{(type === "demo" ||
313-
(type === "image-form" && status === "completed")) && (
325+
{queryType !== "community" && (
314326
<div className="relative" ref={exportDropdownRef}>
315327
<button
316328
disabled={isDownloading || isLoading}
@@ -332,7 +344,7 @@ export const DesktopAdPreviewNavigation: React.FC<
332344
>
333345
<div className="py-1">
334346
<button
335-
// onClick={() => downloadImage("png")}
347+
// onClick={() => console.log("Download as PNG")}
336348
onClick={() => downloadImage("png")}
337349
disabled={isDownloading || isLoading}
338350
className="block w-full text-left px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 cursor-pointer"

0 commit comments

Comments
 (0)