add delete conversation mutation, fix withAuthentication on post requests

This commit is contained in:
Daniel D Orlando 2023-04-03 12:39:00 -07:00
parent bd53b878d4
commit 94e0636b32
5 changed files with 68 additions and 49 deletions

View file

@ -61,29 +61,19 @@ export default function Conversation({ conversation, retainView }) {
if (titleInput === title) { if (titleInput === title) {
return; return;
} }
updateConvoMutation.mutate({ arg: { conversationId, title: titleInput }}); updateConvoMutation.mutate({ conversationId, title: titleInput });
// rename.trigger({ conversationId, title: titleInput }).then(() => {
// refreshConversations();
// if (conversationId == currentConversation?.conversationId)
// setCurrentConversation(prevState => ({
// ...prevState,
// title: titleInput
// }));
// });
}; };
useEffect(() => { useEffect(() => {
if (updateConvoMutation.isSuccess) { if (updateConvoMutation.isSuccess) {
// debugger;
refreshConversations(); refreshConversations();
if (conversationId == currentConversation?.conversationId) if (conversationId == currentConversation?.conversationId) {
setCurrentConversation(prevState => ({ setCurrentConversation(prevState => ({
...prevState, ...prevState,
title: titleInput title: titleInput
})); }));
} }
}
}, [updateConvoMutation.isSuccess]); }, [updateConvoMutation.isSuccess]);
const handleKeyDown = e => { const handleKeyDown = e => {

View file

@ -1,8 +1,8 @@
import React from 'react'; import { useEffect } from 'react';
import TrashIcon from '../svg/TrashIcon'; import TrashIcon from '../svg/TrashIcon';
import CrossIcon from '../svg/CrossIcon'; import CrossIcon from '../svg/CrossIcon';
import manualSWR from '~/utils/fetchers';
import { useRecoilValue } from 'recoil'; import { useRecoilValue } from 'recoil';
import { useDeleteConversationMutation } from '~/data-provider';
import store from '~/store'; import store from '~/store';
@ -10,13 +10,23 @@ export default function DeleteButton({ conversationId, renaming, cancelHandler,
const currentConversation = useRecoilValue(store.conversation) || {}; const currentConversation = useRecoilValue(store.conversation) || {};
const { newConversation } = store.useConversation(); const { newConversation } = store.useConversation();
const { refreshConversations } = store.useConversations(); const { refreshConversations } = store.useConversations();
const { trigger } = manualSWR(`/api/convos/clear`, 'post', () => {
const deleteConvoMutation = useDeleteConversationMutation(conversationId);
useEffect(() => {
if(deleteConvoMutation.isSuccess) {
if (currentConversation?.conversationId == conversationId) newConversation(); if (currentConversation?.conversationId == conversationId) newConversation();
refreshConversations(); refreshConversations();
retainView(); retainView();
}); }
}, [deleteConvoMutation.isSuccess]);
const clickHandler = () => {
deleteConvoMutation.mutate({conversationId, source: 'button' });
};
const clickHandler = () => trigger({ conversationId, source: 'button' });
const handler = renaming ? cancelHandler : clickHandler; const handler = renaming ? cancelHandler : clickHandler;
return ( return (

View file

@ -8,7 +8,6 @@ import {
} from "@tanstack/react-query"; } from "@tanstack/react-query";
import * as t from "./types"; import * as t from "./types";
import * as dataService from "./data-service"; import * as dataService from "./data-service";
import store from '~/store';
export enum QueryKeys { export enum QueryKeys {
messages = "messsages", messages = "messsages",
@ -74,16 +73,27 @@ export const useUpdateConversationMutation = (
{ {
onSuccess: () => { onSuccess: () => {
queryClient.invalidateQueries([QueryKeys.conversation, id]); queryClient.invalidateQueries([QueryKeys.conversation, id]);
queryClient.invalidateQueries([QueryKeys.allConversations, id]); queryClient.invalidateQueries([QueryKeys.allConversations]);
}, },
} }
); );
}; };
// export const useDeleteConversationMutation = ( export const useDeleteConversationMutation = (
// id: string id: string
// ): UseMutationResult< ): UseMutationResult<t.TDeleteConversationResponse, unknown, t.TDeleteConversationRequest, unknown> => {
const queryClient = useQueryClient();
return useMutation(
(payload: t.TDeleteConversationRequest) =>
dataService.deleteConversation(payload),
{
onSuccess: () => {
queryClient.invalidateQueries([QueryKeys.conversation, id]);
queryClient.invalidateQueries([QueryKeys.allConversations]);
},
}
);
};
export const useUpdateCustomGptMutation = (): UseMutationResult< export const useUpdateCustomGptMutation = (): UseMutationResult<
t.TUpdateCustomGptResponse, t.TUpdateCustomGptResponse,

View file

@ -1,13 +1,13 @@
import axios, { AxiosRequestConfig } from "axios"; import axios, { AxiosRequestConfig } from "axios";
async function _get<T>(url: string, options?: AxiosRequestConfig): Promise<T> { async function _get<T>(url: string, options?: AxiosRequestConfig): Promise<T> {
const response = await axios.get(url, { withCredentials: true, ...options}); const response = await axios.get(url, {...options});
return response.data; return response.data;
} }
async function _post(url: string, data?: any) { async function _post(url: string, arg?: any) {
const response = await axios.post(url, JSON.stringify(data), { const modifiedData = {arg, withCredentials: true}
withCredentials: true, const response = await axios.post(url, modifiedData, {
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
}); });
return response.data; return response.data;
@ -19,7 +19,6 @@ async function _postMultiPart(
options?: AxiosRequestConfig options?: AxiosRequestConfig
) { ) {
const response = await axios.post(url, formData, { const response = await axios.post(url, formData, {
withCredentials: true,
...options, ...options,
headers: { "Content-Type": "multipart/form-data" }, headers: { "Content-Type": "multipart/form-data" },
}); });
@ -28,14 +27,13 @@ async function _postMultiPart(
async function _put(url: string, data?: any) { async function _put(url: string, data?: any) {
const response = await axios.put(url, JSON.stringify(data), { const response = await axios.put(url, JSON.stringify(data), {
withCredentials: true,
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
}); });
return response.data; return response.data;
} }
async function _delete<T>(url: string): Promise<T> { async function _delete<T>(url: string): Promise<T> {
const response = await axios.delete(url, { withCredentials: true }); const response = await axios.delete(url);
return response.data; return response.data;
} }
@ -43,13 +41,12 @@ async function _deleteWithOptions<T>(
url: string, url: string,
options?: AxiosRequestConfig options?: AxiosRequestConfig
): Promise<T> { ): Promise<T> {
const response = await axios.delete(url, { withCredentials: true, ...options}); const response = await axios.delete(url, {...options});
return response.data; return response.data;
} }
async function _patch(url: string, data?: any) { async function _patch(url: string, data?: any) {
const response = await axios.patch(url, JSON.stringify(data), { const response = await axios.patch(url, JSON.stringify(data), {
withCredentials: true,
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
}); });
return response.data; return response.data;

View file

@ -106,10 +106,6 @@ export type TGetMessagesResponse = {
data: TMessage[] data: TMessage[]
}; };
export type TDeleteConversationRequest = {
conversationId: string
};
export type TAICompletionRequest = { export type TAICompletionRequest = {
chatGptLabel?: string, chatGptLabel?: string,
conversationId: string, conversationId: string,
@ -150,8 +146,10 @@ export type TConversationUpdate = {
conversationId: string, conversationId: string,
title?: string title?: string
}; };
export type TUpdateConversationRequest = { export type TUpdateConversationRequest = {
arg: {}, conversationId: string,
title: string,
withCredentials?: boolean withCredentials?: boolean
}; };
@ -159,6 +157,20 @@ export type TUpdateConversationResponse = {
data: TConversation data: TConversation
}; };
export type TDeleteConversationRequest = {
conversationId: string,
source: string
}
export type TDeleteConversationResponse = {
acknowledged: boolean,
deletedCount: number,
messages: {
acknowledged: boolean,
deletedCount: number
}
};
export type TUpdateCustomGptRequest = { export type TUpdateCustomGptRequest = {
value: string, value: string,
chatGptLabel: string, chatGptLabel: string,