feat: app complète - tous les modules
This commit is contained in:
50
app/hooks/useContacts.ts
Normal file
50
app/hooks/useContacts.ts
Normal file
@ -0,0 +1,50 @@
|
||||
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),
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user