nsarrazin HF Staff commited on
Commit
23637ad
·
unverified ·
1 Parent(s): 99ce0c3

feat(api): replace form-based branch deletion with fetch API (#1694)

Browse files

* feat(chat): replace form-based branch deletion with fetch API

* fix: error display

src/lib/components/chat/Alternatives.svelte CHANGED
@@ -4,8 +4,12 @@
4
  import CarbonChevronLeft from "~icons/carbon/chevron-left";
5
  import CarbonChevronRight from "~icons/carbon/chevron-right";
6
 
7
- import { enhance } from "$app/forms";
8
  import { createEventDispatcher } from "svelte";
 
 
 
 
 
9
 
10
  interface Props {
11
  message: Message;
@@ -45,22 +49,26 @@
45
  >
46
  <CarbonChevronRight class="text-sm" />
47
  </button>
48
- {#if !loading && message.children}<form
49
- method="POST"
50
- action="?/deleteBranch"
51
  class="hidden group-hover/navbranch:block"
52
- use:enhance={({ cancel }) => {
53
- if (!confirm("Are you sure you want to delete this branch?")) {
54
- cancel();
55
- }
 
 
 
 
 
 
56
  }}
57
  >
58
- <input name="messageId" value={message.id} type="hidden" />
59
- <button
60
  class="flex items-center justify-center text-xs text-gray-400 hover:text-gray-800 dark:text-gray-500 dark:hover:text-gray-200"
61
- type="submit"
62
- ><CarbonTrashCan />
63
- </button>
64
- </form>
65
  {/if}
66
  </div>
 
4
  import CarbonChevronLeft from "~icons/carbon/chevron-left";
5
  import CarbonChevronRight from "~icons/carbon/chevron-right";
6
 
 
7
  import { createEventDispatcher } from "svelte";
8
+ import { base } from "$app/paths";
9
+ import { page } from "$app/state";
10
+ import { error } from "$lib/stores/errors";
11
+ import { invalidate } from "$app/navigation";
12
+ import { UrlDependency } from "$lib/types/UrlDependency";
13
 
14
  interface Props {
15
  message: Message;
 
49
  >
50
  <CarbonChevronRight class="text-sm" />
51
  </button>
52
+ {#if !loading && message.children}
53
+ <button
 
54
  class="hidden group-hover/navbranch:block"
55
+ onclick={() => {
56
+ fetch(`${base}/api/conversation/${page.params.id}/message/${message.id}`, {
57
+ method: "DELETE",
58
+ }).then(async (r) => {
59
+ if (r.ok) {
60
+ await invalidate(UrlDependency.Conversation);
61
+ } else {
62
+ $error = (await r.json()).message;
63
+ }
64
+ });
65
  }}
66
  >
67
+ <div
 
68
  class="flex items-center justify-center text-xs text-gray-400 hover:text-gray-800 dark:text-gray-500 dark:hover:text-gray-200"
69
+ >
70
+ <CarbonTrashCan />
71
+ </div>
72
+ </button>
73
  {/if}
74
  </div>
src/routes/api/conversation/[id]/message/[messageId]/+server.ts ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { authCondition } from "$lib/server/auth";
2
+ import { collections } from "$lib/server/database";
3
+ import { error } from "@sveltejs/kit";
4
+ import { ObjectId } from "mongodb";
5
+
6
+ export async function DELETE({ locals, params }) {
7
+ const messageId = params.messageId;
8
+
9
+ if (!messageId || typeof messageId !== "string") {
10
+ error(400, "Invalid message id");
11
+ }
12
+
13
+ const conversation = await collections.conversations.findOne({
14
+ ...authCondition(locals),
15
+ _id: new ObjectId(params.id),
16
+ });
17
+
18
+ if (!conversation) {
19
+ error(404, "Conversation not found");
20
+ }
21
+
22
+ const filteredMessages = conversation.messages
23
+ .filter(
24
+ (message) =>
25
+ // not the message AND the message is not in ancestors
26
+ !(message.id === messageId) && message.ancestors && !message.ancestors.includes(messageId)
27
+ )
28
+ .map((message) => {
29
+ // remove the message from children if it's there
30
+ if (message.children && message.children.includes(messageId)) {
31
+ message.children = message.children.filter((child) => child !== messageId);
32
+ }
33
+ return message;
34
+ });
35
+
36
+ await collections.conversations.updateOne(
37
+ { _id: conversation._id, ...authCondition(locals) },
38
+ { $set: { messages: filteredMessages } }
39
+ );
40
+
41
+ return new Response();
42
+ }
src/routes/conversation/[id]/+page.server.ts CHANGED
@@ -66,44 +66,3 @@ export const load = async ({ params, depends, locals }) => {
66
  shared,
67
  };
68
  };
69
-
70
- export const actions = {
71
- deleteBranch: async ({ request, locals, params }) => {
72
- const data = await request.formData();
73
- const messageId = data.get("messageId");
74
-
75
- if (!messageId || typeof messageId !== "string") {
76
- error(400, "Invalid message id");
77
- }
78
-
79
- const conversation = await collections.conversations.findOne({
80
- ...authCondition(locals),
81
- _id: new ObjectId(params.id),
82
- });
83
-
84
- if (!conversation) {
85
- error(404, "Conversation not found");
86
- }
87
-
88
- const filteredMessages = conversation.messages
89
- .filter(
90
- (message) =>
91
- // not the message AND the message is not in ancestors
92
- !(message.id === messageId) && message.ancestors && !message.ancestors.includes(messageId)
93
- )
94
- .map((message) => {
95
- // remove the message from children if it's there
96
- if (message.children && message.children.includes(messageId)) {
97
- message.children = message.children.filter((child) => child !== messageId);
98
- }
99
- return message;
100
- });
101
-
102
- await collections.conversations.updateOne(
103
- { _id: conversation._id, ...authCondition(locals) },
104
- { $set: { messages: filteredMessages } }
105
- );
106
-
107
- return { from: "deleteBranch", ok: true };
108
- },
109
- };
 
66
  shared,
67
  };
68
  };