App : page Modules retravaillée

- Liste : pictogramme par module, interrupteur d'activation directe,
  compteur de modules actifs, section « Non inclus dans votre formule »
- Réglages : en-tête avec interrupteur, chiffres du module, champs regroupés
  (Réglages / Messages), bouton Enregistrer fixé en bas dès qu'une
  modification est faite

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Tony AVERTY 2026-10-07 14:16:38 +02:00
parent 53a598080e
commit 98fd599f75
3 changed files with 138 additions and 43 deletions

View file

@ -1,43 +1,88 @@
import { useCallback, useState } from 'react';
import { Pressable, ScrollView, View } from 'react-native';
import { Pressable, ScrollView, Switch, View } from 'react-native';
import { router, useFocusEffect } from 'expo-router';
import { api } from '../src/api';
import { describe } from '../src/errors';
import { Banner, Card, T } from '../src/ui';
import { font, useColors } from '../src/theme';
import type { ModuleInfo } from '../src/modules';
import { MODULE_ICON, ModuleInfo } from '../src/modules';
/** Modules complémentaires du magasin (dirigeants) : état et accès aux réglages. */
/** Modules complémentaires du magasin (dirigeants) : activation directe et accès aux réglages. */
export default function Modules() {
const c = useColors();
const [list, setList] = useState<ModuleInfo[] | null>(null);
const [error, setError] = useState<string | null>(null);
const [switching, setSwitching] = useState<string | null>(null);
useFocusEffect(
useCallback(() => {
api.get<ModuleInfo[]>('/api/staff/modules').then(setList).catch((e) => setError(describe(e).detail));
}, []),
);
/** Interrupteur de la liste : active ou désactive sans ouvrir les réglages. */
async function toggle(m: ModuleInfo, enabled: boolean) {
setSwitching(m.id);
setError(null);
try {
const next = await api.put<ModuleInfo>(`/api/staff/modules/${m.id}`, { enabled });
setList((l) => l?.map((x) => (x.id === m.id ? next : x)) ?? null);
} catch (e) {
setError(describe(e).detail);
} finally {
setSwitching(null);
}
}
const available = list?.filter((m) => m.available) ?? [];
const locked = list?.filter((m) => !m.available) ?? [];
const active = available.filter((m) => m.config.enabled).length;
const row = (m: ModuleInfo) => (
<Pressable key={m.id} accessibilityRole="button" accessibilityLabel={`Réglages ${m.name}`} onPress={() => router.push({ pathname: '/module/[id]', params: { id: m.id } })}>
{({ pressed }) => (
<Card style={{ padding: 14, flexDirection: 'row', alignItems: 'center', gap: 14, opacity: pressed ? 0.7 : m.available ? 1 : 0.6 }}>
<View style={{ width: 48, height: 48, borderRadius: 14, backgroundColor: m.available && m.config.enabled ? c.primarySoft : c.surface2, alignItems: 'center', justifyContent: 'center' }}>
<T style={{ fontSize: 24 }}>{MODULE_ICON[m.id] ?? '🧩'}</T>
</View>
<View style={{ flex: 1, gap: 2 }}>
<T style={{ fontFamily: font.bold, fontSize: 17 }} numberOfLines={1}>{m.name}</T>
<T v="muted" style={{ fontSize: 13 }} numberOfLines={2}>{m.tagline}</T>
</View>
{m.available ? (
<Switch
value={m.config.enabled}
disabled={switching === m.id}
onValueChange={(v) => toggle(m, v)}
trackColor={{ true: c.primary }}
accessibilityLabel={`${m.name} : ${m.config.enabled ? 'activé' : 'désactivé'}`}
/>
) : (
<View style={{ paddingHorizontal: 10, paddingVertical: 4, borderRadius: 999, backgroundColor: c.surface2 }}>
<T style={{ fontSize: 12, fontFamily: font.semibold, color: c.text3 }}>Non inclus</T>
</View>
)}
</Card>
)}
</Pressable>
);
return (
<ScrollView contentContainerStyle={{ padding: 20, gap: 12 }}>
<T v="muted">Faites revenir vos clients avec des messages sur leur carte Wallet. Chaque module se règle entièrement : textes, délais, heures d'envoi.</T>
<ScrollView contentContainerStyle={{ padding: 20, gap: 12 }} bounces={false} overScrollMode="never">
<T v="muted">Activez un module d’un geste ; touchez-le pour régler ses textes, délais et récompenses.</T>
{error && <Banner tone="error">{error}</Banner>}
{list?.map((m) => {
const state = !m.available ? ['Non inclus', c.text3] : m.config.enabled ? ['Activé', c.ok] : ['Désactivé', c.text3];
return (
<Pressable key={m.id} accessibilityRole="button" onPress={() => router.push({ pathname: '/module/[id]', params: { id: m.id } })}>
{({ pressed }) => (
<Card style={{ padding: 16, gap: 4, opacity: pressed ? 0.7 : 1 }}>
<View style={{ flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center' }}>
<T style={{ fontFamily: font.bold, fontSize: 17 }}>{m.name}</T>
<T style={{ fontFamily: font.semibold, fontSize: 13, color: state[1] }}>{state[0]}</T>
</View>
<T v="muted" style={{ fontSize: 14 }}>{m.tagline}</T>
</Card>
)}
</Pressable>
);
})}
{list && (
<T v="label" style={{ color: c.text3, marginTop: 4 }}>
{active} module{active > 1 ? 's' : ''} actif{active > 1 ? 's' : ''} sur {available.length}
</T>
)}
{available.map(row)}
{locked.length > 0 && (
<>
<T v="label" style={{ color: c.text3, marginTop: 12 }}>Non inclus dans votre formule</T>
{locked.map(row)}
</>
)}
</ScrollView>
);
}

View file

@ -3,7 +3,7 @@ import { Pressable, ScrollView, Switch, TextInput, View } from 'react-native';
import { router, Stack, useFocusEffect, useLocalSearchParams } from 'expo-router';
import { api } from '../../src/api';
import { describe } from '../../src/errors';
import { FieldDef, ModuleInfo, Value } from '../../src/modules';
import { FieldDef, isMessageField, MODULE_ICON, ModuleInfo, Value } from '../../src/modules';
import { Banner, Button, Card, T } from '../../src/ui';
import { font, useColors } from '../../src/theme';
@ -86,6 +86,7 @@ function Field({ f, value, onChange }: { f: FieldDef; value: Value; onChange: (v
/** Réglages complets d'un module (dirigeants). */
export default function ModuleScreen() {
const c = useColors();
const { id } = useLocalSearchParams<{ id: string }>();
const [m, setM] = useState<ModuleInfo | null>(null);
const [values, setValues] = useState<Record<string, Value>>({});
@ -120,34 +121,69 @@ export default function ModuleScreen() {
if (!m) return <View style={{ padding: 20 }}>{error ? <Banner tone="error">{error}</Banner> : <T v="muted">Chargement…</T>}</View>;
const dirty = JSON.stringify(values) !== JSON.stringify(m.config);
const settings = m.fields.filter((f) => !isMessageField(f));
const messages = m.fields.filter(isMessageField);
const enabled = !!values.enabled;
const section = (title: string, fields: FieldDef[]) =>
fields.length > 0 && (
<View style={{ gap: 14 }}>
<T v="label" style={{ color: c.text3, textTransform: 'uppercase', fontSize: 13, letterSpacing: 0.5 }}>{title}</T>
{fields.map((f) => (
<Field key={f.key} f={f} value={values[f.key]} onChange={(v) => setValues({ ...values, [f.key]: v })} />
))}
</View>
);
return (
<ScrollView contentContainerStyle={{ padding: 20, gap: 16 }} keyboardShouldPersistTaps="handled">
<View style={{ flex: 1, backgroundColor: c.bg }}>
<Stack.Screen options={{ title: m.name }} />
{error && <Banner tone="error">{error}</Banner>}
{saved && <Banner tone="ok">Réglages enregistrés.</Banner>}
{!m.available && <Banner tone="warn">Ce module n'est pas inclus dans votre formule. Contactez MonEspacePro pour l'ajouter.</Banner>}
<T v="muted">{m.description}</T>
<ScrollView contentContainerStyle={{ padding: 20, gap: 22, paddingBottom: 120 }} keyboardShouldPersistTaps="handled" bounces={false} overScrollMode="never">
{/* En-tête : pictogramme, description, interrupteur principal */}
<Card style={{ gap: 14 }}>
<View style={{ flexDirection: 'row', alignItems: 'center', gap: 14 }}>
<View style={{ width: 52, height: 52, borderRadius: 16, backgroundColor: enabled ? c.primarySoft : c.surface2, alignItems: 'center', justifyContent: 'center' }}>
<T style={{ fontSize: 26 }}>{MODULE_ICON[m.id] ?? '🧩'}</T>
</View>
<View style={{ flex: 1 }}>
<T style={{ fontFamily: font.bold, fontSize: 18 }}>{m.name}</T>
<T style={{ fontFamily: font.semibold, fontSize: 13, color: !m.available ? c.text3 : enabled ? c.ok : c.text3 }}>
{!m.available ? 'Non inclus dans votre formule' : enabled ? 'Activé' : 'Désactivé'}
</T>
</View>
{m.available && (
<Switch value={enabled} onValueChange={(v) => setValues({ ...values, enabled: v })} trackColor={{ true: c.primary }} accessibilityLabel="Module activé" />
)}
</View>
<T v="muted" style={{ fontSize: 14 }}>{m.description}</T>
{m.available && m.config.enabled && m.id === 'campaigns' && <Button label="Envoyer une campagne" onPress={() => router.push('/campaigns')} />}
{m.available && m.config.enabled && m.id === 'stats' && <Button label="Voir les statistiques" onPress={() => router.push('/stats')} />}
</Card>
{m.available && m.config.enabled && m.id === 'campaigns' && <Button label="Envoyer une campagne" onPress={() => router.push('/campaigns')} />}
{m.available && m.config.enabled && m.id === 'stats' && <Button label="Voir les statistiques" onPress={() => router.push('/stats')} />}
{error && <Banner tone="error">{error}</Banner>}
{saved && !dirty && <Banner tone="ok">Réglages enregistrés.</Banner>}
{!m.available && <Banner tone="warn">Ce module n'est pas inclus dans votre formule. Contactez MonEspacePro pour l'ajouter.</Banner>}
<Field f={{ key: 'enabled', type: 'boolean', label: 'Module activé', default: false }} value={values.enabled} onChange={(v) => setValues({ ...values, enabled: v })} />
{m.fields.map((f) => (
<Field key={f.key} f={f} value={values[f.key]} onChange={(v) => setValues({ ...values, [f.key]: v })} />
))}
{m.stats && (
<View style={{ flexDirection: 'row', flexWrap: 'wrap', gap: 10 }}>
{Object.entries(m.stats).map(([k, v]) => (
<View key={k} style={{ width: '47%', flexGrow: 1, padding: 14, gap: 2, borderRadius: 14, backgroundColor: c.primarySoft }}>
<T style={{ fontFamily: font.extrabold, fontSize: 22, color: c.primary }}>{v ?? '—'}</T>
<T v="muted" style={{ fontSize: 13 }}>{k}</T>
</View>
))}
</View>
)}
<Button label="Enregistrer" onPress={save} loading={busy} disabled={!dirty} />
{section('Réglages', settings)}
{section('Messages', messages)}
</ScrollView>
{m.stats && (
<View style={{ flexDirection: 'row', flexWrap: 'wrap', gap: 12 }}>
{Object.entries(m.stats).map(([k, v]) => (
<Card key={k} style={{ width: '47%', flexGrow: 1, padding: 14, gap: 2 }}>
<T style={{ fontFamily: font.extrabold, fontSize: 22 }}>{v ?? '—'}</T>
<T v="muted" style={{ fontSize: 13 }}>{k}</T>
</Card>
))}
{/* Enregistrer toujours à portée de pouce, visible dès qu'il y a une modification */}
{dirty && (
<View style={{ position: 'absolute', left: 0, right: 0, bottom: 0, padding: 16, paddingBottom: 28, backgroundColor: c.bg, borderTopWidth: 1, borderTopColor: c.border }}>
<Button label="Enregistrer les modifications" onPress={save} loading={busy} />
</View>
)}
</ScrollView>
</View>
);
}

View file

@ -18,3 +18,17 @@ export interface ModuleInfo {
config: Record<string, Value> & { enabled: boolean };
stats?: Record<string, number | string | null> | null;
}
/** Pictogramme et teinte de chaque module (liste et en-tête des réglages). */
export const MODULE_ICON: Record<string, string> = {
campaigns: '📣',
reminders: '⏰',
birthday: '🎂',
reviews: '⭐',
referral: '🤝',
stats: '📊',
whiteLabel: '🏷️',
};
/** Champs regroupés par section sur l'écran de réglages. */
export const isMessageField = (f: FieldDef) => f.type === 'text' || f.type === 'textarea';