Skip to content

Commit 53fdba7

Browse files
ReidyTspaenleh
andauthored
fix: update chatbot to be a query-client (#199)
* fix: update chatbot to be a mutation using query-client * fix: update code formatting to match prettier * fix: update usePostAppAction's first setQueryData to be an array - This changes assure to have an array as prevData and solve the s.some is not a function error * Update setQueryData of appAction.ts - Wrap the newData into an array if prevData is null - Add some comments Co-authored-by: Basile Spaenlehauer <[email protected]> --------- Co-authored-by: Basile Spaenlehauer <[email protected]>
1 parent fad7b6f commit 53fdba7

11 files changed

+71
-80
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ yarn-error.log
1919

2020
# editors
2121
.idea/
22-
2322
.DS_Store
23+
.env.*

src/api/chatBot.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ChatBotMessage } from '@graasp/sdk';
2+
3+
import { ApiData, ChatBotCompletion } from 'src/types';
4+
5+
import configureAxios from './axios';
6+
import { buildPostChatBotRoute } from './routes';
7+
8+
const axios = configureAxios();
9+
10+
export const postChatBot = (
11+
args: ApiData & {
12+
body: ChatBotMessage[];
13+
},
14+
) => {
15+
const { token, itemId, apiHost, body } = args;
16+
return axios
17+
.post<ChatBotCompletion>(`${apiHost}/${buildPostChatBotRoute(itemId)}`, body, {
18+
headers: {
19+
Authorization: `Bearer ${token}`,
20+
},
21+
})
22+
.then(({ data }) => data);
23+
};

src/api/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from './appData';
33
export * from './appAction';
44
export * from './appSetting';
55
export * from './mocks';
6+
export * from './chatBot';

src/hooks/index.ts

-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ import configureAppActionHooks from './appAction';
55
import configureAppDataHooks from './appData';
66
import configureAppSettingHooks from './appSetting';
77
import configurePostMessageHooks from './postMessage';
8-
import { useChatbotApi } from './useChatbotApi';
98

109
export default (queryConfig: QueryClientConfig, websocketClient?: WebsocketClient) => ({
1110
...configureAppsHooks(queryConfig),
1211
...configureAppDataHooks(queryConfig, websocketClient),
1312
...configurePostMessageHooks(queryConfig),
1413
...configureAppSettingHooks(queryConfig, websocketClient),
1514
...configureAppActionHooks(queryConfig, websocketClient),
16-
useChatbotApi,
1715
});

src/hooks/useChatbotApi.tsx

-74
This file was deleted.

src/mutations/appAction.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export default (queryConfig: QueryClientConfig) => {
2727
const newData: AppAction = newAppAction;
2828
// check that the websocket event has not already been received and therefore the data were added
2929
if (!prevData) {
30-
queryClient.setQueryData(key, newData);
30+
// we need to wrap the created appAction in an array because the cache key will receive all the actions but the post call only return the current posted data
31+
queryClient.setQueryData<AppAction[]>(key, [newData]);
3132
} else if (!prevData.some((a) => a.id === newData.id)) {
3233
queryClient.setQueryData(key, [...(prevData ?? []), newData]);
3334
}

src/mutations/chatBot.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { ChatBotMessage } from '@graasp/sdk';
2+
3+
import { useMutation, useQueryClient } from '@tanstack/react-query';
4+
5+
import * as Api from '../api';
6+
import { getApiHost, getDataOrThrow } from '../config/utils';
7+
import { postChatBotRoutine } from '../routines';
8+
import { QueryClientConfig } from '../types';
9+
10+
export default (queryConfig: QueryClientConfig) => {
11+
const { notifier } = queryConfig;
12+
13+
const usePostChatBot = () => {
14+
const queryClient = useQueryClient();
15+
return useMutation(
16+
async (payload: ChatBotMessage[]) => {
17+
const apiHost = getApiHost(queryClient);
18+
const data = getDataOrThrow(queryClient);
19+
20+
return Api.postChatBot({ ...data, body: payload, apiHost });
21+
},
22+
{
23+
onError: (error: Error) => {
24+
notifier?.({
25+
type: postChatBotRoutine.FAILURE,
26+
payload: { error },
27+
});
28+
},
29+
},
30+
);
31+
};
32+
33+
return { usePostChatBot };
34+
};

src/mutations/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import { QueryClientConfig } from '../types';
22
import appActionMutations from './appAction';
33
import appMutations from './appData';
44
import appSettingMutations from './appSetting';
5+
import chatBotMutations from './chatBot';
56

6-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
77
const configureMutations = (queryConfig: QueryClientConfig) => ({
88
...appMutations(queryConfig),
99
...appSettingMutations(queryConfig),
1010
...appActionMutations(queryConfig),
11+
...chatBotMutations(queryConfig),
1112
});
1213

1314
export default configureMutations;

src/routines/chatBot.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import createRoutine from './utils';
2+
3+
export const postChatBotRoutine = createRoutine('POST_CHAT_BOT');

src/routines/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './appData';
22
export * from './appSetting';
33
export * from './appAction';
44
export * from './postMessage';
5+
export * from './chatBot';

src/types.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,7 @@ export interface Database {
107107
items: DiscriminatedItem[];
108108
}
109109

110-
export type UserDataType = { [key: string]: unknown };
110+
export type ChatBotCompletion = {
111+
completion: string;
112+
model: string;
113+
};

0 commit comments

Comments
 (0)