ff-admin/src/stores/admin/user/backup.ts

57 lines
1.8 KiB
TypeScript
Raw Normal View History

2025-02-02 16:37:46 +01:00
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<string>,
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<AxiosResponse<any, any>> {
return http.get(`/admin/backup/${filename}`);
},
async triggerBackupCreate(): Promise<AxiosResponse<any, any>> {
const result = await http.post("/admin/backup");
this.fetchBackups();
return result;
},
async restoreBackup(backup: BackupRestoreViewModel): Promise<AxiosResponse<any, any>> {
return await http.post("/admin/backup/restore", backup);
},
async uploadBackup(file: File): Promise<AxiosResponse<any, any>> {
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;
},
},
});