11import React , { useState , useEffect } from "react" ;
2- import axios from "axios " ;
2+ import { api } from "../../api/apiClient " ;
33import Layout from "../Layout/Layout" ;
44import FileRow from "./FileRow" ;
55import Table from "../../components/Table/Table" ;
@@ -30,17 +30,17 @@ const ListOfFiles: React.FC<{ showTable?: boolean }> = ({
3030 const [ downloading , setDownloading ] = useState < string | null > ( null ) ;
3131 const [ opening , setOpening ] = useState < string | null > ( null ) ;
3232
33+ const baseUrl = import . meta. env . VITE_API_BASE_URL ;
34+
3335 useEffect ( ( ) => {
3436 const fetchFiles = async ( ) => {
3537 try {
36- const baseUrl = import . meta. env . VITE_API_BASE_URL ;
37- const response = await axios . get ( `${ baseUrl } /v1/api/uploadFile` , {
38- headers : {
39- Authorization : `JWT ${ localStorage . getItem ( "access" ) } ` ,
40- } ,
41- } ) ;
42- if ( Array . isArray ( response . data ) ) {
43- setFiles ( response . data ) ;
38+ const url = `${ baseUrl } /v1/api/uploadFile` ;
39+
40+ const { data } = await api . get ( url ) ;
41+
42+ if ( Array . isArray ( data ) ) {
43+ setFiles ( data ) ;
4444 }
4545 } catch ( error ) {
4646 console . error ( "Error fetching files" , error ) ;
@@ -50,7 +50,7 @@ const ListOfFiles: React.FC<{ showTable?: boolean }> = ({
5050 } ;
5151
5252 fetchFiles ( ) ;
53- } , [ ] ) ;
53+ } , [ baseUrl ] ) ;
5454
5555 const updateFileName = ( guid : string , updatedFile : Partial < File > ) => {
5656 setFiles ( ( prevFiles ) =>
@@ -63,15 +63,9 @@ const ListOfFiles: React.FC<{ showTable?: boolean }> = ({
6363 const handleDownload = async ( guid : string , fileName : string ) => {
6464 try {
6565 setDownloading ( guid ) ;
66- const baseUrl = import . meta. env . VITE_API_BASE_URL ;
67- const response = await axios . get ( `${ baseUrl } /v1/api/uploadFile/${ guid } ` , {
68- headers : {
69- Authorization : `JWT ${ localStorage . getItem ( "access" ) } ` ,
70- } ,
71- responseType : "blob" ,
72- } ) ;
66+ const { data } = await api . get ( `/v1/api/uploadFile/${ guid } ` , { responseType : 'blob' } ) ;
7367
74- const url = window . URL . createObjectURL ( new Blob ( [ response . data ] ) ) ;
68+ const url = window . URL . createObjectURL ( new Blob ( [ data ] ) ) ;
7569 const link = document . createElement ( "a" ) ;
7670 link . href = url ;
7771 link . setAttribute ( "download" , fileName ) ;
@@ -90,15 +84,9 @@ const ListOfFiles: React.FC<{ showTable?: boolean }> = ({
9084 const handleOpen = async ( guid : string ) => {
9185 try {
9286 setOpening ( guid ) ;
93- const baseUrl = import . meta. env . VITE_API_BASE_URL ;
94- const response = await axios . get ( `${ baseUrl } /v1/api/uploadFile/${ guid } ` , {
95- headers : {
96- Authorization : `JWT ${ localStorage . getItem ( "access" ) } ` ,
97- } ,
98- responseType : "arraybuffer" ,
99- } ) ;
87+ const { data } = await api . get ( `/v1/api/uploadFile/${ guid } ` , { responseType : 'arraybuffer' } ) ;
10088
101- const file = new Blob ( [ response . data ] , { type : 'application/pdf' } ) ;
89+ const file = new Blob ( [ data ] , { type : 'application/pdf' } ) ;
10290 const fileURL = window . URL . createObjectURL ( file ) ;
10391 window . open ( fileURL ) ;
10492 } catch ( error ) {
@@ -118,17 +106,24 @@ const ListOfFiles: React.FC<{ showTable?: boolean }> = ({
118106 { Header : 'Date Published' , accessor : 'publication_date' } ,
119107 { Header : '' , accessor : 'file_open' } ,
120108 ] ;
109+
110+ const formatUTCDate = ( dateStr : string | null ) => {
111+ if ( ! dateStr ) return "N/A" ;
112+ const formatter = new Intl . DateTimeFormat ( "en-US" , {
113+ timeZone : "UTC" ,
114+ year : "numeric" ,
115+ month : "numeric" ,
116+ day : "numeric"
117+ } ) ;
118+ const formattedDate = formatter . format ( new Date ( dateStr ) ) ;
119+ return formattedDate ;
120+ }
121+
121122 const data = files . map ( ( file ) => (
122123 {
123124 file_name : file ?. title || file . file_name . replace ( / \. [ ^ / . ] + $ / , "" ) ,
124125 publication : file ?. publication || '' ,
125- publication_date : file . publication_date
126- ? new Intl . DateTimeFormat ( "en-US" , {
127- year : "numeric" ,
128- month : "2-digit" ,
129- day : "2-digit"
130- } ) . format ( new Date ( file . publication_date ) )
131- : "" ,
126+ publication_date : formatUTCDate ( file . publication_date ) ,
132127 file_open :
133128 < a
134129 key = { file . guid }
0 commit comments