feat: app complète - tous les modules

This commit is contained in:
Bastien COIGNOUX
2026-05-04 09:09:10 +02:00
parent 695d4e76d0
commit 432f8ce176
15 changed files with 1355 additions and 108 deletions

50
app/hooks/useContacts.ts Normal file
View 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),
});
}

78
app/hooks/useTaches.ts Normal file
View File

@ -0,0 +1,78 @@
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { getCurrentUserId, pb } from '@/services/pocketbase';
import type { TacheExpanded, TacheRecord } from '@/types/collections';
export type TacheCreateInput = {
titre: string;
description?: string;
date_echeance?: string;
bien?: string;
type_tache?: string;
priorite?: number;
is_urgent?: boolean;
statut?: string;
};
export function useTachesList() {
const uid = getCurrentUserId();
const queryClient = useQueryClient();
const query = useQuery({
queryKey: ['taches_list', uid],
queryFn: async () => {
if (!uid) return [] as TacheExpanded[];
return pb.collection('taches').getFullList<TacheExpanded>({
filter: `user="${uid}"`,
sort: '-id',
expand: 'bien',
});
},
enabled: Boolean(uid),
});
const invalidate = () => {
void queryClient.invalidateQueries({ queryKey: ['taches_list', uid] });
};
const createTache = useMutation({
mutationFn: async (input: TacheCreateInput) => {
if (!uid) throw new Error('Utilisateur non connecté');
return pb.collection('taches').create<TacheRecord>({
user: uid,
titre: input.titre,
description: input.description,
date_echeance: input.date_echeance,
bien: input.bien || undefined,
type_tache: input.type_tache ?? 'autre',
priorite: input.priorite ?? 2,
is_urgent: input.is_urgent ?? false,
statut: input.statut ?? 'a_faire',
});
},
onSuccess: invalidate,
});
const updateTache = useMutation({
mutationFn: async ({ id, patch }: { id: string; patch: Partial<TacheRecord> }) => {
return pb.collection('taches').update<TacheRecord>(id, patch);
},
onSuccess: invalidate,
});
const deleteTache = useMutation({
mutationFn: async (id: string) => pb.collection('taches').delete(id),
onSuccess: invalidate,
});
return {
taches: query.data ?? [],
isLoading: query.isPending,
error: query.error,
refetch: query.refetch,
createTache: createTache.mutateAsync,
updateTache: updateTache.mutateAsync,
deleteTache: deleteTache.mutateAsync,
isCreatePending: createTache.isPending,
};
}

91
app/hooks/useVisites.ts Normal file
View File

@ -0,0 +1,91 @@
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { GENERATE_RAPPORT_PATH } from '@/constants/visiteChecklist';
import { getCurrentUserId, pb } from '@/services/pocketbase';
import type { BienRecord, VisiteRecord } from '@/types/collections';
export type VisiteExpanded = VisiteRecord & {
expand?: { bien?: BienRecord };
};
export function useVisitesList() {
const uid = getCurrentUserId();
return useQuery({
queryKey: ['visites_list', uid],
queryFn: async () => {
if (!uid) return [] as VisiteRecord[];
return pb.collection('visites').getFullList<VisiteRecord>({
filter: `user="${uid}"`,
sort: '-date_visite',
});
},
enabled: Boolean(uid),
});
}
export function useVisiteDetail(id: string | undefined) {
return useQuery({
queryKey: ['visite_detail', id],
queryFn: async () => {
if (!id) throw new Error('id');
return pb.collection('visites').getOne<VisiteExpanded>(id, { expand: 'bien' });
},
enabled: Boolean(id),
});
}
export type VisitePatch = Partial<
Pick<
VisiteRecord,
| 'notes_brutes'
| 'checklist_reponses'
| 'estimation_travaux_min'
| 'estimation_travaux_max'
| 'avis_global'
| 'score_opportunite'
| 'rapport_genere'
>
>;
export function useVisiteUpdate() {
const queryClient = useQueryClient();
const uid = getCurrentUserId();
return async (visiteId: string, patch: VisitePatch) => {
const updated = await pb.collection('visites').update<VisiteRecord>(visiteId, patch);
void queryClient.invalidateQueries({ queryKey: ['visite_detail', visiteId] });
void queryClient.invalidateQueries({ queryKey: ['visites_list', uid] });
return updated;
};
}
export type GenerateRapportInput = {
notes_brutes: string;
checklist_reponses: Record<string, string>;
bien_info: Record<string, unknown>;
};
export async function requestGenerateRapport(body: GenerateRapportInput): Promise<string> {
const res = await pb.send<{ rapport?: string; message?: string }>(GENERATE_RAPPORT_PATH, {
method: 'POST',
body: JSON.stringify(body),
headers: { 'Content-Type': 'application/json' },
});
if (res && typeof res === 'object' && 'rapport' in res && typeof res.rapport === 'string') {
return res.rapport;
}
const msg =
res && typeof res === 'object' && 'message' in res && typeof res.message === 'string'
? res.message
: 'Réponse serveur inattendue';
throw new Error(msg);
}
export async function appendVisitePhoto(visiteId: string, localUri: string): Promise<VisiteRecord> {
const form = new FormData();
form.append('photos', {
uri: localUri,
name: 'photo.jpg',
type: 'image/jpeg',
} as unknown as Blob);
return pb.collection('visites').update<VisiteRecord>(visiteId, form);
}