Skip to content

Commit 94e0636

Browse files
committed
add delete conversation mutation, fix withAuthentication on post requests
1 parent bd53b87 commit 94e0636

File tree

5 files changed

+68
-49
lines changed

5 files changed

+68
-49
lines changed

client/src/components/Conversations/Conversation.jsx

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,30 +61,20 @@ export default function Conversation({ conversation, retainView }) {
6161
if (titleInput === title) {
6262
return;
6363
}
64-
updateConvoMutation.mutate({ arg: { conversationId, title: titleInput }});
65-
66-
// rename.trigger({ conversationId, title: titleInput }).then(() => {
67-
// refreshConversations();
68-
// if (conversationId == currentConversation?.conversationId)
69-
// setCurrentConversation(prevState => ({
70-
// ...prevState,
71-
// title: titleInput
72-
// }));
73-
// });
64+
updateConvoMutation.mutate({ conversationId, title: titleInput });
7465
};
7566

7667
useEffect(() => {
77-
if (updateConvoMutation.isSuccess) {
78-
// debugger;
79-
refreshConversations();
80-
if (conversationId == currentConversation?.conversationId)
81-
82-
setCurrentConversation(prevState => ({
83-
...prevState,
84-
title: titleInput
85-
}));
86-
}
87-
}, [updateConvoMutation.isSuccess]);
68+
if (updateConvoMutation.isSuccess) {
69+
refreshConversations();
70+
if (conversationId == currentConversation?.conversationId) {
71+
setCurrentConversation(prevState => ({
72+
...prevState,
73+
title: titleInput
74+
}));
75+
}
76+
}
77+
}, [updateConvoMutation.isSuccess]);
8878

