184 lines
5.8 KiB
TypeScript
184 lines
5.8 KiB
TypeScript
/**
|
|
* Contact CRUD hooks (legacy contact model — not unified contacts).
|
|
*/
|
|
|
|
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
|
import { apiGet, apiPost, apiPatch, apiDelete, apiPut } from './client';
|
|
import { PaginatedResponse, Contact, ContactDetail } from './types';
|
|
import { ContactFolder } from './contactFolders';
|
|
|
|
export function useContacts(page = 1, pageSize = 25, search?: string) {
|
|
const params = new URLSearchParams({ page: String(page), page_size: String(pageSize) });
|
|
if (search) params.set('search', search);
|
|
return useQuery({
|
|
queryKey: ['contacts', page, pageSize, search],
|
|
queryFn: () =>
|
|
apiGet<PaginatedResponse<Contact>>(`/contacts?${params.toString()}`),
|
|
});
|
|
}
|
|
|
|
export function useContact(id?: string) {
|
|
return useQuery({
|
|
queryKey: ['contacts', id],
|
|
queryFn: () => apiGet<ContactDetail>(`/contacts/${id}`),
|
|
enabled: !!id,
|
|
});
|
|
}
|
|
|
|
export function useCreateContact() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (data: Partial<Contact> & { company_ids?: string[] }) =>
|
|
apiPost('/contacts', data),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['contacts'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUpdateContact() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ id, data }: { id: string; data: Partial<Contact> & { company_ids?: string[] } }) =>
|
|
apiPatch(`/contacts/${id}`, data),
|
|
onSuccess: (_data, variables) => {
|
|
queryClient.invalidateQueries({ queryKey: ['contacts'] });
|
|
queryClient.invalidateQueries({ queryKey: ['contacts', variables.id] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useDeleteContact() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (id: string) => apiDelete(`/contacts/${id}`),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['contacts'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
// ── Contact Folders ──
|
|
|
|
export function useContactFolders() {
|
|
return useQuery({
|
|
queryKey: ['contactFolders'],
|
|
queryFn: async () => {
|
|
const res = await apiGet<ContactFolder[] | { items: ContactFolder[] }>('/contact-folders');
|
|
return Array.isArray(res) ? res : res.items ?? [];
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useCreateContactFolder() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (data: { name: string; parent_id?: string }) => apiPost('/contact-folders', data),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['contactFolders'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUpdateContactFolder() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ id, data }: { id: string; data: { name?: string; parent_id?: string | null; sort_order?: number } }) =>
|
|
apiPut(`/contact-folders/${id}`, data),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['contactFolders'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useDeleteContactFolder() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (id: string) => apiDelete(`/contact-folders/${id}`),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['contactFolders'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useMoveContactToFolder() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ contactId, folderId }: { contactId: string; folderId: string | null }) =>
|
|
apiPut(`/contact-folders/contacts/${contactId}/move`, { folder_id: folderId }),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['unifiedContacts'] });
|
|
queryClient.invalidateQueries({ queryKey: ['contactFolders'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
// ── Folder Permissions ──
|
|
|
|
export function useFolderPermissions(folderId: string | null) {
|
|
return useQuery({
|
|
queryKey: ['folderPermissions', folderId],
|
|
queryFn: async () => {
|
|
if (!folderId) return { items: [], total: 0 };
|
|
const res = await apiGet<{ items: import('./contactFolders').FolderPermission[]; total: number }>(
|
|
`/contact-folders/${folderId}/permissions`
|
|
);
|
|
return res;
|
|
},
|
|
enabled: !!folderId,
|
|
});
|
|
}
|
|
|
|
export function useCreateFolderPermission() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({
|
|
folderId,
|
|
data,
|
|
}: {
|
|
folderId: string;
|
|
data: {
|
|
user_id?: string;
|
|
group_id?: string;
|
|
permission_level: string;
|
|
inherit_to_subfolders?: boolean;
|
|
};
|
|
}) => apiPost(`/contact-folders/${folderId}/permissions`, data),
|
|
onSuccess: (_data, vars) => {
|
|
queryClient.invalidateQueries({ queryKey: ['folderPermissions', vars.folderId] });
|
|
queryClient.invalidateQueries({ queryKey: ['contactFolders'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUpdateFolderPermission() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({
|
|
folderId,
|
|
permissionId,
|
|
data,
|
|
}: {
|
|
folderId: string;
|
|
permissionId: string;
|
|
data: { permission_level: string; inherit_to_subfolders?: boolean };
|
|
}) => apiPut(`/contact-folders/${folderId}/permissions/${permissionId}`, data),
|
|
onSuccess: (_data, vars) => {
|
|
queryClient.invalidateQueries({ queryKey: ['folderPermissions', vars.folderId] });
|
|
queryClient.invalidateQueries({ queryKey: ['contactFolders'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useDeleteFolderPermission() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ folderId, permissionId }: { folderId: string; permissionId: string }) =>
|
|
apiDelete(`/contact-folders/${folderId}/permissions/${permissionId}`),
|
|
onSuccess: (_data, vars) => {
|
|
queryClient.invalidateQueries({ queryKey: ['folderPermissions', vars.folderId] });
|
|
queryClient.invalidateQueries({ queryKey: ['contactFolders'] });
|
|
},
|
|
});
|
|
}
|