mirror of
				https://github.com/mealie-recipes/mealie.git
				synced 2025-10-29 01:04:18 -04:00 
			
		
		
		
	* add vitest * initialize lib w/ tests * move to dev dep * run tests in CI * update file names * move api folder to lib * move api and api types to same folder * update generator outpath * rm husky * i guess i _did_ need those types * reorg types * extract validators into testable components * (WIP) start composable testing * fix import type * fix linter complaint * simplify icon type def * fix linter errors (maybe?) * rename client file for sorting
		
			
				
	
	
		
			120 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			120 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import { useAsync, ref, reactive } from "@nuxtjs/composition-api";
 | |
| import { toastLoading, loader } from "./use-toast";
 | |
| import { 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 = 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);
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   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,
 | |
|   };
 | |
| };
 |