144 lines
6.9 KiB
TypeScript
144 lines
6.9 KiB
TypeScript
import { useEffect, useRef, useState } from 'react';
|
||
import { Alert, Animated, Pressable, View } from 'react-native';
|
||
import { router, useLocalSearchParams } from 'expo-router';
|
||
import { useKeepAwake } from 'expo-keep-awake';
|
||
import Svg, { Path } from 'react-native-svg';
|
||
import { useStore } from '../src/session';
|
||
import { Button, Screen, T, unit } from '../src/ui';
|
||
import { font, useColors } from '../src/theme';
|
||
import { Icon } from '../src/icons';
|
||
import { api } from '../src/api';
|
||
import { describe } from '../src/errors';
|
||
|
||
/** Confirmation (maquette 7) : nouveau solde, Wallet mis à jour. */
|
||
export default function Done() {
|
||
const c = useColors();
|
||
const store = useStore();
|
||
const p = useLocalSearchParams<{ kind: 'credit' | 'redeem'; firstName: string; points: string; balance: string; reward?: string; tx?: string; serial?: string }>();
|
||
const [cancelling, setCancelling] = useState(false);
|
||
useKeepAwake();
|
||
const scale = useRef(new Animated.Value(0.4)).current;
|
||
const mode = store.design.pointsMode;
|
||
const n = Number(p.points);
|
||
|
||
useEffect(() => {
|
||
Animated.spring(scale, { toValue: 1, friction: 5, useNativeDriver: true }).start();
|
||
}, [scale]);
|
||
|
||
const redeem = p.kind === 'redeem';
|
||
const balance = Number(p.balance);
|
||
// Crédit en vert, récompense en doré.
|
||
const [tint, tintBg] = redeem ? [c.warn, c.warnBg] : [c.ok, c.okBg];
|
||
const rewards = [...store.design.rewards].sort((a, b) => a.cost - b.cost);
|
||
const next = rewards.find((r) => r.cost > balance);
|
||
const ready = rewards.filter((r) => r.cost <= balance).length;
|
||
const fg = store.design.foregroundColor;
|
||
|
||
/** Erreur de saisie : annule l'opération et revient sur la fiche du client pour ressaisir. */
|
||
function confirmCancel() {
|
||
Alert.alert(
|
||
redeem ? 'Annuler cette récompense ?' : 'Annuler ce crédit ?',
|
||
redeem
|
||
? `${p.points} ${unit(mode, n)} seront rendus à ${p.firstName}.`
|
||
: `${n} ${unit(mode, n)} seront retirés de la carte de ${p.firstName}. Vous pourrez ressaisir aussitôt le bon montant.`,
|
||
[
|
||
{ text: 'Non', style: 'cancel' },
|
||
{
|
||
text: 'Annuler l’opération',
|
||
style: 'destructive',
|
||
onPress: async () => {
|
||
setCancelling(true);
|
||
try {
|
||
const r = await api.post<{ balance: number }>(`/api/staff/transactions/${p.tx}/cancel`);
|
||
Alert.alert('Opération annulée', `Nouveau solde de ${p.firstName} : ${r.balance} ${unit(mode, r.balance)}.`);
|
||
if (p.serial) router.replace({ pathname: '/client/[serial]', params: { serial: p.serial } });
|
||
else router.dismissTo('/');
|
||
} catch (e) {
|
||
Alert.alert('Annulation impossible', describe(e).detail);
|
||
} finally {
|
||
setCancelling(false);
|
||
}
|
||
},
|
||
},
|
||
],
|
||
);
|
||
}
|
||
|
||
return (
|
||
<Screen noHeader style={{ alignItems: 'stretch' }}>
|
||
<View style={{ flex: 1 }} />
|
||
<View style={{ alignItems: 'center', gap: 14 }}>
|
||
{/* Halo + pastille de validation */}
|
||
<View style={{ width: 140, height: 140, borderRadius: 70, backgroundColor: tintBg, alignItems: 'center', justifyContent: 'center' }}>
|
||
<Animated.View
|
||
style={{ width: 100, height: 100, borderRadius: 50, backgroundColor: tint, alignItems: 'center', justifyContent: 'center', transform: [{ scale }] }}
|
||
>
|
||
{redeem ? (
|
||
<Icon name="gift" size={46} color={fg} />
|
||
) : (
|
||
<Svg width={50} height={50} viewBox="0 0 24 24" fill="none" stroke="#fff" strokeWidth={2.6} strokeLinecap="round" strokeLinejoin="round">
|
||
<Path d="M5 12.5l4.5 4.5L19 7.5" />
|
||
</Svg>
|
||
)}
|
||
</Animated.View>
|
||
</View>
|
||
<Animated.View style={{ alignItems: 'center', gap: 6, opacity: scale.interpolate({ inputRange: [0.4, 1], outputRange: [0, 1] }) }}>
|
||
<T v="h1" accessibilityRole="alert" style={{ textAlign: 'center' }}>
|
||
{redeem ? `${p.reward}` : `+ ${n} ${unit(mode, n)}`}
|
||
</T>
|
||
<View style={{ paddingHorizontal: 12, paddingVertical: 4, borderRadius: 999, backgroundColor: tintBg }}>
|
||
<T style={{ color: tint, fontFamily: font.semibold, fontSize: 14 }}>
|
||
{redeem ? `Récompense remise · − ${n} ${unit(mode, n)}` : `Crédité pour ${p.firstName}`}
|
||
</T>
|
||
</View>
|
||
</Animated.View>
|
||
</View>
|
||
|
||
{/* Nouveau solde aux couleurs de la carte */}
|
||
<View style={{ backgroundColor: store.design.backgroundColor, borderRadius: 22, padding: 20, gap: 12 }}>
|
||
<View style={{ flexDirection: 'row', justifyContent: 'space-between', alignItems: 'flex-end' }}>
|
||
<View>
|
||
<T style={{ color: fg, opacity: 0.8, fontSize: 14 }}>Nouveau solde de {p.firstName}</T>
|
||
<T style={{ color: fg, fontFamily: font.extrabold, fontSize: 40, lineHeight: 46 }}>
|
||
{p.balance} <T style={{ color: fg, fontSize: 18 }}>{unit(mode, balance)}</T>
|
||
</T>
|
||
</View>
|
||
<View style={{ flexDirection: 'row', alignItems: 'center', gap: 4, marginBottom: 6, opacity: 0.8 }}><T style={{ color: fg, fontSize: 12 }}>Wallet mis à jour</T><Icon name="check" size={13} color={fg} strokeWidth={2.5} /></View>
|
||
</View>
|
||
{(next || ready > 0) && (
|
||
<View style={{ gap: 6 }}>
|
||
{next && (
|
||
<View style={{ height: 6, borderRadius: 3, backgroundColor: fg.length === 7 ? `${fg}40` : 'rgba(127,127,127,0.3)', overflow: 'hidden' }}>
|
||
<View style={{ width: `${Math.round(Math.min(balance / next.cost, 1) * 100)}%`, height: 6, borderRadius: 3, backgroundColor: c.accent }} />
|
||
</View>
|
||
)}
|
||
<T style={{ color: fg, fontSize: 14 }}>
|
||
{ready > 0
|
||
? `${ready} récompense${ready > 1 ? 's' : ''} disponible${ready > 1 ? 's' : ''}`
|
||
: `Encore ${next!.cost - balance} ${unit(mode, next!.cost - balance)} avant « ${next!.label} »`}
|
||
</T>
|
||
</View>
|
||
)}
|
||
</View>
|
||
|
||
<View style={{ flex: 1 }} />
|
||
<Button large label="Scanner une autre carte" onPress={() => router.replace('/scan')} />
|
||
<View style={{ flexDirection: 'row', gap: 10 }}>
|
||
{/* Retour à la fiche du client (autre récompense, crédit…) */}
|
||
{router.canGoBack() && (
|
||
<View style={{ flex: 1 }}>
|
||
<Button label="Fiche client" variant="secondary" onPress={() => router.back()} />
|
||
</View>
|
||
)}
|
||
<View style={{ flex: 1 }}>
|
||
<Button label="Accueil" variant="secondary" onPress={() => router.dismissTo('/')} />
|
||
</View>
|
||
</View>
|
||
{!!p.tx && (
|
||
<Pressable accessibilityRole="button" disabled={cancelling} onPress={confirmCancel} hitSlop={8} style={{ alignSelf: 'center', paddingVertical: 6, opacity: cancelling ? 0.5 : 1 }}>
|
||
<T style={{ color: c.err, fontFamily: font.semibold, fontSize: 15 }}>Erreur ? Annuler cette opération</T>
|
||
</Pressable>
|
||
)}
|
||
</Screen>
|
||
);
|
||
}
|