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