Mobile Work

This commit is contained in:
Eric Gullickson
2025-09-19 11:33:31 -05:00
parent 040da4c759
commit 3588372cef
7 changed files with 189 additions and 13 deletions

View File

@@ -1,11 +1,23 @@
import { useQuery } from '@tanstack/react-query';
import { useAuth0 } from '@auth0/auth0-react';
import { fuelLogsApi } from '../api/fuel-logs.api';
import { FuelType, FuelGradeOption } from '../types/fuel-logs.types';
export const useFuelGrades = (fuelType: FuelType) => {
const { isAuthenticated, isLoading: authLoading } = useAuth0();
const { data, isLoading, error } = useQuery<FuelGradeOption[]>({
queryKey: ['fuelGrades', fuelType],
queryFn: () => fuelLogsApi.getFuelGrades(fuelType),
enabled: isAuthenticated && !authLoading,
retry: (failureCount, error: any) => {
// Retry 401 errors up to 3 times for mobile auth timing issues
if (error?.response?.status === 401 && failureCount < 3) {
console.log(`[Mobile Auth] Fuel grades API retry ${failureCount + 1}/3 for 401 error`);
return true;
}
return false;
},
retryDelay: (attemptIndex) => Math.min(1000 * 2 ** attemptIndex, 30000),
});
return { fuelGrades: data || [], isLoading, error };
};

View File

@@ -1,19 +1,40 @@
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { useAuth0 } from '@auth0/auth0-react';
import { fuelLogsApi } from '../api/fuel-logs.api';
import { CreateFuelLogRequest, FuelLogResponse, EnhancedFuelStats } from '../types/fuel-logs.types';
export const useFuelLogs = (vehicleId?: string) => {
const { isAuthenticated, isLoading } = useAuth0();
const queryClient = useQueryClient();
const logsQuery = useQuery<FuelLogResponse[]>({
queryKey: ['fuelLogs', vehicleId || 'all'],
queryFn: () => (vehicleId ? fuelLogsApi.getFuelLogsByVehicle(vehicleId) : fuelLogsApi.getUserFuelLogs()),
enabled: isAuthenticated && !isLoading,
retry: (failureCount, error: any) => {
// Retry 401 errors up to 3 times for mobile auth timing issues
if (error?.response?.status === 401 && failureCount < 3) {
console.log(`[Mobile Auth] Fuel logs API retry ${failureCount + 1}/3 for 401 error`);
return true;
}
return false;
},
retryDelay: (attemptIndex) => Math.min(1000 * 2 ** attemptIndex, 30000),
});
const statsQuery = useQuery<EnhancedFuelStats>({
queryKey: ['fuelLogsStats', vehicleId],
queryFn: () => fuelLogsApi.getVehicleStats(vehicleId!),
enabled: !!vehicleId,
enabled: !!vehicleId && isAuthenticated && !isLoading,
retry: (failureCount, error: any) => {
// Retry 401 errors up to 3 times for mobile auth timing issues
if (error?.response?.status === 401 && failureCount < 3) {
console.log(`[Mobile Auth] Fuel stats API retry ${failureCount + 1}/3 for 401 error`);
return true;
}
return false;
},
retryDelay: (attemptIndex) => Math.min(1000 * 2 ** attemptIndex, 30000),
});
const createMutation = useMutation({

View File

@@ -1,4 +1,5 @@
import { useCallback } from 'react';
import { safeStorage } from '../../../core/utils/safe-storage';
export interface SettingsState {
darkMode: boolean;
@@ -15,19 +16,19 @@ const SETTINGS_STORAGE_KEY = 'motovaultpro-mobile-settings';
export const useSettingsPersistence = () => {
const loadSettings = useCallback((): SettingsState | null => {
try {
const stored = localStorage.getItem(SETTINGS_STORAGE_KEY);
const stored = safeStorage.getItem(SETTINGS_STORAGE_KEY);
return stored ? JSON.parse(stored) : null;
} catch (error) {
console.error('Error loading settings:', error);
console.error('[Settings] Error loading settings:', error);
return null;
}
}, []);
const saveSettings = useCallback((settings: SettingsState) => {
try {
localStorage.setItem(SETTINGS_STORAGE_KEY, JSON.stringify(settings));
safeStorage.setItem(SETTINGS_STORAGE_KEY, JSON.stringify(settings));
} catch (error) {
console.error('Error saving settings:', error);
console.error('[Settings] Error saving settings:', error);
}
}, []);

View File

@@ -23,6 +23,15 @@ export const useVehicles = () => {
queryKey: ['vehicles'],
queryFn: vehiclesApi.getAll,
enabled: isAuthenticated && !isLoading,
retry: (failureCount, error: any) => {
// Retry 401 errors up to 3 times for mobile auth timing issues
if (error?.response?.status === 401 && failureCount < 3) {
console.log(`[Mobile Auth] API retry ${failureCount + 1}/3 for 401 error`);
return true;
}
return false;
},
retryDelay: (attemptIndex) => Math.min(1000 * 2 ** attemptIndex, 30000),
});
};
@@ -32,6 +41,15 @@ export const useVehicle = (id: string) => {
queryKey: ['vehicles', id],
queryFn: () => vehiclesApi.getById(id),
enabled: !!id && isAuthenticated && !isLoading,
retry: (failureCount, error: any) => {
// Retry 401 errors up to 3 times for mobile auth timing issues
if (error?.response?.status === 401 && failureCount < 3) {
console.log(`[Mobile Auth] API retry ${failureCount + 1}/3 for 401 error`);
return true;
}
return false;
},
retryDelay: (attemptIndex) => Math.min(1000 * 2 ** attemptIndex, 30000),
});
};