warren creation / edit / deletion
This commit is contained in:
104
frontend/components/admin/CreateWarrenDialog.vue
Normal file
104
frontend/components/admin/CreateWarrenDialog.vue
Normal file
@@ -0,0 +1,104 @@
|
||||
<script setup lang="ts">
|
||||
import { toTypedSchema } from '@vee-validate/yup';
|
||||
import { useForm } from 'vee-validate';
|
||||
import { createWarren } from '~/lib/api/admin/createWarren';
|
||||
import { createWarrenSchema } from '~/lib/schemas/admin';
|
||||
|
||||
const adminStore = useAdminStore();
|
||||
const creating = ref(false);
|
||||
|
||||
function close() {
|
||||
adminStore.closeCreateWarrenDialog();
|
||||
form.resetForm();
|
||||
}
|
||||
|
||||
const form = useForm({
|
||||
validationSchema: toTypedSchema(createWarrenSchema),
|
||||
});
|
||||
|
||||
const onSubmit = form.handleSubmit(async (values) => {
|
||||
if (creating.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
creating.value = true;
|
||||
|
||||
const result = await createWarren(values.name, values.path);
|
||||
|
||||
creating.value = false;
|
||||
|
||||
if (result.success) {
|
||||
close();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
<template>
|
||||
<AlertDialog :open="adminStore.createWarrenDialogOpen">
|
||||
<AlertDialogTrigger><slot /></AlertDialogTrigger>
|
||||
<AlertDialogContent @escape-key-down="close">
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>Create warren</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
Enter a name and an absolute file path to create a new
|
||||
warren
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
|
||||
<form
|
||||
id="create-warren-form"
|
||||
class="flex flex-col gap-2"
|
||||
@submit.prevent="onSubmit"
|
||||
>
|
||||
<FormField v-slot="{ componentField }" name="name">
|
||||
<FormItem>
|
||||
<FormLabel>Name</FormLabel>
|
||||
<FormControl>
|
||||
<Input
|
||||
v-bind="componentField"
|
||||
id="name"
|
||||
type="text"
|
||||
placeholder="my-warren"
|
||||
autocomplete="off"
|
||||
data-1p-ignore
|
||||
data-protonpass-ignore
|
||||
data-bwignore
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
</FormField>
|
||||
|
||||
<FormField v-slot="{ componentField }" name="path">
|
||||
<FormItem>
|
||||
<FormLabel>File path</FormLabel>
|
||||
<FormControl>
|
||||
<Input
|
||||
v-bind="componentField"
|
||||
id="path"
|
||||
type="text"
|
||||
placeholder="/mywarren"
|
||||
autocomplete="off"
|
||||
data-1p-ignore
|
||||
data-protonpass-ignore
|
||||
data-bwignore
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
</FormField>
|
||||
</form>
|
||||
|
||||
<AlertDialogFooter class="gap-y-0">
|
||||
<AlertDialogCancel variant="outline" @click="close">
|
||||
Cancel
|
||||
</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
type="submit"
|
||||
form="create-warren-form"
|
||||
:disabled="creating"
|
||||
>Create</AlertDialogAction
|
||||
>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
</template>
|
||||
@@ -64,7 +64,7 @@ async function submit() {
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
|
||||
<AlertDialogDescription class="space-y-1">
|
||||
<p ref="test">
|
||||
<p>
|
||||
This action cannot be undone. This will permanently
|
||||
delete the user and remove their data from the database
|
||||
</p>
|
||||
|
||||
113
frontend/components/admin/DeleteWarrenDialog.vue
Normal file
113
frontend/components/admin/DeleteWarrenDialog.vue
Normal file
@@ -0,0 +1,113 @@
|
||||
<script setup lang="ts">
|
||||
import { Input } from '@/components/ui/input';
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
AlertDialogTrigger,
|
||||
} from '@/components/ui/alert-dialog';
|
||||
import { deleteWarren } from '~/lib/api/admin/deleteWarren';
|
||||
import type { AdminWarrenData } from '~/shared/types/warrens';
|
||||
|
||||
const adminStore = useAdminStore();
|
||||
// We'll only update this value if there is a warren to prevent layout shifts on close
|
||||
const warren = ref<AdminWarrenData>();
|
||||
const deleting = ref(false);
|
||||
|
||||
const confirmPath = ref<string>('');
|
||||
const confirmPathInput = ref<InstanceType<typeof Input>>();
|
||||
|
||||
const pathMatches = computed(
|
||||
() => warren.value != null && warren.value.path === confirmPath.value
|
||||
);
|
||||
|
||||
adminStore.$subscribe(async (_mutation, state) => {
|
||||
if (state.deleteWarrenDialog != null) {
|
||||
warren.value = state.deleteWarrenDialog?.warren;
|
||||
setTimeout(() => confirmPathInput.value?.domRef?.focus(), 25);
|
||||
}
|
||||
});
|
||||
|
||||
function close() {
|
||||
confirmPath.value = '';
|
||||
adminStore.closeDeleteWarrenDialog();
|
||||
}
|
||||
|
||||
async function submit() {
|
||||
if (deleting.value || adminStore.deleteWarrenDialog == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
deleting.value = true;
|
||||
|
||||
const { success } = await deleteWarren(
|
||||
adminStore.deleteWarrenDialog.warren.id
|
||||
);
|
||||
|
||||
if (success) {
|
||||
close();
|
||||
}
|
||||
|
||||
deleting.value = false;
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<AlertDialog :open="adminStore.deleteWarrenDialog != null">
|
||||
<AlertDialogTrigger as-child>
|
||||
<slot />
|
||||
</AlertDialogTrigger>
|
||||
<AlertDialogContent @escape-key-down="close">
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
|
||||
<AlertDialogDescription class="space-y-1">
|
||||
<p>
|
||||
This action cannot be undone. This will permanently
|
||||
delete the warren. The contained files will be left
|
||||
unchanged.
|
||||
</p>
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlterDialogContent v-if="warren != null">
|
||||
<div class="flex flex-col gap-4">
|
||||
<AdminWarrenListing :warren />
|
||||
<div class="flex flex-col gap-1">
|
||||
<p
|
||||
:class="[
|
||||
'tight text-sm',
|
||||
pathMatches
|
||||
? 'text-muted-foreground'
|
||||
: 'text-destructive-foreground',
|
||||
]"
|
||||
>
|
||||
Enter the warren's path to continue
|
||||
</p>
|
||||
<Input
|
||||
ref="confirmPathInput"
|
||||
v-model="confirmPath"
|
||||
type="text"
|
||||
:placeholder="warren.path"
|
||||
autocomplete="off"
|
||||
data-1p-ignore
|
||||
data-protonpass-ignore
|
||||
data-bwignore
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</AlterDialogContent>
|
||||
<AlertDialogFooter class="gap-y-0">
|
||||
<AlertDialogCancel @click="close">Cancel</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
:disabled="!pathMatches || deleting"
|
||||
@click="submit"
|
||||
>Delete</AlertDialogAction
|
||||
>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
</template>
|
||||
158
frontend/components/admin/EditWarrenDialog.vue
Normal file
158
frontend/components/admin/EditWarrenDialog.vue
Normal file
@@ -0,0 +1,158 @@
|
||||
<script setup lang="ts">
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
AlertDialogTrigger,
|
||||
} from '@/components/ui/alert-dialog';
|
||||
import { useForm } from 'vee-validate';
|
||||
import { toTypedSchema } from '@vee-validate/yup';
|
||||
import { editWarrenSchema } from '~/lib/schemas/admin';
|
||||
import { editWarren } from '~/lib/api/admin/editWarren';
|
||||
import type { AdminWarrenData } from '~/shared/types/warrens';
|
||||
|
||||
const adminStore = useAdminStore();
|
||||
|
||||
const isValid = computed(() => Object.keys(form.errors.value).length < 1);
|
||||
|
||||
// We'll only update this value if there is a warren to prevent layout shifts on close
|
||||
const warren = ref<AdminWarrenData>();
|
||||
const editing = ref(false);
|
||||
|
||||
const isChanged = computed(() => {
|
||||
if (warren.value == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
const values = editWarrenSchema.validateSync(
|
||||
form.controlledValues.value
|
||||
);
|
||||
return (
|
||||
values.name !== warren.value.name ||
|
||||
values.path !== warren.value.path
|
||||
);
|
||||
} catch {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
function close() {
|
||||
adminStore.closeEditWarrenDialog();
|
||||
}
|
||||
|
||||
const form = useForm({
|
||||
validationSchema: toTypedSchema(editWarrenSchema),
|
||||
});
|
||||
|
||||
adminStore.$subscribe((_mutation, state) => {
|
||||
if (state.editWarrenDialog != null && !editing.value) {
|
||||
warren.value = Object.values(state.resources.warrens).find(
|
||||
(w) => w.id === state.editWarrenDialog?.warren.id
|
||||
);
|
||||
|
||||
if (warren.value != null) {
|
||||
form.setValues(warren.value);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const onSubmit = form.handleSubmit(async (values) => {
|
||||
if (warren.value == null || !isChanged.value || editing.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
editing.value = true;
|
||||
|
||||
const result = await editWarren(warren.value.id, values.name, values.path);
|
||||
|
||||
if (result.success) {
|
||||
const newWarren: AdminWarrenData = {
|
||||
id: result.warren.id,
|
||||
name: result.warren.name,
|
||||
path: result.warren.path,
|
||||
};
|
||||
adminStore.openEditWarrenDialog(newWarren);
|
||||
}
|
||||
|
||||
editing.value = false;
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<AlertDialog :open="adminStore.editWarrenDialog != null">
|
||||
<AlertDialogTrigger><slot /></AlertDialogTrigger>
|
||||
<AlertDialogContent
|
||||
v-if="warren != null"
|
||||
class="flex max-h-[95vh] flex-col"
|
||||
@escape-key-down="close"
|
||||
>
|
||||
<AlertDialogHeader class="shrink">
|
||||
<AlertDialogTitle>Edit warren</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
Modify the warren's name and path
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
|
||||
<form
|
||||
id="edit-warren-form"
|
||||
class="flex flex-col gap-2"
|
||||
@submit.prevent="onSubmit"
|
||||
>
|
||||
<input type="hidden" name="id" :value="warren.id" />
|
||||
<FormField v-slot="{ componentField }" name="name">
|
||||
<FormItem>
|
||||
<FormLabel>Name</FormLabel>
|
||||
<FormControl>
|
||||
<Input
|
||||
v-bind="componentField"
|
||||
id="name"
|
||||
type="text"
|
||||
placeholder="my-warren"
|
||||
autocomplete="off"
|
||||
data-1p-ignore
|
||||
data-protonpass-ignore
|
||||
data-bwignore
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
</FormField>
|
||||
|
||||
<FormField v-slot="{ componentField }" name="path">
|
||||
<FormItem>
|
||||
<FormLabel>Path</FormLabel>
|
||||
<FormControl>
|
||||
<Input
|
||||
v-bind="componentField"
|
||||
id="path"
|
||||
type="text"
|
||||
placeholder="/mywarren"
|
||||
autocomplete="off"
|
||||
data-1p-ignore
|
||||
data-protonpass-ignore
|
||||
data-bwignore
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
</FormField>
|
||||
</form>
|
||||
|
||||
<AlertDialogFooter class="gap-y-0">
|
||||
<AlertDialogCancel @click="close">Close</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
type="submit"
|
||||
form="edit-warren-form"
|
||||
:disabled="!isChanged || !isValid"
|
||||
>Save</AlertDialogAction
|
||||
>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
</template>
|
||||
Reference in New Issue
Block a user