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

View File

@ -1,16 +1,25 @@
<template>
<ModalTemplate v-model="open">
<template #default>
<DialogTitle as="h3" class="text-lg font-medium leading-6 text-white">
Create Collection
</DialogTitle>
<div>
<DialogTitle as="h3" class="text-lg font-medium leading-6 text-white">
Create collection
</DialogTitle>
<p class="mt-1 text-zinc-400 text-sm">
Collections can used to organise your games and find them more easily,
especially if you have a large library.
</p>
</div>
<div class="mt-2">
<input
type="text"
v-model="collectionName"
placeholder="Collection name"
class="block w-full rounded-md border-0 bg-zinc-800 py-1.5 text-white shadow-sm ring-1 ring-inset ring-zinc-700 placeholder:text-zinc-400 focus:ring-2 focus:ring-inset focus:ring-blue-600 sm:text-sm sm:leading-6"
/>
<form @submit.prevent="() => createCollection()">
<input
type="text"
v-model="collectionName"
placeholder="Collection name"
class="block w-full rounded-md border-0 bg-zinc-800 py-1.5 text-white shadow-sm ring-1 ring-inset ring-zinc-700 placeholder:text-zinc-400 focus:ring-2 focus:ring-inset focus:ring-blue-600 sm:text-sm sm:leading-6"
/>
<button class="hidden" type="submit" />
</form>
</div>
</template>

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>

View File

@ -35,8 +35,7 @@
<!-- Delete button (only show for non-default collections) -->
<button
v-if="!collection.isDefault"
@click=""
@click="() => (currentlyDeleting = collection)"
class="px-3 ml-[2px] bg-zinc-800/50 hover:bg-zinc-800"
>
<TrashIcon class="h-5 w-5 text-zinc-400 hover:text-red-400" />
@ -57,7 +56,9 @@
Create Collection
</h3>
</div>
<p class="transition mt-1 text-sm text-zinc-500 group-hover:text-zinc-400">
<p
class="transition mt-1 text-sm text-zinc-500 group-hover:text-zinc-400"
>
Add a new collection to organize your games
</p>
</button>
@ -66,6 +67,7 @@
</div>
<CreateCollectionModal v-model="collectionCreateOpen" />
<DeleteCollectionModal v-model="currentlyDeleting" />
</template>
<script setup lang="ts">
@ -75,18 +77,19 @@ import {
TrashIcon,
ArrowLeftIcon,
} from "@heroicons/vue/20/solid";
import { type Game, type GameVersion } from "@prisma/client";
import { type Collection, type Game, type GameVersion } from "@prisma/client";
import { PlusIcon } from "@heroicons/vue/20/solid";
const headers = useRequestHeaders(["cookie"]);
const { data: gamesData } = await useFetch<
(Game & { versions: GameVersion[] })[]
>("/api/v1/store/recent", { headers });
const games = ref(gamesData.value || []);
const collections = await useCollections();
const collectionCreateOpen = ref(false);
const currentlyDeleting = ref<Collection | undefined>();
useHead({
title: "Home",
});