From 6c7ce6b404f86d83533f28d3c564cda1e3b8a102 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Buget?= Date: Mon, 11 Sep 2023 23:22:06 +0200 Subject: [PATCH] Add thread deletion --- app.vue | 2 +- components/ThreadEditor.vue | 29 ++++++++++++++++--- docker-compose.yml | 9 ------ .../20230906200241_init/migration.sql | 2 +- prisma/schema.prisma | 2 +- server/api/threads/[id].delete.ts | 15 ++++++++++ .../api/threads/{[id]/get.ts => [id].get.ts} | 6 ++-- .../threads/{[id]/post.ts => [id].post.ts} | 6 ++-- server/api/threads/[id]/delete.ts | 7 ----- server/api/threads/[id]/publication.post.ts | 6 ++-- utils/utils.ts | 9 ++++-- 11 files changed, 59 insertions(+), 34 deletions(-) create mode 100644 server/api/threads/[id].delete.ts rename server/api/threads/{[id]/get.ts => [id].get.ts} (76%) rename server/api/threads/{[id]/post.ts => [id].post.ts} (91%) delete mode 100644 server/api/threads/[id]/delete.ts diff --git a/app.vue b/app.vue index c23a854..7573f99 100644 --- a/app.vue +++ b/app.vue @@ -36,7 +36,7 @@ function newThread() {
- +
diff --git a/components/ThreadEditor.vue b/components/ThreadEditor.vue index 7db0478..f5d0aa2 100644 --- a/components/ThreadEditor.vue +++ b/components/ThreadEditor.vue @@ -12,14 +12,14 @@ const props = defineProps<{ threadId: number | null }>() -const emit = defineEmits(['thread-saved']) +const emit = defineEmits(['thread-saved', 'thread-deleted']) const { data: thread, pending } = await useAsyncData( 'thread', async () => { let thread: Thread if (props.threadId) { - const threadData = await $fetch(`/api/threads/${props.threadId}`, {}) + const threadData:any = await $fetch(`/api/threads/${props.threadId}`) thread = { id: threadData.id, messages: threadData.latest.data.messages.map((message: any) => { @@ -79,9 +79,11 @@ async function doSaveThread(thread: Thread): Promise { } const response: any = await $fetch(url, { method: 'post', body: { messages: thread.messages } }) emit('thread-saved') + updateKey.value = generateUniqueKey('message-list') return response.id as number } + async function saveThread(): Promise { if (thread.value && thread.value.messages) { thread.value.id = await doSaveThread(thread.value) @@ -98,6 +100,22 @@ async function publishThread(): Promise { } } +async function deleteThread(): Promise { + if (thread.value && thread.value.id) { + const url = `/api/threads/${thread.value.id}` + await $fetch(url, { method: 'delete' }) + emit('thread-deleted') + toast.add({ severity: 'success', summary: 'Thread deleted', detail: `Removed thread ${thread.value.id} and its messages`, life: 3000 }); + } + thread.value = { + messages: [{ + text: '', + attachments: [] + }] + } + updateKey.value = generateUniqueKey('message-list') +} +