88 lines
2.6 KiB
TypeScript
88 lines
2.6 KiB
TypeScript
import { Stack, useRouter } from 'expo-router';
|
|
import { useState } from 'react';
|
|
import { Pressable, Text, TextInput, View } from 'react-native';
|
|
|
|
import { getCurrentUserId, pb } from '@/services/pocketbase';
|
|
import { formatPocketBaseError } from '@/utils/pocketbaseErrors';
|
|
|
|
export default function ContactNouveauScreen() {
|
|
const router = useRouter();
|
|
const uid = getCurrentUserId();
|
|
const [nom, setNom] = useState('');
|
|
const [prenom, setPrenom] = useState('');
|
|
const [err, setErr] = useState<string | null>(null);
|
|
const [busy, setBusy] = useState(false);
|
|
|
|
const onSave = async () => {
|
|
if (!uid) {
|
|
setErr('Connectez-vous.');
|
|
return;
|
|
}
|
|
if (!nom.trim()) {
|
|
setErr('Le nom est obligatoire.');
|
|
return;
|
|
}
|
|
setErr(null);
|
|
setBusy(true);
|
|
try {
|
|
const c = await pb.collection('contacts').create({
|
|
user: uid,
|
|
nom: nom.trim(),
|
|
prenom: prenom.trim() || undefined,
|
|
categorie: 'autre',
|
|
});
|
|
router.replace(`/contact/${c.id}`);
|
|
} catch (e) {
|
|
setErr(formatPocketBaseError(e));
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
};
|
|
|
|
if (!uid) {
|
|
return (
|
|
<>
|
|
<Stack.Screen options={{ title: 'Nouveau contact', headerShown: true }} />
|
|
<View className="flex-1 items-center justify-center p-6">
|
|
<Text className="text-slate-600">Connexion requise.</Text>
|
|
</View>
|
|
</>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<Stack.Screen options={{ title: 'Nouveau contact', headerShown: true }} />
|
|
<View className="flex-1 bg-slate-50 p-4">
|
|
{err ? (
|
|
<View className="mb-3 rounded-xl border border-red-200 bg-red-50 px-3 py-2">
|
|
<Text className="text-sm text-red-900">{err}</Text>
|
|
</View>
|
|
) : null}
|
|
<Text className="mb-1 text-sm text-slate-600">Nom *</Text>
|
|
<TextInput
|
|
className="mb-3 rounded-xl border border-slate-200 bg-white px-3 py-3 text-base"
|
|
value={nom}
|
|
onChangeText={setNom}
|
|
placeholderTextColor="#94a3b8"
|
|
/>
|
|
<Text className="mb-1 text-sm text-slate-600">Prénom</Text>
|
|
<TextInput
|
|
className="mb-6 rounded-xl border border-slate-200 bg-white px-3 py-3 text-base"
|
|
value={prenom}
|
|
onChangeText={setPrenom}
|
|
placeholderTextColor="#94a3b8"
|
|
/>
|
|
<Pressable
|
|
className="rounded-xl py-3"
|
|
style={{ backgroundColor: busy ? '#94a3b8' : '#1D4ED8' }}
|
|
onPress={onSave}
|
|
disabled={busy}
|
|
>
|
|
<Text className="text-center font-semibold text-white">Enregistrer</Text>
|
|
</Pressable>
|
|
</View>
|
|
</>
|
|
);
|
|
}
|