- Refactor Profile and Login screens to use AuthContext for user data

- Enhance RequestPermitModal with multiple time-off types and validation
- Implement CalendarWidget for visualizing time-off requests
- Improve API error handling and token management
- Add utility functions for consistent date and time formatting
- Clean up unused mock data and update types
This commit is contained in:
2025-12-10 18:21:08 +01:00
parent 49b6ecadb2
commit b9807f6cc2
13 changed files with 503 additions and 343 deletions

View File

@ -1,43 +1,55 @@
import axios from 'axios';
import AsyncStorage from '@react-native-async-storage/async-storage';
import * as SecureStore from 'expo-secure-store';
// CONFIGURAZIONE GATEWAY (Adatta questi valori al tuo DDEV)
// Se sei su emulatore Android usa 10.0.2.2, se su iOS o fisico usa il tuo IP LAN (es 192.168.1.x)
const GATEWAY_BASE_URL = "http://10.0.2.2:PORTA";
export const GATEWAY_ENDPOINT = `${GATEWAY_BASE_URL}/tuo_endpoint_gateway`;
export const GATEWAY_TOKEN = "il_tuo_token_statico_se_esiste";
const API_BASE_URL = `http://10.0.2.2:32768/mobile`;
export const KEY_TOKEN = 'auth_key';
// Crea un'istanza di axios
const api = axios.create({
baseURL: API_BASE_URL,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
timeout: 10000, // 10 secondi timeout
});
// INTERCEPTOR: Configura ogni chiamata al volo
api.interceptors.request.use(async (config) => {
try {
// 1. Cerchiamo se abbiamo già salvato l'URL finale del backend (post-gateway)
const savedBaseUrl = await SecureStore.getItemAsync('App_URL');
if (savedBaseUrl) {
config.baseURL = savedBaseUrl;
} else {
// Se non c'è, usiamo il gateway come fallback o gestiamo l'errore
// (La logica di init nell'AuthContext dovrebbe averlo già settato)
config.baseURL = GATEWAY_BASE_URL;
}
// 2. Cerchiamo il token utente
const token = await SecureStore.getItemAsync('auth-token');
// Interceptor: Aggiunge il token a OGNI richiesta se esiste
api.interceptors.request.use(
async (config) => {
const token = await SecureStore.getItemAsync(KEY_TOKEN);
if (token) {
// Adatta l'header in base al tuo backend (Bearer, x-access-tokens, etc.)
config.headers.Authorization = `Bearer ${token}`;
}
} catch (error) {
console.error("Errore interceptor:", error);
console.log(`[API REQUEST] ${config.method?.toUpperCase()} ${config.url}`);
return config;
},
(error) => {
return Promise.reject(error);
}
return config;
});
);
// Interceptor: Gestione errori globale (es. token scaduto)
api.interceptors.response.use(
(response) => response,
async (error) => {
const originalRequest = error.config;
if (error.response) {
console.error('[API ERROR]', error.response.status, error.response.data);
// Se riceviamo 401 (Unauthorized), potremmo voler fare il logout forzato
if (error.response.status === 401) {
// TODO: Qui potresti emettere un evento per disconnettere l'utente
await SecureStore.deleteItemAsync(KEY_TOKEN);
}
} else {
console.error('[API NETWORK ERROR]', error.message);
}
return Promise.reject(error);
}
);
export default api;