import { useState } from 'react'; import { ActivityIndicator, Alert, Pressable, StyleSheet, View } from 'react-native'; import { router } from 'expo-router'; import * as Haptics from 'expo-haptics'; import { api, Reward } from '../../../src/api'; import { useStore } from '../../../src/session'; import { useCard } from '../../../src/useCard'; import { ErrorScreen } from '../../../src/errors'; import { Button, Screen, T, unit } from '../../../src/ui'; import { font, useColors } from '../../../src/theme'; import { Icon } from '../../../src/icons'; /** Choix d'une récompense (maquette 6) : les accessibles d'abord, les autres grisées. */ export default function Rewards() { const c = useColors(); const store = useStore(); const { serial, card, error: loadError, reload } = useCard(); const [busy, setBusy] = useState(null); const [error, setError] = useState(null); if (loadError || error) return ; if (!card) return ; const mode = store.design.pointsMode; function confirm(r: Reward) { Alert.alert(r.label, `Déduire ${r.cost} ${unit(mode, r.cost)} du solde de ${card!.firstName} ?`, [ { text: 'Annuler', style: 'cancel' }, { text: 'Valider', onPress: () => redeem(r) }, ]); } async function redeem(r: Reward) { setBusy(r.id); try { const res = await api.post<{ balance: number; firstName: string }>(`/api/staff/cards/${encodeURIComponent(serial)}/redeem`, { rewardId: r.id, }); Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success); router.replace({ pathname: '/done', params: { kind: 'redeem', firstName: res.firstName, reward: r.label, points: String(r.cost), balance: String(res.balance) }, }); } catch (e) { Haptics.notificationAsync(Haptics.NotificationFeedbackType.Error); setError(e); } finally { setBusy(null); } } const sorted = [...store.design.rewards].sort((a, b) => Number(b.cost <= card.balance) - Number(a.cost <= card.balance) || a.cost - b.cost); const available = sorted.filter((r) => r.cost <= card.balance); const later = sorted.filter((r) => r.cost > card.balance); const item = (r: Reward) => { const ok = r.cost <= card.balance; const progress = Math.min(card.balance / r.cost, 1); return ( confirm(r)} style={({ pressed }) => ({ padding: 16, borderRadius: 18, borderWidth: ok ? 2 : StyleSheet.hairlineWidth, borderColor: ok ? c.primary : c.border, backgroundColor: ok ? (pressed ? c.primarySoft : c.surface) : c.surface, gap: 10, opacity: ok ? 1 : 0.75, })} > {r.label} {r.cost} {unit(mode, r.cost)} {!ok && ` · encore ${r.cost - card.balance}`} {busy === r.id ? ( ) : ( ok && ( Utiliser ) )} {/* Progression vers une récompense pas encore atteinte */} {!ok && ( )} ); }; return ( {/* Solde du client aux couleurs de la carte */} Solde de {card.firstName} {card.balance} {unit(mode, card.balance)} {available.length > 0 ? ( Disponibles {available.map(item)} ) : ( Aucune récompense disponible pour l'instant. )} {later.length > 0 && ( Prochainement {later.map(item)} )}