refactor: simplify backup result handling
This commit is contained in:
@@ -107,15 +107,17 @@ type ScriptResult = {
|
|||||||
downloadFolders: string[]
|
downloadFolders: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
const selectedBackup = ref<string | null>(null)
|
const emptyScriptResult = (): ScriptResult => ({
|
||||||
const scriptResult = ref<ScriptResult>({
|
key: null,
|
||||||
key: null as string | null,
|
|
||||||
label: "",
|
label: "",
|
||||||
output: "",
|
output: "",
|
||||||
isError: false,
|
isError: false,
|
||||||
downloadFolders: []
|
downloadFolders: []
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const selectedBackup = ref<string | null>(null)
|
||||||
|
const scriptResult = ref<ScriptResult>(emptyScriptResult())
|
||||||
|
|
||||||
const fetchLatestBackup = async (folder: string) => {
|
const fetchLatestBackup = async (folder: string) => {
|
||||||
const files = await $fetch<string[]>(`/api/backups?folder=${encodeURIComponent(folder)}`)
|
const files = await $fetch<string[]>(`/api/backups?folder=${encodeURIComponent(folder)}`)
|
||||||
return files[0] || null
|
return files[0] || null
|
||||||
@@ -130,8 +132,16 @@ const triggerDownload = (folder: string, file: string) => {
|
|||||||
link.remove()
|
link.remove()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const downloadLatestBackup = async (folder: string) => {
|
||||||
|
const latestFile = await fetchLatestBackup(folder)
|
||||||
|
|
||||||
|
if (latestFile) {
|
||||||
|
triggerDownload(folder, latestFile)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const handleScriptResult = async (payload: ScriptResult) => {
|
const handleScriptResult = async (payload: ScriptResult) => {
|
||||||
scriptResult.value = payload
|
scriptResult.value = { ...emptyScriptResult(), ...payload }
|
||||||
|
|
||||||
if (payload.isError || payload.downloadFolders.length === 0) {
|
if (payload.isError || payload.downloadFolders.length === 0) {
|
||||||
return
|
return
|
||||||
@@ -139,10 +149,7 @@ const handleScriptResult = async (payload: ScriptResult) => {
|
|||||||
|
|
||||||
for (const folder of payload.downloadFolders) {
|
for (const folder of payload.downloadFolders) {
|
||||||
try {
|
try {
|
||||||
const latestFile = await fetchLatestBackup(folder)
|
await downloadLatestBackup(folder)
|
||||||
if (latestFile) {
|
|
||||||
triggerDownload(folder, latestFile)
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Erreur telechargement automatique pour ${folder}:`, error)
|
console.error(`Erreur telechargement automatique pour ${folder}:`, error)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user