import { useState } from 'react'; import { ActivityIndicator, Alert, Pressable, 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'; /** 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); return ( Solde de {card.firstName} : {card.balance} {unit(mode, card.balance)} {sorted.map((r) => { const ok = r.cost <= card.balance; return ( confirm(r)} style={({ pressed }) => ({ minHeight: 72, padding: 16, borderRadius: 16, borderWidth: ok ? 2 : 1, borderColor: ok ? c.primary : c.border, backgroundColor: ok ? (pressed ? c.primarySoft : c.surface) : c.surface2, flexDirection: 'row', alignItems: 'center', gap: 12, })} > {r.label} {ok ? `${r.cost} ${unit(mode, r.cost)}` : `Encore ${r.cost - card.balance} ${unit(mode, r.cost - card.balance)}`} {busy === r.id ? : ok && Utiliser} ); })}