create dirs + delete dirs + delete files
This commit is contained in:
@@ -1,14 +1,22 @@
|
||||
import type { DirectoryEntry } from '~/types';
|
||||
import { toast } from 'vue-sonner';
|
||||
import type { DirectoryEntry, FileType } from '~/types';
|
||||
import type { Warren } from '~/types/warrens';
|
||||
|
||||
export async function getWarrens(): Promise<Record<string, Warren>> {
|
||||
const arr: Warren[] = await $fetch(getApiUrl('warrens'), {
|
||||
method: 'GET',
|
||||
});
|
||||
const { data: arr, error } = await useFetch<Warren[]>(
|
||||
getApiUrl('warrens'),
|
||||
{
|
||||
method: 'GET',
|
||||
}
|
||||
);
|
||||
|
||||
if (arr.value == null) {
|
||||
throw error.value?.name;
|
||||
}
|
||||
|
||||
const warrens: Record<string, Warren> = {};
|
||||
|
||||
for (const warren of arr) {
|
||||
for (const warren of arr.value) {
|
||||
warrens[warren.id] = warren;
|
||||
}
|
||||
|
||||
@@ -18,12 +26,76 @@ export async function getWarrens(): Promise<Record<string, Warren>> {
|
||||
export async function getWarrenDirectory(
|
||||
path: string
|
||||
): Promise<DirectoryEntry[]> {
|
||||
const entries: DirectoryEntry[] = await $fetch(
|
||||
const { data: entries, error } = await useFetch<DirectoryEntry[]>(
|
||||
getApiUrl(`warrens/${path}`),
|
||||
{
|
||||
method: 'GET',
|
||||
}
|
||||
);
|
||||
|
||||
return entries;
|
||||
if (entries.value == null) {
|
||||
throw error.value?.name;
|
||||
}
|
||||
|
||||
return entries.value;
|
||||
}
|
||||
|
||||
export async function createDirectory(
|
||||
path: string,
|
||||
directoryName: string
|
||||
): Promise<{ success: boolean }> {
|
||||
const { status } = await useFetch(
|
||||
getApiUrl(`warrens/${path}/${directoryName}`),
|
||||
{
|
||||
method: 'POST',
|
||||
}
|
||||
);
|
||||
|
||||
if (status.value !== 'success') {
|
||||
toast.error('Directory', {
|
||||
id: 'CREATE_DIRECTORY_TOAST',
|
||||
description: `Failed to create directory`,
|
||||
});
|
||||
return { success: false };
|
||||
}
|
||||
|
||||
await refreshNuxtData('current-directory');
|
||||
|
||||
toast.success('Directory', {
|
||||
id: 'CREATE_DIRECTORY_TOAST',
|
||||
description: `Successfully created directory: ${directoryName}`,
|
||||
});
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
export async function deleteWarrenEntry(
|
||||
path: string,
|
||||
directoryName: string,
|
||||
fileType: FileType
|
||||
): Promise<{ success: boolean }> {
|
||||
const { status } = await useFetch(
|
||||
getApiUrl(`warrens/${path}/${directoryName}?fileType=${fileType}`),
|
||||
{
|
||||
method: 'DELETE',
|
||||
}
|
||||
);
|
||||
|
||||
const toastTitle = fileType.slice(0, 1).toUpperCase() + fileType.slice(1);
|
||||
|
||||
if (status.value !== 'success') {
|
||||
toast.error(toastTitle, {
|
||||
id: 'DELETE_DIRECTORY_TOAST',
|
||||
description: `Failed to delete ${fileType}`,
|
||||
});
|
||||
return { success: false };
|
||||
}
|
||||
|
||||
await refreshNuxtData('current-directory');
|
||||
|
||||
toast.success(toastTitle, {
|
||||
id: 'DELETE_DIRECTORY_TOAST',
|
||||
description: `Successfully deleted ${fileType}: ${directoryName}`,
|
||||
});
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user