MonEspacePro/pro/app/client/[serial]/credit.tsx
Tony AVERTY 5f354e8987 App : page de crédit retravaillée
Montant dans un bloc qui vire au rouge au-delà du plafond, points gagnés en
pastille, montants rapides (5 à 50 €), pavé numérique plus doux (appui long
sur ⌫ pour tout effacer), bouton « Créditer N points » ; mode tampons avec
tampon aux couleurs de la carte et bouton Annuler.

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

179 lines
7.2 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={{ alignItems: 'stretch' }}>
<View style={{ flex: 1 }} />
<View style={{ alignItems: 'center', gap: 16 }}>
{/* Tampon aux couleurs de la carte */}
<View style={{ width: 150, height: 150, borderRadius: 75, backgroundColor: c.primarySoft, alignItems: 'center', justifyContent: 'center' }}>
<View
style={{
width: 112,
height: 112,
borderRadius: 56,
backgroundColor: store.design.backgroundColor,
alignItems: 'center',
justifyContent: 'center',
borderWidth: 4,
borderStyle: 'dashed',
borderColor: store.design.foregroundColor,
}}
>
<T style={{ fontFamily: font.extrabold, fontSize: 40, color: store.design.foregroundColor }}>+1</T>
</View>
</View>
<T v="h1" style={{ textAlign: 'center' }}>Ajouter un tampon</T>
<T v="muted" style={{ textAlign: 'center' }}>Un tampon par passage, quel que soit le montant.</T>
</View>
<View style={{ flex: 1 }} />
<Button large label="Valider le tampon" onPress={submit} loading={busy} />
<Button label="Annuler" variant="secondary" onPress={() => router.back()} />
</Screen>
);
}
const QUICK = [5, 10, 20, 50];
return (
<Screen>
{/* Montant et points gagnés */}
<View style={{ alignItems: 'center', gap: 8, paddingVertical: 18, borderRadius: 22, backgroundColor: overLimit ? c.errBg : c.surface }}>
<T v="muted" style={{ fontSize: 14 }}>Montant de la commande</T>
<T
accessibilityLabel={`Montant ${euros(cents)}`}
adjustsFontSizeToFit
numberOfLines={1}
style={{ fontFamily: font.extrabold, fontSize: 56, lineHeight: 64, color: overLimit ? c.err : input ? c.text : c.text3 }}
>
{input || '0'} €
</T>
<View style={{ paddingHorizontal: 14, paddingVertical: 6, borderRadius: 999, backgroundColor: overLimit ? c.err : points > 0 ? c.primarySoft : c.surface2 }}>
<T style={{ fontFamily: font.bold, fontSize: 16, color: overLimit ? '#fff' : points > 0 ? c.primary : c.text3 }}>
{overLimit ? `Plafond : ${euros(store.maxAmountCents)}` : `+ ${points} ${unit('AMOUNT', points)}`}
</T>
</View>
<T v="muted" style={{ fontSize: 12 }}>
{store.design.pointsPerEuro} {unit('AMOUNT', store.design.pointsPerEuro)} par euro
</T>
</View>
{/* Montants rapides */}
<View style={{ flexDirection: 'row', gap: 8 }}>
{QUICK.map((q) => (
<Pressable
key={q}
accessibilityRole="button"
accessibilityLabel={`${q} euros`}
onPress={() => {
Haptics.selectionAsync();
setInput(String(q));
}}
style={({ pressed }) => ({ flex: 1, height: 44, borderRadius: 999, alignItems: 'center', justifyContent: 'center', backgroundColor: pressed ? c.primary : c.primarySoft })}
>
{({ pressed }) => <T style={{ fontFamily: font.bold, fontSize: 16, color: pressed ? c.onPrimary : c.primary }}>{q} €</T>}
</Pressable>
))}
</View>
{/* Pavé numérique (appui long sur ⌫ : tout effacer) */}
<View style={{ flex: 1, flexDirection: 'row', flexWrap: 'wrap', gap: 10, alignContent: 'flex-end' }}>
{KEYS.map((k) => {
const del = k === '⌫';
return (
<Pressable
key={k}
accessibilityRole="button"
accessibilityLabel={del ? 'Effacer' : k === ',' ? 'Virgule' : k}
accessibilityHint={del ? 'Appui long pour tout effacer' : undefined}
onPress={() => press(k)}
onLongPress={del ? () => setInput('') : undefined}
style={({ pressed }) => ({
width: '31%',
flexGrow: 1,
height: 62,
borderRadius: 18,
backgroundColor: pressed ? c.primarySoft : del ? c.surface2 : c.surface,
alignItems: 'center',
justifyContent: 'center',
shadowColor: '#000',
shadowOpacity: del ? 0 : 0.05,
shadowRadius: 6,
shadowOffset: { width: 0, height: 2 },
elevation: del ? 0 : 1,
})}
>
<T style={{ fontFamily: font.semibold, fontSize: 26, color: del ? c.text3 : c.text }}>{k}</T>
</Pressable>
);
})}
</View>
<Button
large
label={points > 0 && !overLimit ? `Créditer ${points} ${unit('AMOUNT', points)}` : 'Créditer'}
onPress={submit}
loading={busy}
disabled={points <= 0 || overLimit}
/>
</Screen>
);
}