simon | 26e79f7 | 2022-10-05 22:16:08 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 18 | import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; |
| 19 | import axios from 'axios'; |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 20 | |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 21 | import { useAuthContext } from '../contexts/AuthProvider'; |
| 22 | import { apiUrl } from '../utils/constants'; |
| 23 | |
| 24 | export const useConversationQuery = (conversationId: string) => { |
| 25 | const { token } = useAuthContext(); |
| 26 | return useQuery(['conversation', conversationId], () => fetchConversation(conversationId, token), { |
| 27 | enabled: !!conversationId, |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 28 | }); |
| 29 | }; |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 30 | |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 31 | export const useMessagesQuery = (conversationId: string) => { |
| 32 | const { token } = useAuthContext(); |
| 33 | return useQuery(['messages', conversationId], () => fetchMessages(conversationId, token), { |
| 34 | enabled: !!conversationId, |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 35 | }); |
| 36 | }; |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 37 | |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 38 | export const useSendMessageMutation = (conversationId: string) => { |
| 39 | const { token } = useAuthContext(); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 40 | const queryClient = useQueryClient(); |
| 41 | return useMutation( |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 42 | (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 | ), |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 52 | { |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 53 | onSuccess: () => queryClient.invalidateQueries(['messages', conversationId]), |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 54 | } |
| 55 | ); |
| 56 | }; |
idillon | ea46560 | 2022-09-13 19:58:51 -0400 | [diff] [blame] | 57 | |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 58 | const 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); |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 66 | |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 67 | const 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); |