- 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>
114 lines
4.3 KiB
TypeScript
114 lines
4.3 KiB
TypeScript
import { useState } from 'react';
|
|
import { Pressable, View } from 'react-native';
|
|
import { router, useLocalSearchParams } from 'expo-router';
|
|
import * as Haptics from 'expo-haptics';
|
|
import { api } from '../../../src/api';
|
|
import { useStore } from '../../../src/session';
|
|
import { ErrorScreen } from '../../../src/errors';
|
|
import { Button, Screen, T, euros, unit } from '../../../src/ui';
|
|
import { font, useColors } from '../../../src/theme';
|
|
|
|
const KEYS = ['1', '2', '3', '4', '5', '6', '7', '8', '9', ',', '0', '⌫'];
|
|
|
|
type CreditResult = { points: number; balance: number; firstName: string };
|
|
|
|
/** Saisie du montant (maquette 5) ou confirmation d'un tampon (5b). */
|
|
export default function Credit() {
|
|
const c = useColors();
|
|
const store = useStore();
|
|
const { serial } = useLocalSearchParams<{ serial: string }>();
|
|
const stamp = store.design.pointsMode === 'STAMP';
|
|
const [input, setInput] = useState('');
|
|
const [busy, setBusy] = useState(false);
|
|
const [error, setError] = useState<unknown>(null);
|
|
|
|
// « 23,5 » → 2350 centimes ; deux décimales maximum.
|
|
const cents = Math.round(parseFloat(input.replace(',', '.') || '0') * 100);
|
|
const points = stamp ? 1 : Math.floor((cents * store.design.pointsPerEuro) / 100);
|
|
const overLimit = cents > store.maxAmountCents;
|
|
|
|
function press(k: string) {
|
|
Haptics.selectionAsync();
|
|
if (k === '⌫') return setInput((v) => v.slice(0, -1));
|
|
setInput((v) => {
|
|
if (k === ',' && (v.includes(',') || v === '')) return v === '' ? '0,' : v;
|
|
if (v.includes(',') && v.split(',')[1].length >= 2) return v;
|
|
if (v === '0' && k !== ',') return k;
|
|
if (v.replace(',', '').length >= 7) return v;
|
|
return v + k;
|
|
});
|
|
}
|
|
|
|
async function submit() {
|
|
setBusy(true);
|
|
try {
|
|
const r = await api.post<CreditResult>(`/api/staff/cards/${encodeURIComponent(serial)}/credit`, stamp ? {} : { amountCents: cents });
|
|
Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success);
|
|
router.replace({
|
|
pathname: '/done',
|
|
params: { kind: 'credit', firstName: r.firstName, points: String(r.points), balance: String(r.balance) },
|
|
});
|
|
} catch (e) {
|
|
Haptics.notificationAsync(Haptics.NotificationFeedbackType.Error);
|
|
setError(e);
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
}
|
|
|
|
if (error) return <ErrorScreen error={error} />;
|
|
|
|
if (stamp) {
|
|
return (
|
|
<Screen style={{ justifyContent: 'center' }}>
|
|
<T v="h1" style={{ textAlign: 'center' }}>+1 tampon</T>
|
|
<T v="muted" style={{ textAlign: 'center' }}>Un tampon par passage, quel que soit le montant.</T>
|
|
<View style={{ flex: 1 }} />
|
|
<Button large label="Valider le tampon" onPress={submit} loading={busy} />
|
|
</Screen>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<Screen>
|
|
<View style={{ alignItems: 'center', gap: 6, paddingTop: 8 }}>
|
|
<T v="muted">Montant de la commande</T>
|
|
<T
|
|
accessibilityLabel={`Montant ${euros(cents)}`}
|
|
style={{ fontFamily: font.extrabold, fontSize: 52, lineHeight: 60, color: overLimit ? c.err : c.text }}
|
|
>
|
|
{input || '0'} €
|
|
</T>
|
|
<T style={{ fontFamily: font.semibold, fontSize: 18, color: overLimit ? c.err : c.primary }}>
|
|
{overLimit ? `Plafond : ${euros(store.maxAmountCents)}` : `+ ${points} ${unit('AMOUNT', points)}`}
|
|
</T>
|
|
</View>
|
|
|
|
<View style={{ flex: 1, flexDirection: 'row', flexWrap: 'wrap', gap: 10, alignContent: 'flex-end' }}>
|
|
{KEYS.map((k) => (
|
|
<Pressable
|
|
key={k}
|
|
accessibilityRole="button"
|
|
accessibilityLabel={k === '⌫' ? 'Effacer' : k === ',' ? 'Virgule' : k}
|
|
onPress={() => press(k)}
|
|
style={({ pressed }) => ({
|
|
width: '31%',
|
|
flexGrow: 1,
|
|
height: 64,
|
|
borderRadius: 14,
|
|
backgroundColor: pressed ? c.surface2 : c.surface,
|
|
borderWidth: 1,
|
|
borderColor: c.border,
|
|
alignItems: 'center',
|
|
justifyContent: 'center',
|
|
})}
|
|
>
|
|
<T style={{ fontFamily: font.semibold, fontSize: 26 }}>{k}</T>
|
|
</Pressable>
|
|
))}
|
|
</View>
|
|
|
|
<Button large label="Valider" onPress={submit} loading={busy} disabled={points <= 0 || overLimit} />
|
|
</Screen>
|
|
);
|
|
}
|