158 lines
6.2 KiB
TypeScript
158 lines
6.2 KiB
TypeScript
import { useCallback, useState } from 'react';
|
|
import { Alert, Pressable, ScrollView, Share, TextInput, View } from 'react-native';
|
|
import { useFocusEffect } from 'expo-router';
|
|
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';
|
|
|
|
type Role = 'MANAGER' | 'EMPLOYEE';
|
|
interface Member {
|
|
id: string;
|
|
email: string;
|
|
displayName: string;
|
|
role: Role;
|
|
active: boolean;
|
|
provisionalPassword?: string;
|
|
}
|
|
|
|
const ROLE: Record<Role, [string, string]> = {
|
|
EMPLOYEE: ['Employé', 'Scanne les cartes et crédite les points'],
|
|
MANAGER: ['Dirigeant', 'Toute la configuration : modules, campagnes, employés, statistiques'],
|
|
};
|
|
|
|
/** Employés du magasin (Dirigeant) : création avec rôle, désactivation, nouveau mot de passe. */
|
|
export default function Team() {
|
|
const c = useColors();
|
|
const { me } = useSession();
|
|
const [list, setList] = useState<Member[] | null>(null);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [name, setName] = useState('');
|
|
const [email, setEmail] = useState('');
|
|
const [role, setRole] = useState<Role>('EMPLOYEE');
|
|
const [busy, setBusy] = useState(false);
|
|
const [created, setCreated] = useState<Member | null>(null);
|
|
|
|
const load = useCallback(async () => {
|
|
try {
|
|
setList(await api.get<Member[]>('/api/staff/team'));
|
|
} catch (e) {
|
|
setError(describe(e).detail);
|
|
}
|
|
}, []);
|
|
useFocusEffect(
|
|
useCallback(() => {
|
|
load();
|
|
}, [load]),
|
|
);
|
|
|
|
async function run(fn: () => Promise<unknown>) {
|
|
setError(null);
|
|
try {
|
|
await fn();
|
|
await load();
|
|
} catch (e) {
|
|
setError(describe(e).detail);
|
|
}
|
|
}
|
|
|
|
async function create() {
|
|
setBusy(true);
|
|
setCreated(null);
|
|
await run(async () => {
|
|
setCreated(await api.post<Member>('/api/staff/team', { displayName: name.trim(), email: email.trim(), role }));
|
|
setName('');
|
|
setEmail('');
|
|
setRole('EMPLOYEE');
|
|
});
|
|
setBusy(false);
|
|
}
|
|
|
|
const input = { borderWidth: 1, borderColor: c.border, borderRadius: 14, padding: 14, backgroundColor: c.surface, color: c.text, fontFamily: font.regular, fontSize: 16 } as const;
|
|
|
|
return (
|
|
<ScrollView contentContainerStyle={{ padding: 20, gap: 16 }} keyboardShouldPersistTaps="handled">
|
|
{error && <Banner tone="error">{error}</Banner>}
|
|
{created?.provisionalPassword && (
|
|
<Card style={{ padding: 16, gap: 8, borderColor: c.ok }}>
|
|
<T v="label">Compte créé pour {created.displayName}</T>
|
|
<T v="muted">Identifiant : {created.email}</T>
|
|
<T style={{ fontFamily: font.bold, fontSize: 22, letterSpacing: 1 }} selectable>{created.provisionalPassword}</T>
|
|
<T v="muted" style={{ fontSize: 13 }}>Mot de passe provisoire : il ne sera plus affiché ensuite.</T>
|
|
<Button
|
|
label="Partager les identifiants"
|
|
variant="secondary"
|
|
onPress={() => Share.share({ message: `MonEspacePro\nIdentifiant : ${created.email}\nMot de passe : ${created.provisionalPassword}` })}
|
|
/>
|
|
</Card>
|
|
)}
|
|
|
|
<View style={{ gap: 10 }}>
|
|
<T v="h2">Nouvel employé</T>
|
|
<TextInput value={name} onChangeText={setName} placeholder="Prénom Nom" placeholderTextColor={c.text3} accessibilityLabel="Prénom et nom" style={input} maxLength={80} />
|
|
<TextInput
|
|
value={email}
|
|
onChangeText={setEmail}
|
|
placeholder="prenom@exemple.fr"
|
|
placeholderTextColor={c.text3}
|
|
accessibilityLabel="E-mail"
|
|
autoCapitalize="none"
|
|
autoCorrect={false}
|
|
keyboardType="email-address"
|
|
style={input}
|
|
/>
|
|
{(['EMPLOYEE', 'MANAGER'] as const).map((r) => (
|
|
<Pressable key={r} accessibilityRole="radio" accessibilityState={{ checked: role === r }} onPress={() => setRole(r)}>
|
|
<Card style={{ padding: 14, gap: 2, borderWidth: 2, borderColor: role === r ? c.primary : c.border }}>
|
|
<T v="label">{ROLE[r][0]}</T>
|
|
<T v="muted" style={{ fontSize: 13 }}>{ROLE[r][1]}</T>
|
|
</Card>
|
|
</Pressable>
|
|
))}
|
|
<Button label="Créer le compte" onPress={create} loading={busy} disabled={name.trim().length < 2 || !email.includes('@')} />
|
|
</View>
|
|
|
|
<View style={{ gap: 10, marginTop: 8 }}>
|
|
<T v="h2">Comptes</T>
|
|
{list?.map((m) => {
|
|
const self = m.id === me?.id;
|
|
return (
|
|
<Card key={m.id} style={{ padding: 14, gap: 8, opacity: m.active ? 1 : 0.6 }}>
|
|
<View>
|
|
<T v="label">
|
|
{m.displayName}
|
|
{self ? ' (vous)' : ''}
|
|
</T>
|
|
<T v="muted" style={{ fontSize: 13 }}>
|
|
{m.email} · {ROLE[m.role][0]}
|
|
{m.active ? '' : ' · désactivé'}
|
|
</T>
|
|
</View>
|
|
{!self && (
|
|
<View style={{ flexDirection: 'row', gap: 8, flexWrap: 'wrap' }}>
|
|
<Button
|
|
label={m.role === 'MANAGER' ? 'Passer Employé' : 'Passer Dirigeant'}
|
|
variant="secondary"
|
|
onPress={() => run(() => api.patch(`/api/staff/team/${m.id}`, { role: m.role === 'MANAGER' ? 'EMPLOYEE' : 'MANAGER' }))}
|
|
/>
|
|
<Button label={m.active ? 'Désactiver' : 'Réactiver'} variant="secondary" onPress={() => run(() => api.patch(`/api/staff/team/${m.id}`, { active: !m.active }))} />
|
|
<Button
|
|
label="Nouveau mot de passe"
|
|
variant="secondary"
|
|
onPress={() =>
|
|
Alert.alert('Nouveau mot de passe ?', `${m.displayName} sera déconnecté(e) de ses appareils.`, [
|
|
{ text: 'Annuler', style: 'cancel' },
|
|
{ text: 'Générer', onPress: () => run(async () => setCreated(await api.post<Member>(`/api/staff/team/${m.id}/reset-password`))) },
|
|
])
|
|
}
|
|
/>
|
|
</View>
|
|
)}
|
|
</Card>
|
|
);
|
|
})}
|
|
</View>
|
|
</ScrollView>
|
|
);
|
|
}
|