import { useCallback, useState } from 'react'; 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, isMessageField, MODULE_ICON, ModuleInfo, Value } from '../../src/modules'; import { Banner, Button, Card, T } from '../../src/ui'; import { font, useColors } from '../../src/theme'; function Field({ f, value, onChange }: { f: FieldDef; value: Value; onChange: (v: Value) => void }) { const c = useColors(); const input = { borderWidth: 1, borderColor: c.border, borderRadius: 14, padding: 14, backgroundColor: c.surface, color: c.text, fontFamily: font.regular, fontSize: 16 } as const; const help = [f.help, 'variables' in f && f.variables?.length ? `Variables : ${f.variables.join(' ')}` : null].filter(Boolean).join(' '); if (f.type === 'boolean') { return ( {f.label} {!!f.help && {f.help}} ); } if (f.type === 'int' || f.type === 'hour') { const n = Number(value); const set = (v: number) => onChange(Math.max(f.min, Math.min(f.max, v))); const btn = (d: number) => ( 0 ? 'plus' : 'moins'}`} onPress={() => set(n + d)} style={{ width: 44, height: 44, borderRadius: 22, borderWidth: 1, borderColor: c.border, backgroundColor: c.surface, alignItems: 'center', justifyContent: 'center' }} > {d > 0 ? '+' : '−'} ); return ( {f.label} {btn(-1)} set(Math.floor(Number(t.replace(/\D/g, '')) || 0))} keyboardType="number-pad" accessibilityLabel={f.label} style={{ ...input, width: 76, textAlign: 'center', paddingVertical: 10 }} /> {btn(1)} {f.type === 'hour' ? 'h' : f.unit} {!!help && {help}} ); } const t = f as Extract; const text = String(value ?? ''); return ( {t.label} {help} {text.length}/{t.maxLength} {t.type !== 'url' && text !== t.default && ( onChange(t.default)}> Remettre le texte par défaut )} ); } /** Réglages complets d'un module (dirigeants). */ export default function ModuleScreen() { const c = useColors(); const { id } = useLocalSearchParams<{ id: string }>(); const [m, setM] = useState(null); const [values, setValues] = useState>({}); const [error, setError] = useState(null); const [saved, setSaved] = useState(false); const [busy, setBusy] = useState(false); const apply = (next: ModuleInfo) => { setM(next); setValues(next.config); }; useFocusEffect( useCallback(() => { api.get(`/api/staff/modules/${id}`).then(apply).catch((e) => setError(describe(e).detail)); }, [id]), ); async function save() { setBusy(true); setError(null); setSaved(false); try { apply(await api.put(`/api/staff/modules/${id}`, values)); setSaved(true); } catch (e) { setError(describe(e).detail); } finally { setBusy(false); } } if (!m) return {error ? {error} : Chargement…}; 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 && ( {title} {fields.map((f) => ( setValues({ ...values, [f.key]: v })} /> ))} ); return ( {/* En-tête : pictogramme, description, interrupteur principal */} {MODULE_ICON[m.id] ?? '🧩'} {m.name} {!m.available ? 'Non inclus dans votre formule' : enabled ? 'Activé' : 'Désactivé'} {m.available && ( setValues({ ...values, enabled: v })} trackColor={{ true: c.primary }} accessibilityLabel="Module activé" /> )} {m.description} {m.available && m.config.enabled && m.id === 'campaigns' &&