import { NativeStackNavigationProp } from '@react-navigation/native-stack'; import { useMutation } from '@tanstack/react-query'; import packetsApi from 'api/packets'; import React, { useEffect, useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { ActivityIndicator, Animated, Dimensions, Linking, StyleSheet, Text, TouchableOpacity, TouchableWithoutFeedback, View, } from 'react-native'; import Check from 'svg/Check'; import Click from 'svg/Click'; import Payme from 'svg/Payme'; import { RootStackParamList } from 'types/types'; import { PaymentStyle } from '../../payment/ui/style'; const { height } = Dimensions.get('window'); interface ModalCardViewProps { isVisible: boolean; setIsVisible: React.Dispatch>; selectedId: 'click' | 'payme' | null; packId: number; setSelectedId: React.Dispatch>; } type NavigationProp = NativeStackNavigationProp< RootStackParamList, 'PaymentMethod' >; const ModalCard = ({ isVisible, setIsVisible, packId, selectedId, setSelectedId, }: ModalCardViewProps) => { const slideAnim = useRef(new Animated.Value(height)).current; const [load, setLoad] = React.useState(false); const [pay, setPay] = useState(''); const { t } = useTranslation(); const openLink = async (url: string) => { try { const supported = await Linking.canOpenURL(url); if (supported) { await Linking.openURL(url); } else { // Agar app ocholmasa, default brauzerda ochishga urinadi await Linking.openURL(url); } } catch (err) { console.error('Link xatolik:', err); // Xato bo‘lsa ham brauzer orqali ochishga urinish try { await Linking.openURL(url); } catch (err2) { console.error('Brauzer orqali ham ochilmadi:', err2); } } }; const { mutate, isPending } = useMutation({ mutationFn: ({ id, payType }: { id: number; payType: string }) => packetsApi.payPackets(id, { payType }), onSuccess: res => { setIsVisible(false); const url = res.data.paymentUrl; openLink(url); }, onError: err => { console.dir(err); }, }); const openModal = () => { Animated.timing(slideAnim, { toValue: 0, duration: 100, useNativeDriver: true, }).start(); }; const closeModal = () => { Animated.timing(slideAnim, { toValue: height, duration: 200, useNativeDriver: true, }).start(() => { setIsVisible(false); }); }; const handlePayment = () => { mutate({ id: packId, payType: pay }); }; useEffect(() => { if (isVisible) openModal(); }, [isVisible]); if (!isVisible) return null; return ( {/* CLICK */} setSelectedId('click')} > {selectedId === 'click' && ( )} {/* PAYME */} { setPay('PAYME'), setSelectedId('payme'); }} > {selectedId === 'payme' && ( )} {load || isPending ? ( ) : ( {t("To'lash")} )} ); }; const styles = StyleSheet.create({ overlay: { position: 'absolute', top: 0, left: 0, right: 0, bottom: 0, zIndex: 1000, justifyContent: 'flex-end', }, backdrop: { ...StyleSheet.absoluteFillObject, backgroundColor: 'rgba(0,0,0,0.4)', }, sheet: { width: '100%', backgroundColor: '#F3FAFF', borderTopLeftRadius: 20, borderTopRightRadius: 20, padding: 20, paddingBottom: 30, }, sheetContent: { gap: 10, }, paymentOption: { flexDirection: 'row', justifyContent: 'space-between', borderRadius: 10, paddingLeft: 20, paddingRight: 20, alignItems: 'center', }, }); export default ModalCard;