Add stale data handling in Programs component
- Enhanced the getPrograms function to return a stale flag indicating if cached data is being used. - Implemented a warning message in the Programs component to inform users when stale data is displayed. - Updated styles for the warning message to improve visibility.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
|
||||
import { FontAwesome, FontAwesome5, MaterialCommunityIcons } from '@expo/vector-icons';
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
|
||||
export type ProgramActivity = {
|
||||
time: string;
|
||||
@@ -14,18 +15,29 @@ export type ProgramActivities = {
|
||||
[date: string]: ProgramActivity[];
|
||||
};
|
||||
|
||||
const PROGRAMS_URL = 'http://kepilhyzmat.com/assets/programs.json';
|
||||
export type ProgramData = {
|
||||
activities: ProgramActivities;
|
||||
isStale: boolean;
|
||||
};
|
||||
|
||||
export const getPrograms = async (): Promise<ProgramActivities> => {
|
||||
const PROGRAMS_URL = 'http://kepilhyzmat.com/assets/programs.json';
|
||||
const PROGRAMS_CACHE_KEY = 'program_activities_cache';
|
||||
|
||||
export const getPrograms = async (): Promise<ProgramData> => {
|
||||
try {
|
||||
const response = await fetch(PROGRAMS_URL);
|
||||
if (!response.ok) {
|
||||
throw new Error(`Request failed with status ${response.status}`);
|
||||
}
|
||||
const data: ProgramActivities = await response.json();
|
||||
return data;
|
||||
await AsyncStorage.setItem(PROGRAMS_CACHE_KEY, JSON.stringify(data));
|
||||
return { activities: data, isStale: false };
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch programs:', error);
|
||||
throw error;
|
||||
const cachedData = await AsyncStorage.getItem(PROGRAMS_CACHE_KEY);
|
||||
if (cachedData) {
|
||||
return { activities: JSON.parse(cachedData), isStale: true };
|
||||
}
|
||||
throw new Error('Failed to fetch programs and no cache available.');
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user