import { defineStore } from "pinia"; import { http } from "@/serverCom"; import type { AxiosResponse, AxiosProgressEvent } from "axios"; import type { BackupRestoreViewModel } from "../../../viewmodels/admin/user/backup.models"; export const useBackupStore = defineStore("backup", { state: () => { return { backups: [] as Array, loading: null as null | "loading" | "success" | "failed", }; }, actions: { fetchBackups() { this.loading = "loading"; http .get("/admin/backup") .then((result) => { this.backups = result.data; this.loading = "success"; }) .catch((err) => { this.loading = "failed"; }); }, fetchBackupById(filename: string): Promise> { return http.get(`/admin/backup/${filename}`); }, async triggerBackupCreate(): Promise> { const result = await http.post("/admin/backup"); this.fetchBackups(); return result; }, async restoreBackup(backup: BackupRestoreViewModel): Promise> { return await http.post("/admin/backup/restore", backup); }, async uploadBackup(file: File): Promise> { const formData = new FormData(); formData.append("file", file); const options = { headers: { "Content-Type": "multipart/form-data", }, onUploadProgress: (progressEvent: AxiosProgressEvent) => { const { loaded, total = 1 } = progressEvent; console.log("progress", Math.floor((loaded * 100) / total)); }, }; const result = await http.post("/admin/backup/upload", formData, options); this.fetchBackups(); return result; }, }, });