2021-08-21 08:46:43 +00:00
|
|
|
import { useAsync, ref, reactive } from "@nuxtjs/composition-api";
|
2021-08-07 23:12:25 +00:00
|
|
|
import { toastLoading, loader } from "./use-toast";
|
2021-08-21 08:46:43 +00:00
|
|
|
import { AllBackups, ImportBackup } from "~/api/class-interfaces/backups";
|
2021-11-06 19:28:47 +00:00
|
|
|
import { useUserApi } from "~/composables/api";
|
2021-08-07 23:12:25 +00:00
|
|
|
|
|
|
|
const backups = ref<AllBackups>({
|
|
|
|
imports: [],
|
|
|
|
templates: [],
|
|
|
|
});
|
|
|
|
|
|
|
|
function setBackups(newBackups: AllBackups | null) {
|
|
|
|
if (newBackups) {
|
2022-01-16 02:38:11 +00:00
|
|
|
backups.value = newBackups;
|
2021-08-07 23:12:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-21 08:46:43 +00:00
|
|
|
function optionsFactory() {
|
|
|
|
return {
|
|
|
|
tag: "",
|
|
|
|
templates: [],
|
|
|
|
options: {
|
|
|
|
recipes: true,
|
|
|
|
settings: true,
|
|
|
|
themes: true,
|
|
|
|
pages: true,
|
|
|
|
users: true,
|
|
|
|
groups: true,
|
|
|
|
notifications: true,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-08-07 23:12:25 +00:00
|
|
|
export const useBackups = function (fetch = true) {
|
2021-11-06 19:28:47 +00:00
|
|
|
const api = useUserApi();
|
2021-08-07 23:12:25 +00:00
|
|
|
|
2021-08-21 08:46:43 +00:00
|
|
|
const backupOptions = reactive(optionsFactory());
|
|
|
|
const deleteTarget = ref("");
|
|
|
|
|
|
|
|
const selected = ref<ImportBackup | null>({
|
|
|
|
name: "",
|
|
|
|
options: {
|
|
|
|
recipes: true,
|
|
|
|
settings: true,
|
|
|
|
pages: true,
|
|
|
|
themes: true,
|
|
|
|
groups: true,
|
|
|
|
users: true,
|
|
|
|
notifications: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-08-07 23:12:25 +00:00
|
|
|
function getBackups() {
|
|
|
|
const backups = useAsync(async () => {
|
|
|
|
const { data } = await api.backups.getAll();
|
|
|
|
return data;
|
|
|
|
});
|
|
|
|
return backups;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function refreshBackups() {
|
|
|
|
const { data } = await api.backups.getAll();
|
|
|
|
if (data) {
|
|
|
|
setBackups(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-21 08:46:43 +00:00
|
|
|
async function createBackup() {
|
2021-08-07 23:12:25 +00:00
|
|
|
loader.info("Creating Backup...");
|
2021-08-21 08:46:43 +00:00
|
|
|
const { response } = await api.backups.createOne(backupOptions);
|
2021-08-07 23:12:25 +00:00
|
|
|
|
|
|
|
if (response && response.status === 201) {
|
|
|
|
refreshBackups();
|
|
|
|
toastLoading.open = false;
|
2021-08-21 08:46:43 +00:00
|
|
|
Object.assign(backupOptions, optionsFactory());
|
2021-08-07 23:12:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-21 08:46:43 +00:00
|
|
|
async function deleteBackup() {
|
|
|
|
const { response } = await api.backups.deleteOne(deleteTarget.value);
|
2021-08-07 23:12:25 +00:00
|
|
|
if (response && response.status === 200) {
|
|
|
|
refreshBackups();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-21 08:46:43 +00:00
|
|
|
async function importBackup() {
|
2021-08-07 23:12:25 +00:00
|
|
|
loader.info("Import Backup...");
|
2021-08-21 08:46:43 +00:00
|
|
|
|
|
|
|
if (!selected.value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { response } = await api.backups.restoreDatabase(selected.value.name, selected.value.options);
|
2021-08-07 23:12:25 +00:00
|
|
|
|
|
|
|
if (response && response.status === 200) {
|
|
|
|
refreshBackups();
|
|
|
|
loader.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fetch) {
|
|
|
|
refreshBackups();
|
|
|
|
}
|
|
|
|
|
2021-08-21 08:46:43 +00:00
|
|
|
return {
|
|
|
|
getBackups,
|
|
|
|
refreshBackups,
|
|
|
|
deleteBackup,
|
|
|
|
importBackup,
|
|
|
|
createBackup,
|
|
|
|
backups,
|
|
|
|
backupOptions,
|
|
|
|
deleteTarget,
|
|
|
|
selected,
|
|
|
|
};
|
2021-08-07 23:12:25 +00:00
|
|
|
};
|