import { useNoticesStore } from "@/store/noticesStore"; import { NoticeCard } from "@/components/NoticeCard"; import { Button, ButtonText } from "@/components/ui/button"; import { Box } from "@/components/ui/box"; import { Text } from "@/components/ui/text"; import { VStack } from "@/components/ui/vstack"; import { ActivityIndicator, FlatList } from "react-native"; import { useEffect, useState, useRef } from "react"; import { createOrder, createPayment, getOrder } from "@/api/order"; import { Linking } from "react-native"; import { Ionicons } from "@expo/vector-icons"; import { useToast, Toast, ToastTitle } from "@/components/ui/toast"; import { AppState } from "react-native"; import { useAuthStore } from "@/store/authStore"; export default function UserNotices() { const { notices, fetchNotices, deleteNotice } = useNoticesStore(); const [isLoading, setIsLoading] = useState(true); const [isRedirecting, setIsRedirecting] = useState(false); const toast = useToast(); const appState = useRef(AppState.currentState); const [toastId, setToastId] = useState(0); const { user_id } = useAuthStore.getState(); const currentUserId = user_id; const [orderId, setOrderId] = useState(null); useEffect(() => { if (!isRedirecting) return; const subscription = AppState.addEventListener("change", (state) => { if (state === "active") { (async () => { const lastOrder = await getOrder(orderId); const lastPayments = lastOrder.payments; const paymentStatus = lastPayments.length > 0 ? lastPayments[lastPayments.length - 1].status : null; setIsRedirecting(false); if (paymentStatus === "INCORRECT") { showNewToast("Płatność została anulowana."); } else if (paymentStatus === "CORRECT") { showNewToast("Płatność została zrealizowana."); } else { showNewToast("Płatność jeszcze nie wpłynęła."); } })(); } appState.current = state; }); return () => subscription.remove(); }, [isRedirecting, toast, orderId]); useEffect(() => { const loadNotices = async () => { setIsLoading(true); try { await fetchNotices(); } catch (err) { console.error("Błąd podczas pobierania ogłoszeń:", err); } finally { setIsLoading(false); } }; loadNotices(); }, [fetchNotices]); const showNewToast = (title) => { const newId = Math.random(); setToastId(newId); toast.show({ id: newId, placement: "top", duration: 3000, render: ({ id }) => { const uniqueToastId = "toast-" + id; return ( {title} ); }, }); }; const handleOrder = async (noticeId, type) => { { try { const result = await createOrder(noticeId, type); if (result) { setOrderId(result); try { const paymentResult = await createPayment(result); if (paymentResult) { setIsRedirecting(true); await Linking.openURL(paymentResult); } else { console.log(`Nie udało się aktywować ogłoszenia 4 ${noticeId}.`); } } catch (err) { // console.log("Błąd podczas aktywacji ogłoszenia 3:", err); } } else { // console.log(`Nie udało się aktywować ogłoszenia 2 ${noticeId}.`); } } catch (err) { console.log("Błąd podczas aktywacji ogłoszenia 1:", err); } } }; const handleDeleteNotice = async (noticeId) => { try { await deleteNotice(noticeId); } catch (err) { console.error("Błąd podczas usuwania ogłoszenia:", err); } }; const userNotices = notices .filter((notice) => notice.clientId === currentUserId) .sort((a, b) => new Date(b.publishDate) - new Date(a.publishDate)); if (isLoading) { return ( ); } return ( {isRedirecting && ( Przekierowanie do płatności... )} {/* Moje ogłoszenia */} {userNotices.length > 0 ? ( ( {item.status === "ACTIVE" ? ( ) : ( )} )} keyExtractor={(item) => item.noticeId.toString()} /> ) : ( Nie masz żadnych ogłoszeń. )} ); }