51 lines
1.5 KiB
TypeScript
51 lines
1.5 KiB
TypeScript
import { useQuery } from '@tanstack/react-query';
|
|
|
|
import { getCurrentUserId, pb } from '@/services/pocketbase';
|
|
import type { BienRecord, ContactRecord } from '@/types/collections';
|
|
|
|
export function useContactDetail(id: string | undefined) {
|
|
return useQuery({
|
|
queryKey: ['contact', id],
|
|
queryFn: async () => {
|
|
if (!id) throw new Error('id');
|
|
return pb.collection('contacts').getOne<ContactRecord>(id);
|
|
},
|
|
enabled: Boolean(id),
|
|
});
|
|
}
|
|
|
|
export function useContactsList() {
|
|
const uid = getCurrentUserId();
|
|
return useQuery({
|
|
queryKey: ['contacts_list', uid],
|
|
queryFn: async () => {
|
|
if (!uid) return [] as ContactRecord[];
|
|
const list = await pb.collection('contacts').getFullList<ContactRecord>({
|
|
filter: `user="${uid}"`,
|
|
sort: '-id',
|
|
});
|
|
return [...list].sort((a, b) => {
|
|
const an = `${a.prenom ?? ''} ${a.nom}`.trim().toLowerCase();
|
|
const bn = `${b.prenom ?? ''} ${b.nom}`.trim().toLowerCase();
|
|
return an.localeCompare(bn, 'fr');
|
|
});
|
|
},
|
|
enabled: Boolean(uid),
|
|
});
|
|
}
|
|
|
|
export function useContactBiens(contactId: string | undefined) {
|
|
const uid = getCurrentUserId();
|
|
return useQuery({
|
|
queryKey: ['contact_biens', uid, contactId],
|
|
queryFn: async () => {
|
|
if (!uid || !contactId) return [] as BienRecord[];
|
|
return pb.collection('biens').getFullList<BienRecord>({
|
|
filter: `user="${uid}" && source_contact="${contactId}"`,
|
|
sort: '-id',
|
|
});
|
|
},
|
|
enabled: Boolean(uid && contactId),
|
|
});
|
|
}
|