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 | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 21 | export const useConversationQuery = (accountId: string, conversationId: string) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 22 | return useQuery(['conversation', accountId, conversationId], () => fetchConversation(accountId, conversationId), { |
| 23 | enabled: !!(accountId && conversationId), |
| 24 | }); |
| 25 | }; |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 26 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 27 | export const useMessagesQuery = (accountId: string, conversationId: string) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 28 | return useQuery(['messages', accountId, conversationId], () => fetchMessages(accountId, conversationId), { |
| 29 | enabled: !!(accountId && conversationId), |
| 30 | }); |
| 31 | }; |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 32 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 33 | export const useSendMessageMutation = (accountId: string, conversationId: string) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 34 | const queryClient = useQueryClient(); |
| 35 | return useMutation( |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 36 | (message: string) => axios.post(`/api/accounts/${accountId}/conversations/${conversationId}`, { message }), |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 37 | { |
| 38 | onSuccess: () => queryClient.invalidateQueries(['messages', accountId, conversationId]), |
| 39 | } |
| 40 | ); |
| 41 | }; |
idillon | ea46560 | 2022-09-13 19:58:51 -0400 | [diff] [blame] | 42 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 43 | const fetchConversation = (accountId: string, conversationId: string) => |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 44 | axios.get(`/api/accounts/${accountId}/conversations/${conversationId}`).then((result) => result.data); |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 45 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 46 | const fetchMessages = (accountId: string, conversationId: string) => |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 47 | axios.get(`/api/accounts/${accountId}/conversations/${conversationId}/messages`).then((result) => result.data); |