MonEspacePro/pro/app/account.tsx

172 lines
7 KiB
TypeScript
Raw 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 { useState } from 'react';
import { Alert, Pressable, ScrollView, TextInput, View } from 'react-native';
import { api, ApiError } 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 [requested, setRequested] = useState(false);
/** Suppression de son compte. Le dernier dirigeant ne peut pas laisser le magasin sans dirigeant :
* il envoie alors une demande de suppression du compte et du magasin. */
function confirmDelete() {
Alert.alert(
'Supprimer votre compte ?',
'Suppression définitive : vous ne pourrez plus vous connecter, et votre nom et votre e-mail sont effacés. Les opérations déjà faites restent dans l’historique du magasin, de façon anonyme.',
[
{ text: 'Annuler', style: 'cancel' },
{ text: 'Supprimer', style: 'destructive', onPress: remove },
],
);
}
async function remove() {
setError(null);
try {
await api.del('/api/staff/me');
await logout();
} catch (e) {
if (e instanceof ApiError && e.status === 409) {
Alert.alert(
'Vous êtes le seul dirigeant',
'Votre compte est lié au magasin. Nous pouvons supprimer votre compte et le magasin (abonnement résilié, données des clients effacées). Envoyer la demande ?',
[
{ text: 'Annuler', style: 'cancel' },
{ text: 'Envoyer la demande', style: 'destructive', onPress: requestDeletion },
],
);
return;
}
setError(describe(e).detail);
}
}
async function requestDeletion() {
setError(null);
try {
await api.post('/api/staff/me/deletion-request');
setRequested(true);
} catch (e) {
setError(describe(e).detail);
}
}
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 },
])
}
/>
{/* Suppression du compte (exigée par l'App Store) */}
{requested ? (
<Banner tone="ok">Demande envoyée. Nous supprimons votre compte et le magasin sous 7 jours et vous le confirmons par e-mail.</Banner>
) : (
<Pressable accessibilityRole="button" hitSlop={8} onPress={confirmDelete} style={{ alignSelf: 'center', paddingVertical: 6 }}>
<T style={{ color: c.err, fontFamily: font.semibold, fontSize: 15 }}>Supprimer mon compte</T>
</Pressable>
)}
</ScrollView>
);
}