8979
const handleKeyDown = e => {
9080
if (e.key === 'Enter') {

client/src/components/Conversations/DeleteButton.jsx

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
1-
import React from 'react';
1+
import { useEffect } from 'react';
22
import TrashIcon from '../svg/TrashIcon';
33
import CrossIcon from '../svg/CrossIcon';
4-
import manualSWR from '~/utils/fetchers';
54
import { useRecoilValue } from 'recoil';
5+
import { useDeleteConversationMutation } from '~/data-provider';
66

77
import store from '~/store';
88

99
export default function DeleteButton({ conversationId, renaming, cancelHandler, retainView }) {
1010
const currentConversation = useRecoilValue(store.conversation) || {};
1111
const { newConversation } = store.useConversation();
1212
const { refreshConversations } = store.useConversations();
13-
const { trigger } = manualSWR(`/api/convos/clear`, 'post', () => {
14-
if (currentConversation?.conversationId == conversationId) newConversation();
15-
refreshConversations();
16-
retainView();
17-
});
1813

19-
const clickHandler = () => trigger({ conversationId, source: 'button' });
14+
const deleteConvoMutation = useDeleteConversationMutation(conversationId);
15+
16+
useEffect(() => {
17+
if(deleteConvoMutation.isSuccess) {
18+
if (currentConversation?.conversationId == conversationId) newConversation();
19+
20+
refreshConversations();
21+
retainView();
22+
}
23+
}, [deleteConvoMutation.isSuccess]);
24+
25+
26+
const clickHandler = () => {
27+
deleteConvoMutation.mutate({conversationId, source: 'button' });
28+
};
29+
2030
const handler = renaming ? cancelHandler : clickHandler;
2131

2232
return (

client/src/data-provider/react-query-service.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
} from "@tanstack/react-query";
99
import * as t from "./types";
1010
import * as dataService from "./data-service";
11-
import store from '~/store';
1211

1312
export enum QueryKeys {
1413
messages = "messsages",
@@ -74,16 +73,27 @@ export const useUpdateConversationMutation = (
7473
{
7574
onSuccess: () => {
7675
queryClient.invalidateQueries([QueryKeys.conversation, id]);
77-
queryClient.invalidateQueries([QueryKeys.allConversations, id]);
76+
queryClient.invalidateQueries([QueryKeys.allConversations]);
7877
},
7978
}
8079
);
8180
};
8281

83-
// export const useDeleteConversationMutation = (
84-
// id: string
85-
// ): UseMutationResult<
86-
82+
export const useDeleteConversationMutation = (
83+
id: string
84+
): UseMutationResult<t.TDeleteConversationResponse, unknown, t.TDeleteConversationRequest, unknown> => {
85+
const queryClient = useQueryClient();
86+
return useMutation(
87+
(payload: t.TDeleteConversationRequest) =>
88+
dataService.deleteConversation(payload),
89+
{
90+
onSuccess: () => {
91+
queryClient.invalidateQueries([QueryKeys.conversation, id]);
92+
queryClient.invalidateQueries([QueryKeys.allConversations]);
93+
},
94+
}
95+
);
96+
};
8797

8898
export const useUpdateCustomGptMutation = (): UseMutationResult<
8999
t.TUpdateCustomGptResponse,

client/src/data-provider/request.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import axios, { AxiosRequestConfig } from "axios";
22

33
async function _get<T>(url: string, options?: AxiosRequestConfig): Promise<T> {
4-
const response = await axios.get(url, { withCredentials: true, ...options});
4+
const response = await axios.get(url, {...options});
55
return response.data;
66
}
77

8-
async function _post(url: string, data?: any) {
9-
const response = await axios.post(url, JSON.stringify(data), {
10-
withCredentials: true,
8+
async function _post(url: string, arg?: any) {
9+
const modifiedData = {arg, withCredentials: true}
10+
const response = await axios.post(url, modifiedData, {
1111
headers: { "Content-Type": "application/json" },
1212
});
1313
return response.data;
@@ -19,7 +19,6 @@ async function _postMultiPart(
1919
options?: AxiosRequestConfig
2020
) {
2121
const response = await axios.post(url, formData, {
22-
withCredentials: true,
2322
...options,
2423
headers: { "Content-Type": "multipart/form-data" },
2524
});
@@ -28,28 +27,26 @@ async function _postMultiPart(
2827

2928
async function _put(url: string, data?: any) {
3029
const response = await axios.put(url, JSON.stringify(data), {
31-
withCredentials: true,
3230
headers: { "Content-Type": "application/json" },
3331
});
3432
return response.data;
3533
}
3634

3735
async function _delete<T>(url: string): Promise<T> {
38-
const response = await axios.delete(url, { withCredentials: true });
36+
const response = await axios.delete(url);
3937
return response.data;
4038
}
4139

4240
async function _deleteWithOptions<T>(
4341
url: string,
4442
options?: AxiosRequestConfig
4543
): Promise<T> {
46-
const response = await axios.delete(url, { withCredentials: true, ...options});
44+
const response = await axios.delete(url, {...options});
4745
return response.data;
4846
}
4947

5048
async function _patch(url: string, data?: any) {
5149
const response = await axios.patch(url, JSON.stringify(data), {
52-
withCredentials: true,
5350
headers: { "Content-Type": "application/json" },
5451
});
5552
return response.data;

client/src/data-provider/types.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,6 @@ export type TGetMessagesResponse = {
106106
data: TMessage[]
107107
};
108108

109-
export type TDeleteConversationRequest = {
110-
conversationId: string
111-
};
112-
113109
export type TAICompletionRequest = {
114110
chatGptLabel?: string,
115111
conversationId: string,
@@ -150,15 +146,31 @@ export type TConversationUpdate = {
150146
conversationId: string,
151147
title?: string
152148
};
149+
153150
export type TUpdateConversationRequest = {
154-
arg: {},
151+
conversationId: string,
152+
title: string,
155153
withCredentials?: boolean
156154
};
157155

158156
export type TUpdateConversationResponse = {
159157
data: TConversation
160158
};
161159

160+
export type TDeleteConversationRequest = {
161+
conversationId: string,
162+
source: string
163+
}
164+
165+
export type TDeleteConversationResponse = {
166+
acknowledged: boolean,
167+
deletedCount: number,
168+
messages: {
169+
acknowledged: boolean,
170+
deletedCount: number
171+
}
172+
};
173+
162174
export type TUpdateCustomGptRequest = {
163175
value: string,
164176
chatGptLabel: string,

0 commit comments

Comments
 (0)