Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | /**
* Example: Offline-Aware Component
*
* Demonstrates best practices for implementing offline mode graceful degradation.
* This example shows how to:
* 1. Use useOfflineAwareData hook for data fetching
* 2. Display offline banners
* 3. Handle loading and error states
* 4. Provide retry functionality
*
* NOTE: This is a reference implementation. Copy patterns from this file
* when adding offline handling to actual screens.
*/
import React from 'react';
import { View, Text, StyleSheet, FlatList, TouchableOpacity, ActivityIndicator, RefreshControl , Alert } from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import { OfflineBanner } from '@/components/OfflineBanner';
import { useOfflineAwareData } from '@/hooks/useOfflineAwareData';
import { offlineAwareApi } from '@/services/offlineAwareApi';
import { AppColors, Spacing, FontSizes } from '@/constants/theme';
import type { Beneficiary } from '@/types';
/**
* Example: Form with Offline Mutation
*/
import { useOfflineAwareMutation } from '@/hooks/useOfflineAwareData';
/**
* Example: Beneficiaries List with Offline Handling
*/
export function BeneficiariesListExample() {
// Use offline-aware data hook
const {
data: beneficiaries,
loading,
error,
refetch,
refetching,
errorMessage,
isOfflineError,
} = useOfflineAwareData(
() => offlineAwareApi.getAllBeneficiaries(),
[], // Dependencies
{
refetchOnReconnect: true, // Auto-refetch when back online
pollInterval: 0, // Disable polling (set to e.g., 30000 for 30s polling)
}
);
// Loading state (initial load)
if (loading) {
return (
<SafeAreaView style={styles.container}>
<OfflineBanner />
<View style={styles.centerContent}>
<ActivityIndicator size="large" color={AppColors.primary} />
<Text style={styles.loadingText}>Loading beneficiaries...</Text>
</View>
</SafeAreaView>
);
}
// Error state (with retry button)
if (error && !beneficiaries) {
return (
<SafeAreaView style={styles.container}>
<OfflineBanner />
<View style={styles.centerContent}>
<Text style={styles.errorTitle}>
{isOfflineError ? 'You\'re Offline' : 'Something Went Wrong'}
</Text>
<Text style={styles.errorMessage}>{errorMessage}</Text>
<TouchableOpacity style={styles.retryButton} onPress={refetch}>
<Text style={styles.retryButtonText}>Try Again</Text>
</TouchableOpacity>
</View>
</SafeAreaView>
);
}
// Empty state
if (!beneficiaries || beneficiaries.length === 0) {
return (
<SafeAreaView style={styles.container}>
<OfflineBanner />
<View style={styles.centerContent}>
<Text style={styles.emptyTitle}>No Beneficiaries</Text>
<Text style={styles.emptyMessage}>Add someone to start monitoring</Text>
</View>
</SafeAreaView>
);
}
// Success state - show data
return (
<SafeAreaView style={styles.container}>
{/* Offline banner - auto-shows when offline */}
<OfflineBanner />
{/* List with pull-to-refresh */}
<FlatList
data={beneficiaries}
keyExtractor={(item) => item.id.toString()}
renderItem={({ item }) => (
<View style={styles.listItem}>
<Text style={styles.itemName}>{item.displayName}</Text>
<Text style={styles.itemStatus}>{item.status}</Text>
</View>
)}
refreshControl={
<RefreshControl
refreshing={refetching}
onRefresh={refetch}
tintColor={AppColors.primary}
/>
}
contentContainerStyle={styles.listContent}
/>
{/* Show error banner if refetch fails (but we still have cached data) */}
{error && (
<View style={styles.errorBanner}>
<Text style={styles.errorBannerText}>{errorMessage}</Text>
<TouchableOpacity onPress={refetch}>
<Text style={styles.errorBannerRetry}>Retry</Text>
</TouchableOpacity>
</View>
)}
</SafeAreaView>
);
}
export function CreateBeneficiaryExample() {
const [name, setName] = React.useState('');
const { mutate, loading, errorMessage, isOfflineError } = useOfflineAwareMutation(
(data: { name: string }) => offlineAwareApi.createBeneficiary(data),
{
offlineMessage: 'Cannot create beneficiary while offline',
onSuccess: (beneficiary) => {
Alert.alert('Success', `Added ${beneficiary.name}`);
setName('');
},
onError: (error) => {
Alert.alert('Error', errorMessage || 'Failed to add beneficiary');
},
}
);
const handleSubmit = async () => {
if (!name.trim()) {
Alert.alert('Error', 'Please enter a name');
return;
}
await mutate({ name: name.trim() });
};
return (
<SafeAreaView style={styles.container}>
<OfflineBanner />
<View style={styles.formContainer}>
<Text style={styles.formLabel}>Beneficiary Name</Text>
{/* Add TextInput here */}
{errorMessage && (
<Text style={styles.formError}>{errorMessage}</Text>
)}
<TouchableOpacity
style={[styles.submitButton, loading && styles.submitButtonDisabled]}
onPress={handleSubmit}
disabled={loading}
>
{loading ? (
<ActivityIndicator color="#fff" />
) : (
<Text style={styles.submitButtonText}>Add Beneficiary</Text>
)}
</TouchableOpacity>
</View>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: AppColors.background,
},
centerContent: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
padding: Spacing.lg,
},
loadingText: {
marginTop: Spacing.md,
fontSize: FontSizes.md,
color: AppColors.textSecondary,
},
errorTitle: {
fontSize: FontSizes.xl,
fontWeight: '600',
color: AppColors.textPrimary,
marginBottom: Spacing.sm,
},
errorMessage: {
fontSize: FontSizes.md,
color: AppColors.textSecondary,
textAlign: 'center',
marginBottom: Spacing.lg,
},
retryButton: {
backgroundColor: AppColors.primary,
paddingHorizontal: Spacing.lg,
paddingVertical: Spacing.md,
borderRadius: 8,
},
retryButtonText: {
color: '#fff',
fontSize: FontSizes.md,
fontWeight: '600',
},
emptyTitle: {
fontSize: FontSizes.xl,
fontWeight: '600',
color: AppColors.textPrimary,
marginBottom: Spacing.sm,
},
emptyMessage: {
fontSize: FontSizes.md,
color: AppColors.textSecondary,
},
listContent: {
padding: Spacing.md,
},
listItem: {
backgroundColor: AppColors.backgroundSecondary,
padding: Spacing.md,
borderRadius: 8,
marginBottom: Spacing.sm,
flexDirection: 'row',
justifyContent: 'space-between',
},
itemName: {
fontSize: FontSizes.md,
fontWeight: '600',
color: AppColors.textPrimary,
},
itemStatus: {
fontSize: FontSizes.sm,
color: AppColors.textSecondary,
},
errorBanner: {
backgroundColor: AppColors.danger,
padding: Spacing.md,
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
},
errorBannerText: {
flex: 1,
color: '#fff',
fontSize: FontSizes.sm,
},
errorBannerRetry: {
color: '#fff',
fontSize: FontSizes.sm,
fontWeight: '600',
},
formContainer: {
padding: Spacing.lg,
},
formLabel: {
fontSize: FontSizes.md,
fontWeight: '600',
color: AppColors.textPrimary,
marginBottom: Spacing.sm,
},
formError: {
fontSize: FontSizes.sm,
color: AppColors.danger,
marginTop: Spacing.sm,
},
submitButton: {
backgroundColor: AppColors.primary,
padding: Spacing.md,
borderRadius: 8,
alignItems: 'center',
marginTop: Spacing.lg,
},
submitButtonDisabled: {
opacity: 0.6,
},
submitButtonText: {
color: '#fff',
fontSize: FontSizes.md,
fontWeight: '600',
},
});
|