import { View, Text, Pressable, ScrollView, RefreshControl, Alert } from "react-native"; import { useRouter } from "expo-router"; import { useState, useCallback } from "react"; import { Ionicons } from "@expo/vector-icons"; import { useQuery, useMutation, usePaginatedQuery } from "convex/react"; import { api } from "../../../convex/_generated/api"; import { useAuth } from "../../_layout"; import { useTheme } from "../../../components/theme"; import { Button } from "../../../components/ui/button"; import { Badge } from "../../../components/ui/badge"; import { Loading } from "../../../components/ui/loading"; import { Card } from "../../../components/ui/card"; import { Avatar } from "../../../components/ui/avatar"; import { ThemeToggleRow } from "../../../components/ui/theme-toggle"; import { AdCard } from "../../../components/ad-card"; import { PostCard } from "../../../components/post-card"; export default function ProfileScreen() { const router = useRouter(); const theme = useTheme(); const { token, userId, isAuthenticated, logout } = useAuth(); const [refreshing, setRefreshing] = useState(false); const user = useQuery(api.users.getCurrentUser, isAuthenticated ? { token: token! } : "skip"); const myAdsResult = usePaginatedQuery( api.ads.getByHandymanPaginated, isAuthenticated && userId ? { handymanId: userId as any } : "skip", { initialNumItems: 20 } ); const myPostsResult = usePaginatedQuery( api.posts.getByCustomerPaginated, isAuthenticated && userId ? { customerId: userId as any } : "skip", { initialNumItems: 20 } ); const deleteAd = useMutation(api.ads.remove); const logoutMutation = useMutation(api.auth.logout); const onRefresh = useCallback(() => { setRefreshing(true); setTimeout(() => setRefreshing(false), 800); }, []); async function handleLogout() { if (token) { try { await logoutMutation({ token }); } catch {} } await logout(); router.replace("/(auth)/login"); } async function handleDeleteAd(adId: string) { Alert.alert( "Избриши оглас", "Дали сте сигурни дека сакате да го избришете овој оглас?", [ { text: "Откажи", style: "cancel" }, { text: "Избриши", style: "destructive", onPress: async () => { try { await deleteAd({ token: token!, adId: adId as any }); } catch (e: any) { Alert.alert("Грешка", e.message || "Неуспешно бришење"); } }, }, ] ); } if (!isAuthenticated) { return ( Профил Потребна најава Најавете се за да го видите вашиот профил ); } if (!user) { return ( ); } const isHandyman = user.role === "handyman"; const myAds = myAdsResult.results; const myPosts = myPostsResult.results; return ( {user.name || "Корисник"} {(user.email || user.phone) && ( {user.email || user.phone} )} {}} style={{ width: 44, height: 44, borderRadius: 22, backgroundColor: theme.colors.surfaceAlt, alignItems: "center", justifyContent: "center", }} > } contentContainerStyle={{ padding: theme.spacing.md, gap: theme.spacing.md, paddingBottom: 100 }} > {isHandyman && ( <> Мои огласи ({myAds.length}) router.push("/ad/create" as any)} style={{ flexDirection: "row", alignItems: "center", gap: 4 }} > Нов оглас {myAdsResult.status === "LoadingFirstPage" ? ( ) : myAds.length === 0 ? ( Сè уште немате огласи ) : ( {myAds.map((ad) => ( ))} )} {myAdsResult.status === "CanLoadMore" && ( myAdsResult.loadMore(20)} style={{ backgroundColor: theme.colors.surface, borderRadius: theme.radius.lg, paddingVertical: 12, alignItems: "center", borderWidth: 1, borderColor: theme.colors.border, ...theme.shadows.xs, }} > Вчитај повеќе )} )} {!isHandyman && ( <> Мои побарувања ({myPosts.length}) router.push("/post/create" as any)} style={{ flexDirection: "row", alignItems: "center", gap: 4 }} > Ново {myPostsResult.status === "LoadingFirstPage" ? ( ) : myPosts.length === 0 ? ( Сè уште немате побарувања ) : ( {myPosts.map((post) => ( ))} )} {myPostsResult.status === "CanLoadMore" && ( myPostsResult.loadMore(20)} style={{ backgroundColor: theme.colors.surface, borderRadius: theme.radius.lg, paddingVertical: 12, alignItems: "center", borderWidth: 1, borderColor: theme.colors.border, ...theme.shadows.xs, }} > Вчитај повеќе )} )} ); }