30 lines
1.2 KiB
TypeScript
30 lines
1.2 KiB
TypeScript
import { useQuery, UseQueryOptions } from '@tanstack/react-query';
|
|
import { useInject } from '@/lib/di/hooks/useInject';
|
|
import { DRIVER_SERVICE_TOKEN } from '@/lib/di/tokens';
|
|
import { enhanceQueryResult } from '@/lib/di/hooks/useReactQueryWithApiError';
|
|
import { ApiError } from '@/lib/api/base/ApiError';
|
|
import { DriverProfileViewModel, type DriverProfileViewModelData } from '@/lib/view-models/DriverProfileViewModel';
|
|
|
|
export function useDriverProfile(
|
|
driverId: string,
|
|
options?: Omit<UseQueryOptions<DriverProfileViewModel, ApiError>, 'queryKey' | 'queryFn'>
|
|
) {
|
|
const driverService = useInject(DRIVER_SERVICE_TOKEN);
|
|
|
|
const queryResult = useQuery({
|
|
queryKey: ['driverProfile', driverId],
|
|
queryFn: async () => {
|
|
const result = await driverService.getDriverProfile(driverId);
|
|
if (result.isErr()) {
|
|
const error = result.getError();
|
|
throw new ApiError(error.message, 'SERVER_ERROR', { timestamp: new globalThis.Date().toISOString() });
|
|
}
|
|
return new DriverProfileViewModel(result.unwrap() as unknown as DriverProfileViewModelData);
|
|
},
|
|
enabled: !!driverId,
|
|
...options,
|
|
});
|
|
|
|
return enhanceQueryResult(queryResult);
|
|
}
|