chore: add notification telegram
This commit is contained in:
@@ -8,11 +8,12 @@ import {
|
||||
Alert,
|
||||
KeyboardAvoidingView,
|
||||
Platform,
|
||||
Linking,
|
||||
} from "react-native";
|
||||
import { useFocusEffect } from "@react-navigation/native";
|
||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||
import { Ionicons } from "@expo/vector-icons";
|
||||
import { getMyProfile, updateMyProfile } from "../../api/api";
|
||||
import { getMyProfile, updateMyProfile, getTelegramStatus, generateTelegramLinkToken, unlinkTelegram } from "../../api/api";
|
||||
import TextInput from "../../components/ui/TextInput";
|
||||
import Button from "../../components/ui/Button";
|
||||
import { useTheme } from "../../context/ThemeContext";
|
||||
@@ -39,14 +40,22 @@ export default function ProfileScreen() {
|
||||
const [loadingProfile, setLoadingProfile] = useState(true);
|
||||
const [savingContact, setSavingContact] = useState(false);
|
||||
|
||||
// Telegram
|
||||
const [telegramLinked, setTelegramLinked] = useState(false);
|
||||
const [telegramEnabled, setTelegramEnabled] = useState(false);
|
||||
const [telegramLoading, setTelegramLoading] = useState(false);
|
||||
|
||||
const loadData = useCallback(async () => {
|
||||
setLoadingProfile(true);
|
||||
const [savedAddress, savedPhone, savedSignal, profileRes] = await Promise.all([
|
||||
const [savedAddress, savedPhone, savedSignal, profileRes, tgStatus] = await Promise.all([
|
||||
AsyncStorage.getItem(STORAGE_ADDRESS),
|
||||
AsyncStorage.getItem(STORAGE_PHONE),
|
||||
AsyncStorage.getItem(STORAGE_SIGNAL),
|
||||
getMyProfile(),
|
||||
getTelegramStatus(),
|
||||
]);
|
||||
setTelegramLinked(tgStatus.linked);
|
||||
setTelegramEnabled(tgStatus.enabled);
|
||||
|
||||
if (savedAddress !== null) setDefaultAddress(savedAddress);
|
||||
if (savedPhone !== null) setDefaultPhone(savedPhone);
|
||||
@@ -79,6 +88,42 @@ export default function ProfileScreen() {
|
||||
Alert.alert("Enregistré", "Informations par défaut sauvegardées");
|
||||
};
|
||||
|
||||
const handleLinkTelegram = async () => {
|
||||
setTelegramLoading(true);
|
||||
const res = await generateTelegramLinkToken();
|
||||
setTelegramLoading(false);
|
||||
if (res.error || !res.link_url) {
|
||||
Alert.alert("Erreur", res.error || "Service Telegram non disponible");
|
||||
return;
|
||||
}
|
||||
Alert.alert(
|
||||
"Lier Telegram",
|
||||
"Appuyez sur OK pour ouvrir le bot Telegram et envoyer le message de liaison.",
|
||||
[
|
||||
{ text: "Annuler", style: "cancel" },
|
||||
{ text: "Ouvrir Telegram", onPress: () => Linking.openURL(res.link_url!) },
|
||||
],
|
||||
);
|
||||
};
|
||||
|
||||
const handleUnlinkTelegram = async () => {
|
||||
Alert.alert(
|
||||
"Délier Telegram",
|
||||
"Vous ne recevrez plus de notifications Telegram. Continuer ?",
|
||||
[
|
||||
{ text: "Annuler", style: "cancel" },
|
||||
{
|
||||
text: "Délier",
|
||||
style: "destructive",
|
||||
onPress: async () => {
|
||||
await unlinkTelegram();
|
||||
setTelegramLinked(false);
|
||||
},
|
||||
},
|
||||
],
|
||||
);
|
||||
};
|
||||
|
||||
const saveContact = async () => {
|
||||
setSavingContact(true);
|
||||
const res = await updateMyProfile({ nom: nom.trim(), prenom: prenom.trim(), telephone: telephone.trim() });
|
||||
@@ -280,6 +325,45 @@ export default function ProfileScreen() {
|
||||
</TouchableOpacity>
|
||||
</View>
|
||||
|
||||
{/* Carte Telegram */}
|
||||
{telegramEnabled && (
|
||||
<View style={styles.card}>
|
||||
<View style={styles.cardTitle}>
|
||||
<Ionicons name="paper-plane-outline" size={18} color="#2AABEE" />
|
||||
<Text style={styles.cardTitleText}>Notifications Telegram</Text>
|
||||
</View>
|
||||
<Text style={styles.hint}>
|
||||
Recevez vos notifications sur Telegram même quand l'application est fermée.
|
||||
</Text>
|
||||
{telegramLinked ? (
|
||||
<View>
|
||||
<View style={{ flexDirection: "row", alignItems: "center", gap: spacing.s, marginBottom: spacing.m }}>
|
||||
<Ionicons name="checkmark-circle" size={16} color="#10b981" />
|
||||
<Text style={{ color: "#10b981", fontSize: fontSize.sm }}>Compte Telegram lié</Text>
|
||||
</View>
|
||||
<TouchableOpacity
|
||||
style={[styles.saveBtn, { backgroundColor: "#ef444422", borderWidth: 1, borderColor: "#ef444466" }]}
|
||||
onPress={handleUnlinkTelegram}
|
||||
>
|
||||
<Ionicons name="unlink-outline" size={16} color="#ef4444" />
|
||||
<Text style={[styles.saveBtnText, { color: "#ef4444" }]}>Délier Telegram</Text>
|
||||
</TouchableOpacity>
|
||||
</View>
|
||||
) : (
|
||||
<TouchableOpacity
|
||||
style={[styles.saveBtn, { backgroundColor: "#2AABEE" }]}
|
||||
onPress={handleLinkTelegram}
|
||||
disabled={telegramLoading}
|
||||
>
|
||||
<Ionicons name="paper-plane-outline" size={16} color="#fff" />
|
||||
<Text style={styles.saveBtnText}>
|
||||
{telegramLoading ? "Génération du lien..." : "Lier mon compte Telegram"}
|
||||
</Text>
|
||||
</TouchableOpacity>
|
||||
)}
|
||||
</View>
|
||||
)}
|
||||
|
||||
</ScrollView>
|
||||
</KeyboardAvoidingView>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user