57 lines
1.8 KiB
TypeScript
57 lines
1.8 KiB
TypeScript
|
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;
|
||
|
},
|
||
|
},
|
||
|
});
|