MonEspacePro/pro/app/client/[serial]/rewards.tsx
Tony AVERTY e7e00b9a6f App : page des récompenses retravaillée
Solde aux couleurs de la carte, récompenses en deux sections (Disponibles /
Prochainement), bouton « Utiliser » en pastille, barre de progression vers
les récompenses pas encore atteintes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 14:38:24 +02:00

143 lines
5.9 KiB
TypeScript

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';
/** 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<string | null>(null);
const [error, setError] = useState<unknown>(null);
if (loadError || error) return <ErrorScreen error={loadError ?? error} onRetry={loadError ? reload : undefined} />;
if (!card) return <Screen style={{ justifyContent: 'center' }}><ActivityIndicator /></Screen>;
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 (
<Pressable
key={r.id}
accessibilityRole="button"
accessibilityState={{ disabled: !ok || !!busy }}
disabled={!ok || !!busy}
onPress={() => 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,
})}
>
<View style={{ flexDirection: 'row', alignItems: 'center', gap: 12 }}>
<View style={{ width: 46, height: 46, borderRadius: 23, backgroundColor: ok ? c.warnBg : c.surface2, alignItems: 'center', justifyContent: 'center' }}>
<T style={{ fontSize: 22 }}>{ok ? '🎁' : '🔒'}</T>
</View>
<View style={{ flex: 1, gap: 2 }}>
<T v="label" style={{ fontSize: 17, color: ok ? c.text : c.text3 }}>{r.label}</T>
<T v="muted" style={{ fontSize: 14 }}>
{r.cost} {unit(mode, r.cost)}
{!ok && ` · encore ${r.cost - card.balance}`}
</T>
</View>
{busy === r.id ? (
<ActivityIndicator />
) : (
ok && (
<View style={{ paddingHorizontal: 14, paddingVertical: 8, borderRadius: 999, backgroundColor: c.primary }}>
<T style={{ color: c.onPrimary, fontFamily: font.bold, fontSize: 15 }}>Utiliser</T>
</View>
)
)}
</View>
{/* Progression vers une récompense pas encore atteinte */}
{!ok && (
<View style={{ height: 6, borderRadius: 3, backgroundColor: c.surface2, overflow: 'hidden' }}>
<View style={{ width: `${Math.round(progress * 100)}%`, height: 6, borderRadius: 3, backgroundColor: c.accent }} />
</View>
)}
</Pressable>
);
};
return (
<Screen scroll>
{/* Solde du client aux couleurs de la carte */}
<View style={{ backgroundColor: store.design.backgroundColor, borderRadius: 20, padding: 18, flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between' }}>
<View>
<T style={{ color: store.design.foregroundColor, opacity: 0.8, fontSize: 14 }}>Solde de {card.firstName}</T>
<T style={{ color: store.design.foregroundColor, fontFamily: font.extrabold, fontSize: 32, lineHeight: 38 }}>
{card.balance} <T style={{ color: store.design.foregroundColor, fontSize: 18 }}>{unit(mode, card.balance)}</T>
</T>
</View>
<T style={{ fontSize: 34 }}>⭐</T>
</View>
{available.length > 0 ? (
<View style={{ gap: 10 }}>
<T v="label" style={{ color: c.text3, textTransform: 'uppercase', fontSize: 13, letterSpacing: 0.5 }}>Disponibles</T>
{available.map(item)}
</View>
) : (
<View style={{ alignItems: 'center', gap: 6, paddingVertical: 12 }}>
<T style={{ fontSize: 32 }}>⏳</T>
<T v="muted" style={{ textAlign: 'center' }}>Aucune récompense disponible pour l'instant.</T>
</View>
)}
{later.length > 0 && (
<View style={{ gap: 10 }}>
<T v="label" style={{ color: c.text3, textTransform: 'uppercase', fontSize: 13, letterSpacing: 0.5 }}>Prochainement</T>
{later.map(item)}
</View>
)}
<View style={{ flex: 1 }} />
<Button label="Retour" variant="secondary" onPress={() => router.back()} />
</Screen>
);
}