192 lines
7.8 KiB
TypeScript
192 lines
7.8 KiB
TypeScript
import { useState } from 'react';
|
||
import { Alert, Pressable, ScrollView, Switch, TextInput, View } from 'react-native';
|
||
import { api, ApiError } from '../src/api';
|
||
import { describe } from '../src/errors';
|
||
import { useSession } from '../src/session';
|
||
import { smooth, Banner, Button, Card, T } from '../src/ui';
|
||
import { font, useColors } from '../src/theme';
|
||
import { useScanSound } from '../src/prefs';
|
||
import { confirmIdentity } from '../src/biometrics';
|
||
|
||
/** 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('');
|
||
smooth();
|
||
setDone(true);
|
||
} catch (e) {
|
||
setError(describe(e).detail);
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
}
|
||
|
||
const [requested, setRequested] = useState(false);
|
||
const [sound, setSound] = useScanSound();
|
||
|
||
/** 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: async () => {
|
||
if (await confirmIdentity('Supprimer votre compte')) 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');
|
||
smooth();
|
||
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>
|
||
|
||
{/* Comptoir */}
|
||
<Card style={{ flexDirection: 'row', alignItems: 'center', gap: 12 }}>
|
||
<View style={{ flex: 1, gap: 2 }}>
|
||
<T v="h2">Bip au scan</T>
|
||
<T v="muted" style={{ fontSize: 14 }}>Un son confirme chaque carte scannée (sauf si l’iPhone est en mode silencieux).</T>
|
||
</View>
|
||
<Switch value={sound} onValueChange={setSound} trackColor={{ true: c.primary }} accessibilityLabel="Bip au scan" />
|
||
</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>
|
||
);
|
||
}
|