MonEspacePro/pro/app/account.tsx
Tony AVERTY 9c5695758d App : page Mon compte retravaillée
Profil (initiale, nom, rôle, magasin), changement de mot de passe avec
affichage facultatif et indicateur de solidité, bouton de déconnexion.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 14:32:43 +02:00

117 lines
5 KiB
TypeScript

import { useState } from 'react';
import { Alert, Pressable, ScrollView, TextInput, View } from 'react-native';
import { api } from '../src/api';
import { describe } from '../src/errors';
import { useSession } from '../src/session';
import { Banner, Button, Card, T } from '../src/ui';
import { font, useColors } from '../src/theme';
/** Mon compte : profil, changement du mot de passe, déconnexion (tous les rôles). */
export default function Account() {
const c = useColors();
const { me, logout } = useSession();
const [current, setCurrent] = useState('');
const [next, setNext] = useState('');
const [show, setShow] = useState(false);
const [error, setError] = useState<string | null>(null);
const [done, setDone] = useState(false);
const [busy, setBusy] = useState(false);
async function save() {
setBusy(true);
setError(null);
setDone(false);
try {
await api.post('/api/staff/password', { current, next });
setCurrent('');
setNext('');
setDone(true);
} catch (e) {
setError(describe(e).detail);
} finally {
setBusy(false);
}
}
const manager = me?.role === 'MANAGER';
// Indication simple : longueur, puis variété (chiffres, majuscules, symboles).
const variety = [/\d/, /[A-Z]/, /[^A-Za-z0-9]/].filter((r) => r.test(next)).length;
const strength = next.length < 10 ? 0 : variety >= 2 || next.length >= 14 ? 2 : 1;
const strengthLabel = ['Trop court (10 caractères minimum)', 'Correct', 'Solide'][strength];
const strengthColor = [c.err, c.warn, c.ok][strength];
const input = { borderWidth: 1, borderColor: c.border, borderRadius: 14, padding: 14, backgroundColor: c.bg, color: c.text, fontFamily: font.regular, fontSize: 16 } as const;
return (
<ScrollView contentContainerStyle={{ padding: 20, gap: 18, flexGrow: 1 }} keyboardShouldPersistTaps="handled" bounces={false} overScrollMode="never">
{/* Profil */}
<View style={{ alignItems: 'center', gap: 8, paddingVertical: 8 }}>
<View style={{ width: 84, height: 84, borderRadius: 42, backgroundColor: c.primarySoft, alignItems: 'center', justifyContent: 'center' }}>
<T style={{ fontFamily: font.extrabold, fontSize: 34, color: c.primary }}>{me?.displayName.charAt(0).toUpperCase()}</T>
</View>
<T v="h1" style={{ textAlign: 'center' }}>{me?.displayName}</T>
<View style={{ flexDirection: 'row', gap: 6, alignItems: 'center' }}>
<View style={{ paddingHorizontal: 10, paddingVertical: 3, borderRadius: 999, backgroundColor: manager ? c.primarySoft : c.surface2 }}>
<T style={{ fontSize: 13, fontFamily: font.semibold, color: manager ? c.primary : c.text3 }}>{manager ? 'Dirigeant' : 'Employé'}</T>
</View>
<T v="muted" style={{ fontSize: 14 }}>{me?.store.name}</T>
</View>
</View>
{error && <Banner tone="error">{error}</Banner>}
{done && <Banner tone="ok">Mot de passe modifié.</Banner>}
{/* Mot de passe */}
<Card style={{ gap: 12 }}>
<View style={{ flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center' }}>
<T v="h2">Mot de passe</T>
<Pressable accessibilityRole="button" hitSlop={10} onPress={() => setShow(!show)}>
<T style={{ color: c.primary, fontFamily: font.semibold, fontSize: 14 }}>{show ? 'Masquer' : 'Afficher'}</T>
</Pressable>
</View>
<TextInput
value={current}
onChangeText={setCurrent}
secureTextEntry={!show}
autoComplete="current-password"
placeholder="Mot de passe actuel"
placeholderTextColor={c.text3}
accessibilityLabel="Mot de passe actuel"
style={input}
/>
<TextInput
value={next}
onChangeText={setNext}
secureTextEntry={!show}
autoComplete="new-password"
placeholder="Nouveau mot de passe"
placeholderTextColor={c.text3}
accessibilityLabel="Nouveau mot de passe"
style={input}
/>
{next.length > 0 && (
<View style={{ gap: 6 }}>
<View style={{ flexDirection: 'row', gap: 4 }}>
{[0, 1, 2].map((i) => (
<View key={i} style={{ flex: 1, height: 5, borderRadius: 3, backgroundColor: i <= strength ? strengthColor : c.surface2 }} />
))}
</View>
<T style={{ fontSize: 13, color: strengthColor, fontFamily: font.semibold }}>{strengthLabel}</T>
</View>
)}
<Button label="Changer le mot de passe" onPress={save} loading={busy} disabled={!current || next.length < 10} />
</Card>
<View style={{ flex: 1 }} />
<Button
label="Se déconnecter"
variant="secondary"
onPress={() =>
Alert.alert('Se déconnecter ?', undefined, [
{ text: 'Annuler', style: 'cancel' },
{ text: 'Se déconnecter', style: 'destructive', onPress: logout },
])
}
/>
</ScrollView>
);
}