MonEspacePro/pro/app/login.tsx

155 lines
6.8 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 { useRef, useState } from 'react';
import { KeyboardAvoidingView, Linking, Platform, Pressable, ScrollView, TextInput, View } from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import { useSession } from '../src/session';
import { api } from '../src/api';
import { describe } from '../src/errors';
import { smooth, Banner, BrandSymbol, Button, Card, T } from '../src/ui';
import { font, useColors } from '../src/theme';
/** Connexion (Dirigeant et employés) : mêmes identifiants que l'espace web. */
export default function Login() {
const c = useColors();
const { login } = useSession();
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [show, setShow] = useState(false);
const [error, setError] = useState<string | null>(null);
const [busy, setBusy] = useState(false);
const pwdRef = useRef<TextInput>(null);
const [forgot, setForgot] = useState(false);
const [forgotDone, setForgotDone] = useState(false);
const [forgotBusy, setForgotBusy] = useState(false);
/** Envoie un lien pour choisir un nouveau mot de passe (même réponse que le compte existe ou non). */
async function sendReset() {
setForgotBusy(true);
setError(null);
try {
await api.postPublic('/api/public/mot-de-passe/oubli', { email: email.trim() });
smooth();
setForgotDone(true);
} catch (e) {
setError(describe(e).detail);
} finally {
setForgotBusy(false);
}
}
async function submit() {
setBusy(true);
setError(null);
try {
await login(email.trim(), password);
} catch (e) {
setError((e as Error).message);
} finally {
setBusy(false);
}
}
const input = {
height: 56,
borderWidth: 1,
borderColor: c.border,
borderRadius: 14,
paddingHorizontal: 16,
fontSize: 17,
fontFamily: font.regular,
color: c.text,
backgroundColor: c.bg,
};
return (
<View style={{ flex: 1, backgroundColor: c.bg }}>
{/* Bandeau de marque */}
<View style={{ backgroundColor: c.primary, borderBottomLeftRadius: 32, borderBottomRightRadius: 32, paddingBottom: 72 }}>
<SafeAreaView edges={['top']}>
<View style={{ alignItems: 'center', gap: 10, paddingTop: 28 }}>
<View style={{ width: 84, height: 84, borderRadius: 24, backgroundColor: c.surface, alignItems: 'center', justifyContent: 'center' }}>
<BrandSymbol size={56} />
</View>
<T style={{ fontFamily: font.extrabold, fontSize: 30, color: c.onPrimary }}>MonEspacePro</T>
<T style={{ color: c.onPrimary, opacity: 0.85, fontSize: 15 }}>La fidélité de vos clients, au comptoir</T>
</View>
</SafeAreaView>
</View>
<KeyboardAvoidingView behavior={Platform.OS === 'ios' ? 'padding' : undefined} style={{ flex: 1 }}>
<ScrollView contentContainerStyle={{ paddingHorizontal: 20, paddingBottom: 32, gap: 18 }} keyboardShouldPersistTaps="handled" bounces={false} overScrollMode="never" style={{ marginTop: -48 }}>
<Card style={{ padding: 20, gap: 14 }}>
<T v="h2">Connexion</T>
{error && <Banner tone="error">{error}</Banner>}
<View style={{ gap: 6 }}>
<T v="label" nativeID="l-email">E-mail</T>
<TextInput
accessibilityLabelledBy="l-email"
style={input}
value={email}
onChangeText={setEmail}
placeholder="vous@exemple.fr"
placeholderTextColor={c.text3}
autoCapitalize="none"
autoCorrect={false}
autoComplete="email"
keyboardType="email-address"
textContentType="username"
returnKeyType="next"
onSubmitEditing={() => pwdRef.current?.focus()}
/>
</View>
<View style={{ gap: 6 }}>
<View style={{ flexDirection: 'row', justifyContent: 'space-between' }}>
<T v="label" nativeID="l-pwd">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
ref={pwdRef}
accessibilityLabelledBy="l-pwd"
style={input}
value={password}
onChangeText={setPassword}
secureTextEntry={!show}
autoComplete="current-password"
textContentType="password"
returnKeyType="go"
onSubmitEditing={submit}
/>
</View>
<Button large label="Se connecter" onPress={submit} loading={busy} disabled={!email || !password} />
<Pressable accessibilityRole="button" hitSlop={8} onPress={() => { smooth(); setForgot(!forgot); setForgotDone(false); }} style={{ alignSelf: 'center', paddingVertical: 4 }}>
<T style={{ color: c.primary, fontFamily: font.semibold, fontSize: 15 }}>Mot de passe oublié ?</T>
</Pressable>
{forgot &&
(forgotDone ? (
<Banner tone="ok">Si un compte existe pour cette adresse, vous allez recevoir un e-mail avec un lien pour choisir un nouveau mot de passe (valable 7 jours). Pensez à vérifier les indésirables.</Banner>
) : (
<View style={{ gap: 10 }}>
<T v="muted" style={{ fontSize: 14 }}>Saisissez votre e-mail ci-dessus : nous vous envoyons un lien pour choisir un nouveau mot de passe.</T>
<Button label="Recevoir un lien" variant="secondary" onPress={sendReset} loading={forgotBusy} disabled={!/^\S+@\S+\.\S+$/.test(email.trim())} />
</View>
))}
</Card>
<T v="muted" style={{ textAlign: 'center', fontSize: 14 }}>
Employé ? Vos identifiants vous sont fournis par votre dirigeant.
</T>
{/* Sur iPhone, pas de lien vers l'abonnement (règles App Store 3.1.1 / 3.1.3) : simple information. */}
{Platform.OS === 'ios' ? (
<T style={{ textAlign: 'center', fontSize: 14, color: c.text3 }}>
Dirigeant ? Utilisez les identifiants reçus à l’inscription de votre commerce.
</T>
) : (
<Pressable accessibilityRole="link" onPress={() => Linking.openURL('https://monespacepro.eu/tarif/')}>
<T style={{ textAlign: 'center', fontSize: 14, color: c.text3 }}>
Pas encore client ? <T style={{ fontSize: 14, color: c.primary, fontFamily: font.semibold }}>Découvrir les formules</T>
</T>
</Pressable>
)}
</ScrollView>
</KeyboardAvoidingView>
</View>
);
}