feat: collection deleting

This commit is contained in:
DecDuck
2025-01-28 16:50:57 +11:00
parent 7c1dec9401
commit 42ebbf2922
3 changed files with 62 additions and 64 deletions
+36 -50
View File
@@ -1,88 +1,74 @@
<template>
<ModalTemplate>
<ModalTemplate :modelValue="!!collection">
<template #default>
<DialogTitle as="h3" class="text-lg font-bold font-display text-zinc-100">
Delete Collection
</DialogTitle>
<div class="mt-2">
<p class="text-sm text-zinc-400">
Are you sure you want to delete "{{ collection?.name }}"? This action
cannot be undone.
<div>
<DialogTitle
as="h3"
class="text-lg font-bold font-display text-zinc-100"
>
Delete Collection
</DialogTitle>
<p class="mt-1 text-sm text-zinc-400">
Are you sure you want to delete "{{ collection?.name }}"?
</p>
<p class="mt-2 text-sm font-bold text-red-500">
This action cannot be undone.
</p>
</div>
</template>
<template #buttons="{ close }">
<button
@click="() => close()"
class="inline-flex items-center rounded-md bg-zinc-800 px-3 py-2 text-sm font-semibold font-display text-white hover:bg-zinc-700"
>
Cancel
</button>
<template #buttons>
<LoadingButton
:loading="deleteLoading"
@click="() => handleDelete()"
@click="() => deleteCollection()"
class="bg-red-600 text-white hover:bg-red-500"
>
Delete
</LoadingButton>
<button
@click="() => (collection = undefined)"
class="inline-flex items-center rounded-md bg-zinc-800 px-3 py-2 text-sm font-semibold font-display text-white hover:bg-zinc-700"
>
Cancel
</button>
</template>
</ModalTemplate>
</template>
<script setup lang="ts">
import type { Collection } from "@prisma/client";
import {
TransitionRoot,
TransitionChild,
Dialog,
DialogPanel,
DialogTitle,
} from "@headlessui/vue";
import { DialogTitle } from "@headlessui/vue";
const props = defineProps<{
collection: Collection | null;
}>();
const emit = defineEmits<{
deleted: [collectionId: string];
}>();
const open = defineModel<boolean>();
const collection = defineModel<Collection | undefined>();
const deleteLoading = ref(false);
const collections = await useCollections();
const handleDelete = async () => {
if (!props.collection) return;
deleteLoading.value = true;
async function deleteCollection() {
try {
await $fetch(`/api/v1/collection/${props.collection.id}`, {
if (!collection.value) return;
deleteLoading.value = true;
await $fetch(`/api/v1/collection/${collection.value.id}`, {
// @ts-ignore
method: "DELETE",
});
collections.value.splice(
collections.value.findIndex((e) => e.id === props.collection?.id),
1
const index = collections.value.findIndex(
(e) => e.id == collection.value?.id
);
collections.value.splice(index, 1);
open.value = false;
emit("deleted", props.collection.id);
} catch (error) {
console.error("Failed to delete collection:", error);
const err = error as { statusMessage?: string };
collection.value = undefined;
} catch (e: any) {
createModal(
ModalType.Notification,
{
title: "Failed to create collection",
description: `Drop couldn't create your collection: ${err?.statusMessage}`,
title: "Failed to add game to library",
description: `Drop couldn't add this game to your library: ${e?.statusMessage}`,
},
(_, c) => c()
);
} finally {
deleteLoading.value = false;
}
};
}
</script>