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(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(`/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 ; if (stamp) { return ( {/* Tampon aux couleurs de la carte */} +1 Ajouter un tampon Un tampon par passage, quel que soit le montant.