Debugging
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import React, { useEffect, useMemo, useState, useRef } from 'react';
|
import React, { useEffect, useMemo, useState, useRef, memo } from 'react';
|
||||||
import { useForm, Controller } from 'react-hook-form';
|
import { useForm, Controller } from 'react-hook-form';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { zodResolver } from '@hookform/resolvers/zod';
|
import { zodResolver } from '@hookform/resolvers/zod';
|
||||||
@@ -30,7 +30,7 @@ const schema = z.object({
|
|||||||
message: 'Cannot specify both odometer reading and trip distance'
|
message: 'Cannot specify both odometer reading and trip distance'
|
||||||
});
|
});
|
||||||
|
|
||||||
export const FuelLogForm: React.FC<{ onSuccess?: () => void; initial?: Partial<CreateFuelLogRequest> }> = ({ onSuccess, initial }) => {
|
const FuelLogFormComponent: React.FC<{ onSuccess?: () => void; initial?: Partial<CreateFuelLogRequest> }> = ({ onSuccess, initial }) => {
|
||||||
const { userSettings } = useUserSettings();
|
const { userSettings } = useUserSettings();
|
||||||
const { createFuelLog, isLoading } = useFuelLogs();
|
const { createFuelLog, isLoading } = useFuelLogs();
|
||||||
const [useOdometer, setUseOdometer] = useState(false);
|
const [useOdometer, setUseOdometer] = useState(false);
|
||||||
@@ -46,13 +46,23 @@ export const FuelLogForm: React.FC<{ onSuccess?: () => void; initial?: Partial<C
|
|||||||
} as any
|
} as any
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// DEBUG: Log component renders and form state
|
||||||
|
console.log('[FuelLogForm] Render - formInitialized:', formInitialized.current, 'isLoading:', isLoading);
|
||||||
|
|
||||||
// Prevent form reset after initial load
|
// Prevent form reset after initial load
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!formInitialized.current) {
|
if (!formInitialized.current) {
|
||||||
formInitialized.current = true;
|
formInitialized.current = true;
|
||||||
|
console.log('[FuelLogForm] Form initialized');
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
// DEBUG: Watch for form value changes
|
||||||
|
const vehicleId = watch('vehicleId');
|
||||||
|
useEffect(() => {
|
||||||
|
console.log('[FuelLogForm] Vehicle ID changed:', vehicleId);
|
||||||
|
}, [vehicleId]);
|
||||||
|
|
||||||
const watched = watch(['fuelUnits', 'costPerUnit']);
|
const watched = watch(['fuelUnits', 'costPerUnit']);
|
||||||
const [fuelUnitsRaw, costPerUnitRaw] = watched as [string | number | undefined, string | number | undefined];
|
const [fuelUnitsRaw, costPerUnitRaw] = watched as [string | number | undefined, string | number | undefined];
|
||||||
|
|
||||||
@@ -167,3 +177,5 @@ export const FuelLogForm: React.FC<{ onSuccess?: () => void; initial?: Partial<C
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const FuelLogForm = memo(FuelLogFormComponent);
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,9 @@ interface Props {
|
|||||||
const VehicleSelectorComponent: React.FC<Props> = ({ value, onChange, error, required, disabled }) => {
|
const VehicleSelectorComponent: React.FC<Props> = ({ value, onChange, error, required, disabled }) => {
|
||||||
const { data: vehicles, isLoading } = useVehicles();
|
const { data: vehicles, isLoading } = useVehicles();
|
||||||
|
|
||||||
|
// DEBUG: Log vehicle selector renders and data changes
|
||||||
|
console.log('[VehicleSelector] Render - value:', value, 'vehicles count:', vehicles?.length, 'isLoading:', isLoading);
|
||||||
|
|
||||||
if (!isLoading && (vehicles?.length || 0) === 0) {
|
if (!isLoading && (vehicles?.length || 0) === 0) {
|
||||||
return (
|
return (
|
||||||
<Box p={2} borderRadius={1} bgcolor={'background.default'}>
|
<Box p={2} borderRadius={1} bgcolor={'background.default'}>
|
||||||
|
|||||||
@@ -15,15 +15,19 @@ export const FuelLogsPage: React.FC = () => {
|
|||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
const [editingLog, setEditingLog] = useState<FuelLogResponse | null>(null);
|
const [editingLog, setEditingLog] = useState<FuelLogResponse | null>(null);
|
||||||
|
|
||||||
|
// DEBUG: Log page renders
|
||||||
|
console.log('[FuelLogsPage] Render - fuel logs count:', fuelLogs?.length, 'isLoading:', isLoading, 'error:', !!error);
|
||||||
|
|
||||||
const handleEdit = (log: FuelLogResponse) => {
|
const handleEdit = (log: FuelLogResponse) => {
|
||||||
setEditingLog(log);
|
setEditingLog(log);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDelete = async (_logId: string) => {
|
const handleDelete = async (_logId: string) => {
|
||||||
try {
|
try {
|
||||||
// Invalidate queries to refresh the data
|
console.log('[FuelLogsPage] handleDelete called - using targeted query updates');
|
||||||
queryClient.invalidateQueries({ queryKey: ['fuelLogs'] });
|
// Use targeted invalidation instead of broad invalidation
|
||||||
queryClient.invalidateQueries({ queryKey: ['fuelLogsStats'] });
|
// This prevents unnecessary re-renders of the form
|
||||||
|
queryClient.refetchQueries({ queryKey: ['fuelLogs', 'all'] });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to refresh fuel logs after delete:', error);
|
console.error('Failed to refresh fuel logs after delete:', error);
|
||||||
}
|
}
|
||||||
@@ -31,10 +35,11 @@ export const FuelLogsPage: React.FC = () => {
|
|||||||
|
|
||||||
const handleSaveEdit = async (id: string, data: UpdateFuelLogRequest) => {
|
const handleSaveEdit = async (id: string, data: UpdateFuelLogRequest) => {
|
||||||
try {
|
try {
|
||||||
|
console.log('[FuelLogsPage] handleSaveEdit called - using targeted query updates');
|
||||||
await fuelLogsApi.update(id, data);
|
await fuelLogsApi.update(id, data);
|
||||||
// Invalidate queries to refresh the data
|
// Use targeted refetch instead of broad invalidation
|
||||||
queryClient.invalidateQueries({ queryKey: ['fuelLogs'] });
|
// This prevents unnecessary re-renders of the form
|
||||||
queryClient.invalidateQueries({ queryKey: ['fuelLogsStats'] });
|
queryClient.refetchQueries({ queryKey: ['fuelLogs', 'all'] });
|
||||||
setEditingLog(null);
|
setEditingLog(null);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to update fuel log:', error);
|
console.error('Failed to update fuel log:', error);
|
||||||
|
|||||||
@@ -19,9 +19,15 @@ interface ApiError {
|
|||||||
|
|
||||||
export const useVehicles = () => {
|
export const useVehicles = () => {
|
||||||
const { isAuthenticated, isLoading } = useAuth0();
|
const { isAuthenticated, isLoading } = useAuth0();
|
||||||
|
|
||||||
|
console.log('[useVehicles] Hook called - isAuthenticated:', isAuthenticated, 'isLoading:', isLoading);
|
||||||
|
|
||||||
return useQuery({
|
return useQuery({
|
||||||
queryKey: ['vehicles'],
|
queryKey: ['vehicles'],
|
||||||
queryFn: vehiclesApi.getAll,
|
queryFn: () => {
|
||||||
|
console.log('[useVehicles] Query function called - fetching vehicles');
|
||||||
|
return vehiclesApi.getAll();
|
||||||
|
},
|
||||||
enabled: isAuthenticated && !isLoading,
|
enabled: isAuthenticated && !isLoading,
|
||||||
staleTime: 5 * 60 * 1000, // 5 minutes - prevent unnecessary refetches
|
staleTime: 5 * 60 * 1000, // 5 minutes - prevent unnecessary refetches
|
||||||
gcTime: 10 * 60 * 1000, // 10 minutes cache time
|
gcTime: 10 * 60 * 1000, // 10 minutes cache time
|
||||||
@@ -36,6 +42,12 @@ export const useVehicles = () => {
|
|||||||
retryDelay: (attemptIndex) => Math.min(1000 * 2 ** attemptIndex, 30000),
|
retryDelay: (attemptIndex) => Math.min(1000 * 2 ** attemptIndex, 30000),
|
||||||
refetchOnWindowFocus: false, // Prevent refetch on window focus
|
refetchOnWindowFocus: false, // Prevent refetch on window focus
|
||||||
refetchOnMount: false, // Only fetch on mount if data is stale
|
refetchOnMount: false, // Only fetch on mount if data is stale
|
||||||
|
onSuccess: (data) => {
|
||||||
|
console.log('[useVehicles] Query success - vehicles loaded:', data?.length);
|
||||||
|
},
|
||||||
|
onError: (error) => {
|
||||||
|
console.log('[useVehicles] Query error:', error);
|
||||||
|
},
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user