diff --git a/src/app/ideas/[id]/IdeaAnalysisReport.tsx b/src/app/ideas/[id]/IdeaAnalysisReport.tsx index b38fe10..cec6df2 100644 --- a/src/app/ideas/[id]/IdeaAnalysisReport.tsx +++ b/src/app/ideas/[id]/IdeaAnalysisReport.tsx @@ -1,6 +1,5 @@ 'use client' -import Link from 'next/link' import { useRouter } from 'next/navigation' import { usePlausible } from 'next-plausible' import React, { useEffect, useState } from 'react' @@ -9,6 +8,7 @@ import FeedbackForm from '@/components/FeedbackForm' import HorizontalLine from '@/components/HorizontalLine' import MessageBox from '@/components/MessageBox' import { Goals } from '@/lib/goals' +import DownloadPDFButton from './components/DownloadPDFButton' import { NavBar } from './components/NavBar' import SectionActionableNextSteps from './components/SectionActionableNextSteps' import SectionCompetitors from './components/SectionCompetitors' @@ -242,8 +242,8 @@ export const IdeaAnalysisReport = ({ data }: Props) => { {readyForReport ? ( - plausible(Goals.DownloadPDF, { props: { @@ -251,11 +251,7 @@ export const IdeaAnalysisReport = ({ data }: Props) => { }, }) } - target="_blank" - className="rounded bg-blue-600 px-4 py-2 font-semibold text-white hover:bg-blue-700" - > - PDF - + /> ) : ( Loading... @@ -350,8 +346,8 @@ export const IdeaAnalysisReport = ({ data }: Props) => {

{readyForReport ? ( - plausible(Goals.DownloadPDF, { props: { @@ -359,11 +355,7 @@ export const IdeaAnalysisReport = ({ data }: Props) => { }, }) } - target="_blank" - className="rounded bg-blue-600 px-4 py-2 font-semibold text-white hover:bg-blue-700" - > - Download - + /> ) : ( Loading... diff --git a/src/app/ideas/[id]/components/DownloadPDFButton.tsx b/src/app/ideas/[id]/components/DownloadPDFButton.tsx new file mode 100644 index 0000000..d4ac48e --- /dev/null +++ b/src/app/ideas/[id]/components/DownloadPDFButton.tsx @@ -0,0 +1,100 @@ +'use client' +import React, { useState } from 'react' + +interface DownloadPDFButtonProps { + ideaId: string + onClick?: () => void +} + +type DownloadError = string | null + +const DownloadPDFButton: React.FC = ({ + ideaId, + onClick, +}) => { + const [loading, setLoading] = useState(false) + const [error, setError] = useState(null) + + const handleDownload = async () => { + if (onClick) { + onClick() + } + + setLoading(true) + setError(null) + + try { + const response = await fetch(`/api/ideas/${ideaId}/pdf`, { + method: 'GET', + headers: { + 'Content-Type': 'application/pdf', + }, + }) + + if (!response.ok) { + let errorMessage = `Server error: ${response.status} ${response.statusText}` + try { + const errorData = await response.json() + if (errorData.error) { + errorMessage = `Error: ${errorData.error}` + } + } catch (parseError) { + errorMessage = (parseError as Error).message + } + throw new Error(errorMessage) + } + + const blob = await response.blob() + + const url = window.URL.createObjectURL(new Blob([blob])) + + const link = document.createElement('a') + link.href = url + + const filename = 'CheckMVP-Report.pdf' + link.setAttribute('download', filename) + + document.body.appendChild(link) + + link.click() + + link.parentNode?.removeChild(link) + window.URL.revokeObjectURL(url) + } catch (err) { + console.error('Error downloading PDF:', err) + + if (err instanceof Error) { + setError(err.message) + } else { + setError('An unknown error occurred while downloading the PDF.') + } + } finally { + setLoading(false) + } + } + + return ( +
+ + {error && ( +

+ {error} +

+ )} +
+ ) +} + +export default DownloadPDFButton