import { View, Text, Pressable, ScrollView, Alert } from "react-native";
import { useRouter } from "expo-router";
import { useQuery, useMutation } 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 { Rating } from "../../../components/ui/rating";
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 { getCategoryName } from "../../../lib/constants";
export default function ProfileScreen() {
const router = useRouter();
const theme = useTheme();
const { token, userId, isAuthenticated, logout } = useAuth();
const user = useQuery(api.users.getCurrentUser, isAuthenticated ? { token: token! } : "skip");
const myAds = useQuery(
api.ads.getByHandyman,
isAuthenticated && userId ? { handymanId: userId as any } : "skip"
);
const deleteAd = useMutation(api.ads.remove);
const logoutMutation = useMutation(api.auth.logout);
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";
return (
{/* Profile header */}
{user.name || "Корисник"}
{user.email && (
{user.email}
)}
{user.phone && (
{user.phone}
)}
{/* Handyman sections */}
{isHandyman && (
<>
Мои огласи
router.push("/ad/create" as any)}>
+ Нов оглас
{myAds === undefined ? (
) : 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.border,
}}
>
Уреди
handleDeleteAd(ad._id)}
style={{
flex: 1,
backgroundColor: theme.colors.errorLight,
borderRadius: theme.radius.md,
paddingVertical: 10,
alignItems: "center",
}}
>
Избриши
))}
)}
>
)}
{/* Customer sections */}
{!isHandyman && (
<>
Мои побарувања
+ Ново побарување
📝
Сеуште немате побарувања. Креирајте ново побарување за да најдете мајстор.
>
)}
{/* Menu items */}
Оцени
Поставки
);
}