MonEspacePro/pro/app/index.tsx

245 lines
11 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 { Image, Linking, Pressable, ScrollView, View } from 'react-native';
import { router, useFocusEffect } from 'expo-router';
import { SafeAreaView } from 'react-native-safe-area-context';
import Svg, { Path } from 'react-native-svg';
import { api, API_URL, Summary } from '../src/api';
import { useSession, useStore } from '../src/session';
import { BrandSymbol, Button, Card, T, unit } from '../src/ui';
import { font, useColors } from '../src/theme';
import { Icon, IconName } from '../src/icons';
function ScanIcon({ color }: { color: string }) {
return (
<Svg width={34} height={34} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth={2} strokeLinecap="round">
<Path d="M4 8V5a1 1 0 0 1 1-1h3M16 4h3a1 1 0 0 1 1 1v3M20 16v3a1 1 0 0 1-1 1h-3M8 20H5a1 1 0 0 1-1-1v-3M7 12h10" />
</Svg>
);
}
/** Adresse de l'espace web du Dirigeant (conception de la carte). */
const ESPACE_URL = 'https://monespacepro.eu/espace';
const greeting = () => (new Date().getHours() >= 18 ? 'Bonsoir' : 'Bonjour');
const today = () => new Date().toLocaleDateString('fr-FR', { weekday: 'long', day: 'numeric', month: 'long' });
export default function Home() {
const { me } = useSession();
// Magasin inscrit en ligne : tant que la carte n'est pas conçue, pas de scan possible.
return me?.store.cardDesign ? <Counter /> : <NoCardYet />;
}
/** En-tête commun : logo du magasin, date, accès au compte. */
function Header() {
const c = useColors();
const { me } = useSession();
const logo = me?.store.cardDesign?.logoUrl;
const first = me?.displayName.split(' ')[0] ?? '';
return (
<View style={{ flexDirection: 'row', alignItems: 'center', gap: 12 }}>
<View style={{ width: 48, height: 48, borderRadius: 14, backgroundColor: c.surface, alignItems: 'center', justifyContent: 'center', overflow: 'hidden', borderWidth: 1, borderColor: c.border }}>
{logo ? <Image source={{ uri: `${API_URL}${logo}` }} style={{ width: 44, height: 44 }} resizeMode="contain" /> : <BrandSymbol size={32} />}
</View>
<View style={{ flex: 1 }}>
<T v="h2" numberOfLines={1}>{me?.store.name}</T>
<T v="muted" style={{ fontSize: 14 }} numberOfLines={1}>{today()}</T>
</View>
<Pressable accessibilityRole="button" accessibilityLabel={`Mon compte (${me?.displayName})`} hitSlop={8} onPress={() => router.push('/account')}>
{({ pressed }) => (
<View style={{ width: 44, height: 44, borderRadius: 22, backgroundColor: c.primarySoft, alignItems: 'center', justifyContent: 'center', opacity: pressed ? 0.7 : 1 }}>
<T style={{ fontFamily: font.extrabold, fontSize: 18, color: c.primary }}>{first.charAt(0).toUpperCase()}</T>
</View>
)}
</Pressable>
</View>
);
}
/** Ligne d'action ou de réglage : pictogramme, titre, sous-titre, chevron. */
function Row({ icon, title, sub, onPress }: { icon: IconName; title: string; sub?: string; onPress: () => void }) {
const c = useColors();
return (
<Pressable accessibilityRole="button" onPress={onPress}>
{({ pressed }) => (
<Card style={{ padding: 14, flexDirection: 'row', alignItems: 'center', gap: 14, opacity: pressed ? 0.7 : 1 }}>
<View style={{ width: 44, height: 44, borderRadius: 13, backgroundColor: c.primarySoft, alignItems: 'center', justifyContent: 'center' }}>
<Icon name={icon} size={22} color={c.primary} />
</View>
<View style={{ flex: 1, gap: 2 }}>
<T style={{ fontFamily: font.bold, fontSize: 16 }}>{title}</T>
{!!sub && <T v="muted" style={{ fontSize: 13 }} numberOfLines={2}>{sub}</T>}
</View>
<Icon name="chevronRight" size={20} color={c.text3} />
</Card>
)}
</Pressable>
);
}
const sectionTitle = (c: ReturnType<typeof useColors>) => ({ color: c.text3, textTransform: 'uppercase' as const, fontSize: 13, letterSpacing: 0.5 });
function NoCardYet() {
const c = useColors();
const { me, reload } = useSession();
const isManager = me?.role === 'MANAGER';
return (
<SafeAreaView style={{ flex: 1, backgroundColor: c.bg }}>
<ScrollView contentContainerStyle={{ padding: 20, gap: 18, flexGrow: 1 }} bounces={false} overScrollMode="never">
<Header />
<View style={{ alignItems: 'center', gap: 10, paddingVertical: 16 }}>
<View style={{ width: 96, height: 96, borderRadius: 48, backgroundColor: c.primarySoft, alignItems: 'center', justifyContent: 'center' }}>
<Icon name="palette" size={44} color={c.primary} />
</View>
<T v="h1" style={{ textAlign: 'center' }}>{isManager ? 'Dernière étape' : 'Presque prêt'}</T>
<T v="muted" style={{ textAlign: 'center' }}>
{isManager
? 'Concevez votre carte de fidélité (logo, couleurs, récompenses) sur votre espace web. Le scan des cartes s’active ensuite.'
: 'La carte de fidélité du magasin n’est pas encore prête. Revenez un peu plus tard.'}
</T>
</View>
{isManager && <Button large label="Concevoir ma carte" onPress={() => Linking.openURL(ESPACE_URL)} />}
<Button label="C’est fait, actualiser" variant="secondary" onPress={reload} />
{isManager && (
<View style={{ gap: 10, marginTop: 8 }}>
<Row icon="users" title="Employés" sub="Créez les comptes de votre équipe" onPress={() => router.push('/team')} />
<Row icon="puzzle" title="Modules" sub="Préparez vos campagnes, relances et cadeaux" onPress={() => router.push('/loyalty')} />
</View>
)}
</ScrollView>
</SafeAreaView>
);
}
function Counter() {
const c = useColors();
const { me } = useSession();
const store = useStore();
const [summary, setSummary] = useState<Summary | null>(null);
const [tab, setTab] = useState<'scan' | 'config'>('scan');
const load = useCallback(async () => {
try {
setSummary(await api.get<Summary>('/api/staff/summary'));
} catch {
/* résumé indicatif : on garde l'ancien */
}
}, []);
useFocusEffect(
useCallback(() => {
load();
}, [load]),
);
const isManager = me?.role === 'MANAGER';
const u = unit(store.design.pointsMode);
return (
<SafeAreaView style={{ flex: 1, backgroundColor: c.bg }}>
<ScrollView
contentContainerStyle={{ padding: 20, gap: 20, flexGrow: 1 }}
// Pas de « tirer pour actualiser » : les chiffres se rechargent à chaque retour sur l'écran.
bounces={false}
overScrollMode="never"
>
<Header />
{isManager && (
<View accessibilityRole="tablist" style={{ flexDirection: 'row', backgroundColor: c.surface, borderRadius: 14, padding: 4, borderWidth: 1, borderColor: c.border }}>
{(
[
['scan', 'Scan'],
['config', 'Configuration'],
] as const
).map(([k, label]) => (
<Pressable
key={k}
accessibilityRole="tab"
accessibilityState={{ selected: tab === k }}
onPress={() => setTab(k)}
style={{ flex: 1, paddingVertical: 10, borderRadius: 10, alignItems: 'center', backgroundColor: tab === k ? c.primary : 'transparent' }}
>
<T style={{ fontFamily: font.bold, fontSize: 15, color: tab === k ? c.onPrimary : c.text }}>{label}</T>
</Pressable>
))}
</View>
)}
{tab === 'config' && isManager ? (
<ConfigTab />
) : (
<>
<T v="h1">
{greeting()} {me?.displayName.split(' ')[0]}
</T>
{/* Action principale */}
<Pressable
accessibilityRole="button"
accessibilityLabel="Scanner une carte"
onPress={() => router.push('/scan')}
style={({ pressed }) => ({
backgroundColor: pressed ? c.primaryPressed : c.primary,
borderRadius: 26,
paddingVertical: 40,
paddingHorizontal: 24,
alignItems: 'center',
gap: 14,
shadowColor: c.primary,
shadowOpacity: 0.35,
shadowRadius: 16,
shadowOffset: { width: 0, height: 8 },
elevation: 6,
})}
>
<View style={{ width: 76, height: 76, borderRadius: 38, backgroundColor: 'rgba(255,255,255,0.18)', alignItems: 'center', justifyContent: 'center' }}>
<ScanIcon color={c.onPrimary} />
</View>
<T style={{ color: c.onPrimary, fontFamily: font.extrabold, fontSize: 26 }}>Scanner une carte</T>
<T style={{ color: c.onPrimary, opacity: 0.85, fontSize: 15 }}>QR code du Wallet du client</T>
</Pressable>
{/* Chiffres du jour */}
<View style={{ gap: 10 }}>
<T v="label" style={sectionTitle(c)}>{isManager ? "Aujourd'hui au magasin" : "Mes opérations aujourd'hui"}</T>
<View style={{ flexDirection: 'row', gap: 10 }}>
{([
['receipt', summary?.operations ?? '—', 'opérations'],
['plusCircle', summary?.pointsGiven ?? '—', `${u} donnés`],
['gift', summary?.rewards ?? '—', 'récompenses'],
] as [IconName, string | number, string][]).map(([icon, n, l]) => (
<View key={String(l)} style={{ flex: 1, padding: 14, gap: 4, borderRadius: 18, backgroundColor: c.primarySoft }}>
<Icon name={icon} size={18} color={c.primary} />
<T style={{ fontFamily: font.extrabold, fontSize: 26, color: c.primary }}>{n}</T>
<T v="muted" style={{ fontSize: 12 }} numberOfLines={1}>{l}</T>
</View>
))}
</View>
</View>
{/* Autres actions */}
<View style={{ gap: 10 }}>
{isManager && <Row icon="megaphone" title="Envoyer une campagne" sub="Une notification à tous vos clients" onPress={() => router.push('/campaigns')} />}
{isManager && <Row icon="chart" title="Statistiques" sub="Passages, clients fidèles, exports" onPress={() => router.push('/stats')} />}
<Row icon="history" title="Historique des opérations" sub={isManager ? 'Toutes les opérations du magasin' : 'Vos opérations'} onPress={() => router.push('/history')} />
</View>
</>
)}
</ScrollView>
</SafeAreaView>
);
}
/** Onglet Configuration (Dirigeant) : tout ce qui se règle. */
function ConfigTab() {
const c = useColors();
return (
<View style={{ gap: 10 }}>
<T v="label" style={sectionTitle(c)}>Fidélisation</T>
<Row icon="puzzle" title="Modules" sub="Campagnes, relances, anniversaire, avis Google, parrainage" onPress={() => router.push('/loyalty')} />
<Row icon="palette" title="Carte de fidélité" sub="Logo, couleurs, règle de points, récompenses (espace web)" onPress={() => Linking.openURL(ESPACE_URL)} />
<T v="label" style={[sectionTitle(c), { marginTop: 10 }]}>Équipe et compte</T>
<Row icon="users" title="Employés" sub="Comptes, rôles Employé / Dirigeant, mots de passe" onPress={() => router.push('/team')} />
<Row icon="key" title="Mon compte" sub="Mot de passe, déconnexion" onPress={() => router.push('/account')} />
</View>
);
}