import { useState } from "react";
import { View, Text, Pressable, Alert, ScrollView } from "react-native";
import { useLocalSearchParams, Stack, 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 { Input } from "../../components/ui/input";
import { Loading } from "../../components/ui/loading";
export default function CreateReviewScreen() {
const { adId } = useLocalSearchParams<{ adId: string }>();
const theme = useTheme();
const router = useRouter();
const { token, isAuthenticated, userId } = useAuth();
const ad = useQuery(api.ads.getById, adId ? { id: adId as any } : "skip");
const handyman = useQuery(
api.users.getById,
ad ? { id: ad.handymanId } : "skip"
);
const [rating, setRating] = useState(0);
const [comment, setComment] = useState("");
const [submitting, setSubmitting] = useState(false);
const createReview = useMutation(api.reviews.create);
if (!isAuthenticated) {
return (
<>
Најавете се за да напишете оценка.
>
);
}
async function handleSubmit() {
if (rating === 0) {
Alert.alert("Грешка", "Одберете оценка (1-5 ѕвезди)");
return;
}
if (!token || !adId) return;
setSubmitting(true);
try {
await createReview({ token, adId: adId as any, rating, comment: comment.trim() || undefined });
router.back();
} catch (e: any) {
Alert.alert("Грешка", e.message || "Неуспешно креирање оценка");
} finally {
setSubmitting(false);
}
}
if (!ad) {
return (
<>
>
);
}
return (
<>
{ad.title}
{handyman && (
Мајстор: {handyman.name}
)}
Оценка
{[1, 2, 3, 4, 5].map((star) => (
setRating(star)}
style={{ padding: 4 }}
>
★
))}
{rating > 0 && (
{rating === 1 && "Многу лошо"}
{rating === 2 && "Лошо"}
{rating === 3 && "Просечно"}
{rating === 4 && "Добро"}
{rating === 5 && "Одлично"}
)}
Коментар (опционално)
>
);
}