blob: 9ea4d29ed8d0f3faa03fc17e3b6d40ed094cb75d [file] [log] [blame]
idillon847b4642022-12-29 14:28:38 -05001/*
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 */
18import { useMutation, useQuery } from '@tanstack/react-query';
Ziwei Wang046fb202023-01-17 18:27:17 -050019import { useQueryClient } from '@tanstack/react-query';
idillon847b4642022-12-29 14:28:38 -050020import { ContactDetails, LookupResult } from 'jami-web-common';
21
22import { useAuthContext } from '../contexts/AuthProvider';
23import { Contact } from '../models/contact';
24import { useRefreshConversationsSummaries } from './conversationQueries';
25
26export const useContactsSearchQuery = (searchQuery: string) => {
27 const { axiosInstance } = useAuthContext();
28 return useQuery({
idillon2ef2be92022-12-30 10:59:06 -050029 queryKey: ['ns', 'username', searchQuery],
idillon847b4642022-12-29 14:28:38 -050030 queryFn: async () => {
31 const { data } = await axiosInstance.get<LookupResult>(`/ns/username/${searchQuery}`);
32 return data ? [new Contact(data.address, data.username)] : [];
33 },
34 enabled: !!searchQuery,
35 });
36};
37
idillon18283ac2023-01-07 12:06:42 -050038export const useContactQuery = (contactId?: string) => {
39 const { axiosInstance } = useAuthContext();
40
41 return useQuery({
42 queryKey: ['contacts', contactId],
43 queryFn: async () => {
44 const { data } = await axiosInstance.get<ContactDetails>(`/contacts/${contactId}`);
45 return data;
46 },
47 enabled: !!contactId,
48 });
49};
50
idillon847b4642022-12-29 14:28:38 -050051export const useAddContactMutation = () => {
52 const { axiosInstance } = useAuthContext();
53
54 const refreshConversationsSummaries = useRefreshConversationsSummaries();
55
56 return useMutation({
57 mutationFn: async (contactId: string) => {
58 const { data } = await axiosInstance.put<ContactDetails>(`/contacts/${contactId}`);
59 return data;
60 },
61 onSuccess: () => {
62 refreshConversationsSummaries();
63 },
64 });
65};
Ziwei Wang046fb202023-01-17 18:27:17 -050066export const useContactListQuery = () => {
67 const { axiosInstance } = useAuthContext();
68 return useQuery({
69 queryKey: ['contact', 'list'],
70 queryFn: async ({ signal }) => {
71 const { data } = await axiosInstance.get<ContactDetails[]>(`/contacts`, { signal });
72 return data;
73 },
74 });
75};
76
77export const useRemoveContactMutation = () => {
78 const queryClient = useQueryClient();
79 const { axiosInstance } = useAuthContext();
80 return useMutation({
81 mutationKey: ['contact', 'remove'],
82 mutationFn: async (contactId: string) => {
83 const { data } = await axiosInstance.delete(`/contacts/${contactId}`);
84 return data;
85 },
86 onSuccess: () => {
87 queryClient.invalidateQueries({ queryKey: ['contact', 'list'] });
88 },
89 onError: () => {
90 console.log(`Error deleting contact`);
91 queryClient.invalidateQueries({ queryKey: ['contact', 'list'] });
92 },
93 });
94};
95
96export const useBlockContactMutation = () => {
97 const queryClient = useQueryClient();
98 const { axiosInstance } = useAuthContext();
99 return useMutation({
100 mutationKey: ['contact', 'block'],
101 mutationFn: async (contactId: string) => {
102 const { data } = await axiosInstance.post(`/contacts/${contactId}/block`);
103 return data;
104 },
105 onSuccess: () => {
106 queryClient.invalidateQueries({ queryKey: ['contact', 'list'] });
107 },
108 onError: () => {
109 console.log(`Error blocking contact`);
110 queryClient.invalidateQueries({ queryKey: ['contact', 'list'] });
111 },
112 });
113};