Files
umra-app/utils/programs.ts
Mekan1206 a1871510a8 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.
2025-09-20 13:23:55 +05:00

44 lines
1.5 KiB
TypeScript

import { FontAwesome, FontAwesome5, MaterialCommunityIcons } from '@expo/vector-icons';
import AsyncStorage from '@react-native-async-storage/async-storage';
export type ProgramActivity = {
time: string;
title: string;
description: string;
iconSet: 'FontAwesome' | 'FontAwesome5' | 'MaterialCommunityIcons';
iconName: React.ComponentProps<typeof FontAwesome>['name'] | React.ComponentProps<typeof FontAwesome5>['name'] | React.ComponentProps<typeof MaterialCommunityIcons>['name'];
transport?: string;
};
export type ProgramActivities = {
[date: string]: ProgramActivity[];
};
export type ProgramData = {
activities: ProgramActivities;
isStale: boolean;
};
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();
await AsyncStorage.setItem(PROGRAMS_CACHE_KEY, JSON.stringify(data));
return { activities: data, isStale: false };
} catch (error) {
console.error('Failed to fetch programs:', 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.');
}
};