idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame^] | 1 | import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; |
| 2 | import axios from "axios" |
| 3 | |
| 4 | export const useConversationQuery = (accountId, conversationId) => { |
| 5 | return useQuery( |
| 6 | ["conversation", accountId, conversationId], |
| 7 | () => fetchConversation(accountId, conversationId), |
| 8 | { |
| 9 | "enabled": !!(accountId && conversationId), |
| 10 | } |
| 11 | ) |
| 12 | } |
| 13 | |
| 14 | export const useMessagesQuery = (accountId, conversationId) => { |
| 15 | return useQuery( |
| 16 | ["messages", accountId, conversationId], |
| 17 | () => fetchMessages(accountId, conversationId), |
| 18 | { |
| 19 | "enabled": !!(accountId && conversationId), |
| 20 | } |
| 21 | ) |
| 22 | } |
| 23 | |
| 24 | const fetchConversation = (accountId, conversationId) => ( |
| 25 | axios |
| 26 | .get(`/api/accounts/${accountId}/conversations/${conversationId}`) |
| 27 | .then((result) => result.data) |
| 28 | ) |
| 29 | |
| 30 | const fetchMessages = (accountId, conversationId) => ( |
| 31 | axios |
| 32 | .get(`/api/accounts/${accountId}/conversations/${conversationId}/messages`) |
| 33 | .then((result) => result.data) |
| 34 | ) |