- API /staff : profil, scan d'une carte, crédit (montant ou tampon), récompenses, historique (gérant : tout le magasin, employé : le sien), résumé du jour - Anti-triche côté serveur : carte du bon magasin, plafond de montant, délai minimum entre deux crédits (atomique, scans simultanés compris), tentatives refusées journalisées avec l'employé - App Expo SDK 54 / Expo Router : connexion, accueil, scanner QR avec lampe, fiche client, pavé numérique, récompenses, confirmation, erreurs, historique ; icônes générées depuis le logo Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
93 lines
3.6 KiB
TypeScript
93 lines
3.6 KiB
TypeScript
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 { 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);
|
|
|
|
return (
|
|
<Screen scroll>
|
|
<T v="muted">
|
|
Solde de {card.firstName} : <T style={{ fontFamily: font.bold }}>{card.balance} {unit(mode, card.balance)}</T>
|
|
</T>
|
|
<View style={{ gap: 12 }}>
|
|
{sorted.map((r) => {
|
|
const ok = r.cost <= card.balance;
|
|
return (
|
|
<Pressable
|
|
key={r.id}
|
|
accessibilityRole="button"
|
|
accessibilityState={{ disabled: !ok || !!busy }}
|
|
disabled={!ok || !!busy}
|
|
onPress={() => 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,
|
|
})}
|
|
>
|
|
<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 }}>
|
|
{ok ? `${r.cost} ${unit(mode, r.cost)}` : `Encore ${r.cost - card.balance} ${unit(mode, r.cost - card.balance)}`}
|
|
</T>
|
|
</View>
|
|
{busy === r.id ? <ActivityIndicator /> : ok && <T style={{ color: c.primary, fontFamily: font.bold }}>Utiliser</T>}
|
|
</Pressable>
|
|
);
|
|
})}
|
|
</View>
|
|
</Screen>
|
|
);
|
|
}
|