diff --git a/pages/backup.vue b/pages/backup.vue index 94cf418..5a03de5 100644 --- a/pages/backup.vue +++ b/pages/backup.vue @@ -107,15 +107,17 @@ type ScriptResult = { downloadFolders: string[] } -const selectedBackup = ref(null) -const scriptResult = ref({ - key: null as string | null, +const emptyScriptResult = (): ScriptResult => ({ + key: null, label: "", output: "", isError: false, downloadFolders: [] }) +const selectedBackup = ref(null) +const scriptResult = ref(emptyScriptResult()) + const fetchLatestBackup = async (folder: string) => { const files = await $fetch(`/api/backups?folder=${encodeURIComponent(folder)}`) return files[0] || null @@ -130,8 +132,16 @@ const triggerDownload = (folder: string, file: string) => { link.remove() } +const downloadLatestBackup = async (folder: string) => { + const latestFile = await fetchLatestBackup(folder) + + if (latestFile) { + triggerDownload(folder, latestFile) + } +} + const handleScriptResult = async (payload: ScriptResult) => { - scriptResult.value = payload + scriptResult.value = { ...emptyScriptResult(), ...payload } if (payload.isError || payload.downloadFolders.length === 0) { return @@ -139,10 +149,7 @@ const handleScriptResult = async (payload: ScriptResult) => { for (const folder of payload.downloadFolders) { try { - const latestFile = await fetchLatestBackup(folder) - if (latestFile) { - triggerDownload(folder, latestFile) - } + await downloadLatestBackup(folder) } catch (error) { console.error(`Erreur telechargement automatique pour ${folder}:`, error) }