11import { useTranslation } from "react-i18next" ;
2- import { Page , Text , View , Document , PDFViewer } from "@react-pdf/renderer" ;
2+ import { useEffect , useState } from "react" ;
3+ import {
4+ Image as PDFImage ,
5+ Page ,
6+ Text ,
7+ View ,
8+ Document ,
9+ PDFViewer ,
10+ } from "@react-pdf/renderer" ;
311
12+ import { useSecuredApi } from "@/components/auth0" ;
413import { title } from "@/components/primitives" ;
514import DefaultLayout from "@/layouts/default" ;
15+ import { ReadyForRefundPurchase } from "@/types/data" ;
16+
17+ const MAX_OLDEST_READY_TO_REFUND = 10 ;
18+
19+ /** Function for converting a webp base64 image url ( data:image/webp;base64,… )to a base64 image containing the image converted to png
20+ * @param {string } base64DataUrl - The base64 data URL ( data:image/webp;base64,… ) of the webp image
21+ * @returns {Promise<string> } - A promise that resolves to a base64 image url ( data:image/webp;base64,… ) containing the png converted of the webp image
22+ */
23+ function convertWebpToPng ( base64DataUrl : string | undefined ) : Promise < string > {
24+ return new Promise ( ( resolve , reject ) => {
25+ if ( ! base64DataUrl ) {
26+ reject ( new Error ( "No base64 data URL provided" ) ) ;
27+
28+ return ;
29+ }
30+ const img = new Image ( ) ;
31+
32+ img . src = base64DataUrl ;
33+ img . onload = ( ) => {
34+ const canvas = document . createElement ( "canvas" ) ;
35+
36+ canvas . width = img . width ;
37+ canvas . height = img . height ;
38+ const ctx = canvas . getContext ( "2d" ) ;
39+
40+ if ( ctx ) {
41+ ctx . drawImage ( img , 0 , 0 ) ;
42+ const pngDataUrl = canvas . toDataURL ( "image/png" ) ;
43+
44+ resolve ( pngDataUrl ) ;
45+ } else {
46+ reject ( new Error ( "Failed to get canvas context" ) ) ;
47+ }
48+ } ;
49+ img . onerror = ( error ) => {
50+ reject ( error ) ;
51+ } ;
52+ } ) ;
53+ }
54+
655export default function OldestReadyToRefundPage ( ) {
756 const { t } = useTranslation ( ) ;
8-
9- const ReadyToRefundPDF = ( ) => (
10- < Document >
11- < Page size = "A4" >
12- < View >
13- < Text > Section #1</ Text >
14- </ View >
15- < View >
16- < Text > Section #2</ Text >
17- </ View >
18- </ Page >
19- </ Document >
57+ const [ readyToRefund , setReadyToRefund ] = useState (
58+ [ ] as ReadyForRefundPurchase [ ] ,
2059 ) ;
60+ const { getJson } = useSecuredApi ( ) ;
61+ const fetchReadyToRefund = async ( ) => {
62+ try {
63+ const response = await getJson (
64+ `${ import . meta. env . API_BASE_URL } /purchases/ready-to-refund?limit=${ MAX_OLDEST_READY_TO_REFUND } ` ,
65+ ) ;
66+
67+ if ( response . success ) {
68+ setReadyToRefund ( response . data ) ;
69+ } else {
70+ // eslint-disable-next-line no-console
71+ console . error ( "Error fetching data:" , JSON . stringify ( response ) ) ;
72+ }
73+ } catch ( error ) {
74+ // eslint-disable-next-line no-console
75+ console . error ( "Error fetching data:" , error ) ;
76+ }
77+ } ;
78+
79+ useEffect ( ( ) => {
80+ fetchReadyToRefund ( ) ;
81+ } , [ ] ) ;
2182
2283 return (
2384 < DefaultLayout >
@@ -27,12 +88,33 @@ export default function OldestReadyToRefundPage() {
2788 < p > { t ( "oldest-ready-to-refund-description" ) } </ p >
2889 </ div >
2990 </ section >
30- < section className = "flex flex-col items-center justify-center gap-4 py-8 md:py-10" >
31- < div className = "inline-block max-w-lg text-center justify-center" >
32- < PDFViewer style = { { width : "100%" , height : "600px" } } >
33- < ReadyToRefundPDF />
91+ < section className = "flex flex-col items-center justify-center min-w-full lg:min-w-2xl" >
92+ { readyToRefund && readyToRefund . length > 0 ? (
93+ < PDFViewer className = "w-full h-screen" >
94+ < Document >
95+ < Page size = "A4" >
96+ < View >
97+ { readyToRefund . map ( ( purchase ) => (
98+ < View key = { purchase . id } >
99+ < Text > { `ID: ${ purchase . id } ` } </ Text >
100+ < Text > { `Date: ${ purchase . date } ` } </ Text >
101+ < Text > { `Order: ${ purchase . order } ` } </ Text >
102+ < Text > { `Description: ${ purchase . description } ` } </ Text >
103+ < Text > { `Refunded: ${ purchase . refunded } ` } </ Text >
104+ < Text > { `Amount: ${ purchase . amount } ` } </ Text >
105+ < PDFImage
106+ src = { convertWebpToPng ( purchase . publicationScreenShot ) }
107+ style = { { width : "50%" , height : "auto" } }
108+ />
109+ </ View >
110+ ) ) }
111+ </ View >
112+ </ Page >
113+ </ Document >
34114 </ PDFViewer >
35- </ div >
115+ ) : (
116+ < p > { t ( "no-data-available" ) } </ p >
117+ ) }
36118 </ section >
37119 </ DefaultLayout >
38120 ) ;
0 commit comments