MonEspacePro/pro/app/loyalty.tsx

89 lines
3.9 KiB
TypeScript
Raw Permalink 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 { router, useFocusEffect } from 'expo-router';
import { api } from '../src/api';
import { describe } from '../src/errors';
import { smooth, Banner, Card, T } from '../src/ui';
import { font, useColors } from '../src/theme';
import { MODULE_ICON, ModuleInfo } from '../src/modules';
import { Icon } from '../src/icons';
/** 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((l) => { smooth(); setList(l); }).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' }}>
<Icon name={MODULE_ICON[m.id] ?? 'puzzle'} size={24} color={m.available && m.config.enabled ? c.primary : c.text3} />
</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 }} 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 && (
<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>
);
}