197 lines
8.9 KiB
TypeScript
197 lines
8.9 KiB
TypeScript
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 { smooth, Banner, Button, Card, T } from '../../src/ui';
|
||
import { font, useColors } from '../../src/theme';
|
||
import { Icon } from '../../src/icons';
|
||
|
||
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 (
|
||
<Card style={{ flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', padding: 16, gap: 12 }}>
|
||
<View style={{ flex: 1 }}>
|
||
<T v="label">{f.label}</T>
|
||
{!!f.help && <T v="muted" style={{ fontSize: 13 }}>{f.help}</T>}
|
||
</View>
|
||
<Switch value={!!value} onValueChange={onChange} accessibilityLabel={f.label} />
|
||
</Card>
|
||
);
|
||
}
|
||
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) => (
|
||
<Pressable
|
||
accessibilityRole="button"
|
||
accessibilityLabel={`${f.label} : ${d > 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' }}
|
||
>
|
||
<T style={{ fontFamily: font.bold, fontSize: 20 }}>{d > 0 ? '+' : '−'}</T>
|
||
</Pressable>
|
||
);
|
||
return (
|
||
<View style={{ gap: 6 }}>
|
||
<T v="label">{f.label}</T>
|
||
<View style={{ flexDirection: 'row', alignItems: 'center', gap: 10 }}>
|
||
{btn(-1)}
|
||
<TextInput
|
||
value={String(n)}
|
||
onChangeText={(t) => 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)}
|
||
<T v="muted" style={{ flex: 1 }}>{f.type === 'hour' ? 'h' : f.unit}</T>
|
||
</View>
|
||
{!!help && <T v="muted" style={{ fontSize: 13 }}>{help}</T>}
|
||
</View>
|
||
);
|
||
}
|
||
const t = f as Extract<FieldDef, { maxLength: number }>;
|
||
const text = String(value ?? '');
|
||
return (
|
||
<View style={{ gap: 6 }}>
|
||
<T v="label">{t.label}</T>
|
||
<TextInput
|
||
value={text}
|
||
onChangeText={onChange}
|
||
multiline={t.type === 'textarea'}
|
||
maxLength={t.maxLength}
|
||
autoCapitalize={t.type === 'url' ? 'none' : 'sentences'}
|
||
autoCorrect={t.type !== 'url'}
|
||
keyboardType={t.type === 'url' ? 'url' : 'default'}
|
||
accessibilityLabel={t.label}
|
||
style={{ ...input, ...(t.type === 'textarea' && { minHeight: 90, textAlignVertical: 'top' as const }) }}
|
||
/>
|
||
<View style={{ flexDirection: 'row', justifyContent: 'space-between', gap: 8 }}>
|
||
<T v="muted" style={{ fontSize: 13, flex: 1 }}>{help}</T>
|
||
<T v="muted" style={{ fontSize: 12 }}>{text.length}/{t.maxLength}</T>
|
||
</View>
|
||
{t.type !== 'url' && text !== t.default && (
|
||
<Pressable accessibilityRole="button" onPress={() => onChange(t.default)}>
|
||
<T style={{ color: c.primary, fontSize: 14 }}>Remettre le texte par défaut</T>
|
||
</Pressable>
|
||
)}
|
||
</View>
|
||
);
|
||
}
|
||
|
||
/** 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>>({});
|
||
const [error, setError] = useState<string | null>(null);
|
||
const [saved, setSaved] = useState(false);
|
||
const [busy, setBusy] = useState(false);
|
||
|
||
const apply = (next: ModuleInfo) => {
|
||
smooth();
|
||
setM(next);
|
||
setValues(next.config);
|
||
};
|
||
useFocusEffect(
|
||
useCallback(() => {
|
||
api.get<ModuleInfo>(`/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<ModuleInfo>(`/api/staff/modules/${id}`, values));
|
||
smooth();
|
||
setSaved(true);
|
||
} catch (e) {
|
||
setError(describe(e).detail);
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
}
|
||
|
||
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);
|
||
// La barre « Enregistrer » glisse seulement quand elle apparaît ou disparaît (pas à chaque lettre tapée).
|
||
const change = (next: Record<string, Value>) => {
|
||
if ((JSON.stringify(next) !== JSON.stringify(m.config)) !== dirty) smooth();
|
||
setValues(next);
|
||
};
|
||
|
||
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) => change({ ...values, [f.key]: v })} />
|
||
))}
|
||
</View>
|
||
);
|
||
|
||
return (
|
||
<View style={{ flex: 1, backgroundColor: c.bg }}>
|
||
<Stack.Screen options={{ title: m.name }} />
|
||
<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' }}>
|
||
<Icon name={MODULE_ICON[m.id] ?? 'puzzle'} size={26} color={enabled ? c.primary : c.text3} />
|
||
</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) => change({ ...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>
|
||
|
||
{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>}
|
||
|
||
{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>
|
||
)}
|
||
|
||
{section('Réglages', settings)}
|
||
{section('Messages', messages)}
|
||
</ScrollView>
|
||
|
||
{/* 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>
|
||
)}
|
||
</View>
|
||
);
|
||
}
|