MonEspacePro/pro/app/referral.tsx
Tony AVERTY b75e396bde Parrainage (formule Pro)
- Code personnel par carte, lien d'inscription ?parrain=CODE (page de la carte, dos Apple, liens Google)
- Bandeau « X vous invite » à l'inscription ; bouton Partager (partage natif ou copie)
- Récompense au premier passage du filleul : parrain et filleul (réglables), une fois,
  au plus 10 récompenses par parrain sur 30 jours
- Notifications via la rubrique Actualité (message personnel)
- Admin : carte Parrainage ; app Pro : écran dans Fidélisation
- Migration manuelle 2026-10-07-parrainage.sql

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

113 lines
4.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useCallback, useState } from 'react';
import { Pressable, ScrollView, Switch, View } from 'react-native';
import { useFocusEffect } from 'expo-router';
import { api } from '../src/api';
import { describe } from '../src/errors';
import { useStore } from '../src/session';
import { Banner, Button, Card, T, unit } from '../src/ui';
import { font, useColors } from '../src/theme';
interface Referral {
allowed: boolean;
settings: { enabled: boolean; points: number; friendPoints: number };
maxPoints: number;
maxRewardsPer30Days: number;
stats: { referred: number; rewarded: number };
}
/** Parrainage (gérants, formule Pro). */
export default function ReferralScreen() {
const c = useColors();
const store = useStore();
const [data, setData] = useState<Referral | null>(null);
const [enabled, setEnabled] = useState(false);
const [points, setPoints] = useState(5);
const [friendPoints, setFriendPoints] = useState(0);
const [error, setError] = useState<string | null>(null);
const [saved, setSaved] = useState(false);
const [busy, setBusy] = useState(false);
const apply = (d: Referral) => {
setData(d);
setEnabled(d.settings.enabled);
setPoints(d.settings.points);
setFriendPoints(d.settings.friendPoints);
};
useFocusEffect(
useCallback(() => {
api.get<Referral>('/api/staff/referral').then(apply).catch((e) => setError(describe(e).detail));
}, []),
);
async function save() {
setBusy(true);
setError(null);
setSaved(false);
try {
apply(await api.put<Referral>('/api/staff/referral', { enabled, points, friendPoints }));
setSaved(true);
} catch (e) {
setError(describe(e).detail);
} finally {
setBusy(false);
}
}
if (!data) return <View style={{ padding: 20 }}>{error ? <Banner tone="error">{error}</Banner> : <T v="muted">Chargement…</T>}</View>;
const btn = (label: string, d: number, value: number, set: (n: number) => void) => (
<Pressable
accessibilityRole="button"
accessibilityLabel={`${label} : ${d > 0 ? 'plus' : 'moins'}`}
onPress={() => set(Math.max(0, Math.min(data.maxPoints, value + d)))}
style={{ width: 48, height: 48, borderRadius: 24, borderWidth: 1, borderColor: c.border, backgroundColor: c.surface, alignItems: 'center', justifyContent: 'center' }}
>
<T style={{ fontFamily: font.bold, fontSize: 22 }}>{d > 0 ? '+' : '−'}</T>
</Pressable>
);
const stepper = (label: string, value: number, set: (n: number) => void) => (
<View style={{ gap: 8 }}>
<T v="label">{label}</T>
<View style={{ flexDirection: 'row', alignItems: 'center', gap: 16 }}>
{btn(label, -1, value, set)}
<T style={{ fontFamily: font.extrabold, fontSize: 24, minWidth: 40, textAlign: 'center' }}>{value}</T>
{btn(label, 1, value, set)}
<T v="muted" style={{ flex: 1 }}>{value === 0 ? 'rien' : unit(store.design.pointsMode, value)}</T>
</View>
</View>
);
return (
<ScrollView contentContainerStyle={{ padding: 20, gap: 16 }}>
{error && <Banner tone="error">{error}</Banner>}
{saved && <Banner tone="ok">Réglages enregistrés.</Banner>}
{!data.allowed && <Banner tone="warn">Le parrainage est inclus dans la formule Pro. Contactez MonEspacePro pour changer de formule.</Banner>}
<T v="muted">
Chaque client a un lien à partager. Quand un ami s'inscrit avec ce lien et fait son premier passage, le parrain est récompensé. Au plus {data.maxRewardsPer30Days} récompenses par parrain sur 30 jours.
</T>
<Card style={{ flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', padding: 16 }}>
<T v="label">Parrainage</T>
<Switch value={enabled} onValueChange={setEnabled} disabled={!data.allowed && !enabled} accessibilityLabel="Parrainage" />
</Card>
{stepper('Offerts au parrain', points, setPoints)}
{stepper('Offerts au filleul', friendPoints, setFriendPoints)}
<Button label="Enregistrer" onPress={save} loading={busy} />
<View style={{ flexDirection: 'row', gap: 12 }}>
{[
[data.stats.referred, 'filleuls inscrits'],
[data.stats.rewarded, 'venus en magasin'],
].map(([n, l]) => (
<Card key={String(l)} style={{ flex: 1, padding: 14, gap: 2 }}>
<T style={{ fontFamily: font.extrabold, fontSize: 22 }}>{n}</T>
<T v="muted" style={{ fontSize: 13 }}>{l}</T>
</Card>
))}
</View>
</ScrollView>
);
}