-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuseSendMessage.tsx
More file actions
37 lines (31 loc) · 1.02 KB
/
useSendMessage.tsx
File metadata and controls
37 lines (31 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { useCallback } from 'react';
import { AppActionsType } from '@/config/appActions';
import { AppDataTypes } from '@/config/appData';
import { mutations } from '@/config/queryClient';
/**
* Create a function that save an app data with the user message
* @returns sendMessage function
*/
export const useSendMessage = () => {
const { mutateAsync: postAppDataAsync, isLoading } =
mutations.usePostAppData();
const { mutateAsync: postAppActionAsync } = mutations.usePostAppAction();
const sendMessage = useCallback(
async (newUserComment: string) => {
// post new user comment as appData with normal call
const userMessage = await postAppDataAsync({
data: {
content: newUserComment,
},
type: AppDataTypes.UserComment,
});
await postAppActionAsync({
type: AppActionsType.Reply,
data: { content: newUserComment },
});
return userMessage;
},
[postAppDataAsync, postAppActionAsync],
);
return { sendMessage, isLoading };
};