mealie/frontend/composables/use-backups.ts
Hoa (Kyle) Trinh c24d532608
Some checks are pending
CodeQL / Analyze (push) Waiting to run
Docker Nightly Production / Backend Server Tests (push) Waiting to run
Docker Nightly Production / Frontend Tests (push) Waiting to run
Docker Nightly Production / Build Package (push) Waiting to run
Docker Nightly Production / Build Tagged Release (push) Blocked by required conditions
Docker Nightly Production / Notify Discord (push) Blocked by required conditions
Release Drafter / ✏️ Draft release (push) Waiting to run
feat: Migrate to Nuxt 3 framework (#5184)
Co-authored-by: Michael Genson <71845777+michael-genson@users.noreply.github.com>
Co-authored-by: Kuchenpirat <24235032+Kuchenpirat@users.noreply.github.com>
2025-06-19 17:09:12 +00:00

118 lines
2.4 KiB
TypeScript

import { toastLoading, loader } from "./use-toast";
import type { AllBackups, BackupOptions } from "~/lib/api/types/admin";
import { useUserApi } from "~/composables/api";
interface ImportBackup {
name: string;
options: BackupOptions;
}
const backups = ref<AllBackups>({
imports: [],
templates: [],
});
function setBackups(newBackups: AllBackups | null) {
if (newBackups) {
backups.value = newBackups;
}
}
function optionsFactory() {
return {
tag: "",
templates: [],
options: {
recipes: true,
settings: true,
themes: true,
pages: true,
users: true,
groups: true,
notifications: true,
},
};
}
export const useBackups = function (fetch = true) {
const api = useUserApi();
const backupOptions = reactive(optionsFactory());
const deleteTarget = ref("");
const selected = ref<ImportBackup | null>({
name: "",
options: {
recipes: true,
settings: true,
themes: true,
groups: true,
users: true,
notifications: true,
},
});
function getBackups() {
const backups = useAsyncData(async () => {
const { data } = await api.backups.getAll();
return data;
});
return backups;
}
async function refreshBackups() {
const { data } = await api.backups.getAll();
if (data) {
setBackups(data);
}
}
async function createBackup() {
loader.info("Creating Backup...");
const { response } = await api.backups.createOne(backupOptions);
if (response && response.status === 201) {
refreshBackups();
toastLoading.open = false;
Object.assign(backupOptions, optionsFactory());
}
}
async function deleteBackup() {
const { response } = await api.backups.deleteOne(deleteTarget.value);
if (response && response.status === 200) {
refreshBackups();
}
}
async function importBackup() {
loader.info("Import Backup...");
if (!selected.value) {
return;
}
const { response } = await api.backups.restoreDatabase(selected.value.name, selected.value.options);
if (response && response.status === 200) {
refreshBackups();
loader.close();
}
}
if (fetch) {
refreshBackups();
}
return {
getBackups,
refreshBackups,
deleteBackup,
importBackup,
createBackup,
backups,
backupOptions,
deleteTarget,
selected,
};
};