import { View, Text, Pressable, ScrollView, RefreshControl, Alert } from "react-native";
import { useRouter } from "expo-router";
import { useState, useCallback } from "react";
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 { 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 (
}
contentContainerStyle={{ padding: theme.spacing.lg, gap: theme.spacing.md, paddingBottom: 80 }}
>
{user.name || "Корисник"}
{user.email && (
{user.email}
)}
{user.phone && (
{user.phone}
)}
{isHandyman && (
<>
Мои огласи
{myAdsResult.status === "LoadingFirstPage" ? (
) : myAds.length === 0 ? (
📋
Сè уште немате огласи.
) : (
{myAds.map((ad) => (
router.push(`/ad/create?editId=${ad._id}` as any)}
style={{
flex: 1,
backgroundColor: theme.colors.surface,
borderRadius: theme.radius.md,
paddingVertical: 10,
alignItems: "center",
borderWidth: 1,
borderColor: theme.colors.borderLight,
}}
>
Уреди
handleDeleteAd(ad._id)}
style={{
flex: 1,
backgroundColor: theme.colors.errorLight,
borderRadius: theme.radius.md,
paddingVertical: 10,
alignItems: "center",
}}
>
Избриши
))}
)}
{myAdsResult.status === "CanLoadMore" && (
myAdsResult.loadMore(20)}
style={{
backgroundColor: theme.colors.surfaceAlt,
borderRadius: theme.radius.md,
paddingVertical: theme.spacing.md,
alignItems: "center",
borderWidth: 1,
borderColor: theme.colors.borderLight,
}}
>
Вчитај повеќе
)}
>
)}
{!isHandyman && (
<>
Мои побарувања
{myPostsResult.status === "LoadingFirstPage" ? (
) : myPosts.length === 0 ? (
📝
Сè уште немате побарувања.
) : (
{myPosts.map((post) => (
))}
)}
{myPostsResult.status === "CanLoadMore" && (
myPostsResult.loadMore(20)}
style={{
backgroundColor: theme.colors.surfaceAlt,
borderRadius: theme.radius.md,
paddingVertical: theme.spacing.md,
alignItems: "center",
borderWidth: 1,
borderColor: theme.colors.borderLight,
}}
>
Вчитај повеќе
)}
>
)}
Поставки
Оцени ја апликацијата
);
}