blob: d7427300faee6da8ef922237d6e0dccf0c7e78a7 [file] [log] [blame]
simon26e79f72022-10-05 22:16:08 -04001/*
2 * Copyright (C) 2022 Savoir-faire Linux Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public
15 * License along with this program. If not, see
16 * <https://www.gnu.org/licenses/>.
17 */
simond47ef9e2022-09-28 22:24:28 -040018import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
19import axios from 'axios';
idillon08f77172022-09-13 19:14:17 -040020
simon5da8ca62022-11-09 15:21:25 -050021import { useAuthContext } from '../contexts/AuthProvider';
22import { apiUrl } from '../utils/constants';
23
24export const useConversationQuery = (conversationId: string) => {
25 const { token } = useAuthContext();
26 return useQuery(['conversation', conversationId], () => fetchConversation(conversationId, token), {
27 enabled: !!conversationId,
simond47ef9e2022-09-28 22:24:28 -040028 });
29};
idillon08f77172022-09-13 19:14:17 -040030
simon5da8ca62022-11-09 15:21:25 -050031export const useMessagesQuery = (conversationId: string) => {
32 const { token } = useAuthContext();
33 return useQuery(['messages', conversationId], () => fetchMessages(conversationId, token), {
34 enabled: !!conversationId,
simond47ef9e2022-09-28 22:24:28 -040035 });
36};
idillon08f77172022-09-13 19:14:17 -040037
simon5da8ca62022-11-09 15:21:25 -050038export const useSendMessageMutation = (conversationId: string) => {
39 const { token } = useAuthContext();
simond47ef9e2022-09-28 22:24:28 -040040 const queryClient = useQueryClient();
41 return useMutation(
simon5da8ca62022-11-09 15:21:25 -050042 (message: string) =>
43 axios.post(
44 new URL(`/conversations/${conversationId}/messages`, apiUrl).toString(),
45 { message },
46 {
47 headers: {
48 Authorization: `Bearer ${token}`,
49 },
50 }
51 ),
simond47ef9e2022-09-28 22:24:28 -040052 {
simon5da8ca62022-11-09 15:21:25 -050053 onSuccess: () => queryClient.invalidateQueries(['messages', conversationId]),
simond47ef9e2022-09-28 22:24:28 -040054 }
55 );
56};
idillonea465602022-09-13 19:58:51 -040057
simon5da8ca62022-11-09 15:21:25 -050058const fetchConversation = (conversationId: string, token: string) =>
59 axios
60 .get(new URL(`/conversations/${conversationId}`, apiUrl).toString(), {
61 headers: {
62 Authorization: `Bearer ${token}`,
63 },
64 })
65 .then((result) => result.data);
idillon08f77172022-09-13 19:14:17 -040066
simon5da8ca62022-11-09 15:21:25 -050067const fetchMessages = (conversationId: string, token: string) =>
68 axios
69 .get(new URL(`/conversations/${conversationId}/messages`, apiUrl).toString(), {
70 headers: {
71 Authorization: `Bearer ${token}`,
72 },
73 })
74 .then((result) => result.data);