idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [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 | */ |
| 18 | import { useMutation, useQuery } from '@tanstack/react-query'; |
| 19 | import { ContactDetails, LookupResult } from 'jami-web-common'; |
| 20 | |
| 21 | import { useAuthContext } from '../contexts/AuthProvider'; |
| 22 | import { Contact } from '../models/contact'; |
| 23 | import { useRefreshConversationsSummaries } from './conversationQueries'; |
| 24 | |
| 25 | export const useContactsSearchQuery = (searchQuery: string) => { |
| 26 | const { axiosInstance } = useAuthContext(); |
| 27 | return useQuery({ |
idillon | 2ef2be9 | 2022-12-30 10:59:06 -0500 | [diff] [blame] | 28 | queryKey: ['ns', 'username', searchQuery], |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame] | 29 | queryFn: async () => { |
| 30 | const { data } = await axiosInstance.get<LookupResult>(`/ns/username/${searchQuery}`); |
| 31 | return data ? [new Contact(data.address, data.username)] : []; |
| 32 | }, |
| 33 | enabled: !!searchQuery, |
| 34 | }); |
| 35 | }; |
| 36 | |
| 37 | export const useAddContactMutation = () => { |
| 38 | const { axiosInstance } = useAuthContext(); |
| 39 | |
| 40 | const refreshConversationsSummaries = useRefreshConversationsSummaries(); |
| 41 | |
| 42 | return useMutation({ |
| 43 | mutationFn: async (contactId: string) => { |
| 44 | const { data } = await axiosInstance.put<ContactDetails>(`/contacts/${contactId}`); |
| 45 | return data; |
| 46 | }, |
| 47 | onSuccess: () => { |
| 48 | refreshConversationsSummaries(); |
| 49 | }, |
| 50 | }); |
| 51 | }; |