From d6d7738148cbac4a256f1bf70715f869bb0d0d34 Mon Sep 17 00:00:00 2001
From: Gagaro <gagaro42@gmail.com>
Date: Wed, 16 Nov 2022 16:24:19 +0100
Subject: [PATCH] =?UTF-8?q?Fix=20#17:=20Ajout=20du=20bouton=20de=20t=C3=A9?=
 =?UTF-8?q?l=C3=A9chargement=20des=20logs=20de=20la=20trajectoire?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/pages/DetailTrajectory.tsx | 43 +++++++++++++++++++++++++++++-----
 1 file changed, 37 insertions(+), 6 deletions(-)

diff --git a/src/pages/DetailTrajectory.tsx b/src/pages/DetailTrajectory.tsx
index 29429ff5b..5cec408de 100644
--- a/src/pages/DetailTrajectory.tsx
+++ b/src/pages/DetailTrajectory.tsx
@@ -34,7 +34,8 @@ import useTrajectory from '../hooks/useTrajectory';
 export const DetailTrajectory = () => {
   const { t } = useTranslation();
   const { keycloak } = useKeycloak();
-  const [busyDataQuery, setBusyDataQuery] = useState<boolean>(false);
+  const [busyResultsDataQuery, setBusyResultsDataQuery] = useState<boolean>(false);
+  const [busyLogsDataQuery, setBusyLogsDataQuery] = useState<boolean>(false);
 
   const { id } = useParams();
   const { isLoading, trajectory } = useTrajectory(id);
@@ -78,14 +79,12 @@ export const DetailTrajectory = () => {
   }
 
   const downloadResults = async () => {
-    setBusyDataQuery(true);
+    setBusyResultsDataQuery(true);
 
     const res = await fetch(
       `${import.meta.env.VITE_API_HOST}/trajectories/${id}/get_tdumps/`,
       {
         headers: {
-          Accept: 'application/json',
-          'Content-Type': 'application/json',
           Authorization: `Bearer ${keycloak.token}`,
         },
       },
@@ -99,7 +98,30 @@ export const DetailTrajectory = () => {
     a.click();
     a.parentElement?.removeChild(a);
 
-    setBusyDataQuery(false);
+    setBusyResultsDataQuery(false);
+  };
+
+  const downloadLogs = async () => {
+    setBusyLogsDataQuery(true);
+
+    const res = await fetch(
+      `${import.meta.env.VITE_API_HOST}/trajectories/${id}/get_logs/`,
+      {
+        headers: {
+          Authorization: `Bearer ${keycloak.token}`,
+        },
+      },
+    );
+
+    // Create a blob of the data
+    const blob = await res.blob();
+    const a = document.createElement('a');
+    a.href = URL.createObjectURL(blob);
+    a.download = `trajectory_logs_${trajectory.slug}.tar.gz`
+    a.click();
+    a.parentElement?.removeChild(a);
+
+    setBusyLogsDataQuery(false);
   };
 
   return (
@@ -228,13 +250,22 @@ export const DetailTrajectory = () => {
           <Paper sx={{ px: 2, py: 1 }}>
             <Box>
               <LoadingButton
-                loading={busyDataQuery}
+                loading={busyResultsDataQuery}
                 variant="outlined"
                 onClick={() => downloadResults()}
                 sx={{ textTransform: 'none' }}
               >
                 {t('Download all results')}
               </LoadingButton>
+              {' '}
+              <LoadingButton
+                loading={busyLogsDataQuery}
+                variant="outlined"
+                onClick={() => downloadLogs()}
+                sx={{ textTransform: 'none' }}
+              >
+                {t('Download all logs')}
+              </LoadingButton>
             </Box>
           </Paper>
         </>
-- 
GitLab