warren creation / edit / deletion
This commit is contained in:
43
frontend/lib/api/admin/createWarren.ts
Normal file
43
frontend/lib/api/admin/createWarren.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import type { ApiResponse } from '~/shared/types/api';
|
||||
import type { AdminWarrenData } from '~/shared/types/warrens';
|
||||
import { getApiHeaders } from '..';
|
||||
import { toast } from 'vue-sonner';
|
||||
|
||||
export async function createWarren(
|
||||
name: string,
|
||||
path: string
|
||||
): Promise<{ success: true; warren: AdminWarrenData } | { success: false }> {
|
||||
const { data, error } = await useFetch<ApiResponse<AdminWarrenData>>(
|
||||
getApiUrl('admin/warrens'),
|
||||
{
|
||||
method: 'POST',
|
||||
headers: getApiHeaders(),
|
||||
body: JSON.stringify({
|
||||
name: name,
|
||||
path: path,
|
||||
}),
|
||||
responseType: 'json',
|
||||
}
|
||||
);
|
||||
|
||||
if (data.value == null) {
|
||||
toast.error('Create warren', {
|
||||
description: error.value?.data ?? 'Failed to create warren',
|
||||
});
|
||||
|
||||
return {
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
|
||||
await refreshNuxtData('admin-resources');
|
||||
|
||||
toast.success('Create warren', {
|
||||
description: 'Successfully created warren',
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
warren: data.value.data,
|
||||
};
|
||||
}
|
||||
41
frontend/lib/api/admin/deleteWarren.ts
Normal file
41
frontend/lib/api/admin/deleteWarren.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import type { ApiResponse } from '~/shared/types/api';
|
||||
import type { AdminWarrenData } from '~/shared/types/warrens';
|
||||
import { getApiHeaders } from '..';
|
||||
import { toast } from 'vue-sonner';
|
||||
|
||||
export async function deleteWarren(
|
||||
warrenId: string
|
||||
): Promise<{ success: true; warren: AdminWarrenData } | { success: false }> {
|
||||
const { data, error } = await useFetch<ApiResponse<AdminWarrenData>>(
|
||||
getApiUrl('admin/warrens'),
|
||||
{
|
||||
method: 'DELETE',
|
||||
headers: getApiHeaders(),
|
||||
body: JSON.stringify({
|
||||
id: warrenId,
|
||||
}),
|
||||
responseType: 'json',
|
||||
}
|
||||
);
|
||||
|
||||
if (data.value == null) {
|
||||
toast.error('Delete warren', {
|
||||
description: error.value?.data ?? 'Failed to delete warren',
|
||||
});
|
||||
|
||||
return {
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
|
||||
await refreshNuxtData(['warrens', 'admin-resources']);
|
||||
|
||||
toast.success('Delete warren', {
|
||||
description: 'Successfully deleted warren',
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
warren: data.value.data,
|
||||
};
|
||||
}
|
||||
45
frontend/lib/api/admin/editWarren.ts
Normal file
45
frontend/lib/api/admin/editWarren.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import type { ApiResponse } from '~/shared/types/api';
|
||||
import type { AdminWarrenData } from '~/shared/types/warrens';
|
||||
import { getApiHeaders } from '..';
|
||||
import { toast } from 'vue-sonner';
|
||||
|
||||
export async function editWarren(
|
||||
warrenId: string,
|
||||
name: string,
|
||||
path: string
|
||||
): Promise<{ success: true; warren: AdminWarrenData } | { success: false }> {
|
||||
const { data, error } = await useFetch<ApiResponse<AdminWarrenData>>(
|
||||
getApiUrl('admin/warrens'),
|
||||
{
|
||||
method: 'PATCH',
|
||||
headers: getApiHeaders(),
|
||||
body: JSON.stringify({
|
||||
id: warrenId,
|
||||
name: name,
|
||||
path: path,
|
||||
}),
|
||||
responseType: 'json',
|
||||
}
|
||||
);
|
||||
|
||||
if (data.value == null) {
|
||||
toast.error('Edit warren', {
|
||||
description: error.value?.data ?? 'Failed to edit warren',
|
||||
});
|
||||
|
||||
return {
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
|
||||
await refreshNuxtData('admin-resources');
|
||||
|
||||
toast.success('Edit warren', {
|
||||
description: 'Successfully editd warren',
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
warren: data.value.data,
|
||||
};
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
export async function logout() {
|
||||
useAuthSession().value = null;
|
||||
useAdminStore().$reset();
|
||||
await navigateTo({
|
||||
path: '/login',
|
||||
});
|
||||
|
||||
@@ -26,3 +26,13 @@ export const userWarrenSchema = object({
|
||||
canModifyFiles: boolean().required(),
|
||||
canDeleteFiles: boolean().required(),
|
||||
});
|
||||
|
||||
export const createWarrenSchema = object({
|
||||
name: string().min(1).required('required'),
|
||||
path: string().min(1).required('required'),
|
||||
});
|
||||
|
||||
export const editWarrenSchema = object({
|
||||
name: string().min(1).required('required'),
|
||||
path: string().min(1).required('required'),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user