- Error boundary with Macedonian fallback screen and retry button wrapping all navigation - Pull-to-refresh on all list/detail screens (home, explore, posts, chat, profile, ad detail, post detail) - Accessibility: labels on all interactive elements, roles on Pressable/Button, selectable text for user data - Deep linking: mojmajstor:// scheme configured in app.json, ad/[id] and chat/[id] routes work via Expo Router auto-routing - Pagination: cursor-based paginated queries for ads, posts, messages, chats, reviews with Load More buttons on frontend - Profile: shows customer posts with PostCard, loading/error states, create-post navigation - Auth error messages in Macedonian (Невалидни акредитиви, итн.) All UI text in Macedonian. TypeScript clean, Convex functions deployed.
167 lines
6.7 KiB
TypeScript
167 lines
6.7 KiB
TypeScript
import { useCallback, useState } from "react";
|
||
import { View, Text, ScrollView, Alert, RefreshControl } 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 { Badge } from "../../components/ui/badge";
|
||
import { Button } from "../../components/ui/button";
|
||
import { Card } from "../../components/ui/card";
|
||
import { Loading } from "../../components/ui/loading";
|
||
import { Avatar } from "../../components/ui/avatar";
|
||
import { getCategoryName } from "../../lib/constants";
|
||
|
||
export default function PostDetailScreen() {
|
||
const { id } = useLocalSearchParams<{ id: string }>();
|
||
const router = useRouter();
|
||
const theme = useTheme();
|
||
const { token, userId, isAuthenticated } = useAuth();
|
||
|
||
const post = useQuery(api.posts.getById, { id: id as any });
|
||
const currentUser = useQuery(
|
||
api.users.getCurrentUser,
|
||
isAuthenticated && token ? { token } : "skip"
|
||
);
|
||
|
||
const closePost = useMutation(api.posts.close);
|
||
const getOrCreateChat = useMutation(api.chats.getOrCreate);
|
||
const [refreshing, setRefreshing] = useState(false);
|
||
|
||
const onRefresh = useCallback(() => {
|
||
setRefreshing(true);
|
||
setTimeout(() => setRefreshing(false), 800);
|
||
}, []);
|
||
|
||
if (!post) {
|
||
return (
|
||
<>
|
||
<Stack.Screen options={{ title: "Побарување", headerShown: true, headerBackButtonDisplayMode: "minimal" }} />
|
||
<View style={{ flex: 1, justifyContent: "center", alignItems: "center", backgroundColor: theme.colors.background }}>
|
||
<Loading />
|
||
</View>
|
||
</>
|
||
);
|
||
}
|
||
|
||
const customerId = post.customerId as string;
|
||
const isOwner = isAuthenticated && userId && customerId === userId;
|
||
const isHandyman = currentUser?.role === "handyman";
|
||
const isOpen = post.status === "open";
|
||
|
||
async function handleClose() {
|
||
Alert.alert(
|
||
"Затвори побарување",
|
||
"Дали сте сигурни дека сакате да го затворите оваа побарување?",
|
||
[
|
||
{ text: "Откажи", style: "cancel" },
|
||
{
|
||
text: "Затвори",
|
||
style: "destructive",
|
||
onPress: async () => {
|
||
try {
|
||
await closePost({ token: token!, postId: id as any });
|
||
} catch (e: any) {
|
||
Alert.alert("Грешка", e.message || "Неуспешно затворање");
|
||
}
|
||
},
|
||
},
|
||
]
|
||
);
|
||
}
|
||
|
||
async function handleRespond() {
|
||
try {
|
||
const chatId = await getOrCreateChat({ token: token!, partnerId: post!.customerId });
|
||
router.push(`/chat/${chatId}` as any);
|
||
} catch (e: any) {
|
||
Alert.alert("Грешка", e.message || "Неуспешно поврзување");
|
||
}
|
||
}
|
||
|
||
const categoryName = post.category ? getCategoryName(post.category) : null;
|
||
const timeAgo = getTimeAgo(post.createdAt);
|
||
|
||
return (
|
||
<>
|
||
<Stack.Screen options={{ title: "Побарување", headerShown: true, headerBackButtonDisplayMode: "minimal" }} />
|
||
<ScrollView
|
||
contentInsetAdjustmentBehavior="automatic"
|
||
refreshControl={
|
||
<RefreshControl refreshing={refreshing} onRefresh={onRefresh} tintColor={theme.colors.primary} />
|
||
}
|
||
contentContainerStyle={{ padding: theme.spacing.lg, gap: theme.spacing.md, paddingBottom: 100 }}>
|
||
<View style={{ flexDirection: "row", justifyContent: "space-between", alignItems: "flex-start" }}>
|
||
<View style={{ flex: 1 }}>
|
||
<Text style={{ fontSize: 22, fontWeight: "700", color: theme.colors.text }}>
|
||
{post.title}
|
||
</Text>
|
||
</View>
|
||
<Badge label={isOpen ? "Отворено" : "Затворено"} variant={isOpen ? "success" : "error"} />
|
||
</View>
|
||
|
||
<Card>
|
||
<View style={{ gap: theme.spacing.sm }}>
|
||
{categoryName && (
|
||
<View style={{ flexDirection: "row", alignItems: "center", gap: 8 }}>
|
||
<Text style={{ fontSize: 14, color: theme.colors.textSecondary }}>Категорија:</Text>
|
||
<Badge label={categoryName} variant="primary" />
|
||
</View>
|
||
)}
|
||
{post.location && (
|
||
<View style={{ flexDirection: "row", alignItems: "center", gap: 8 }}>
|
||
<Text style={{ fontSize: 14, color: theme.colors.textSecondary }}>📍 Локација:</Text>
|
||
<Text selectable style={{ fontSize: 14, color: theme.colors.text }}>{post.location}</Text>
|
||
</View>
|
||
)}
|
||
{post.budget && (
|
||
<View style={{ flexDirection: "row", alignItems: "center", gap: 8 }}>
|
||
<Text style={{ fontSize: 14, color: theme.colors.textSecondary }}>💰 Буџет:</Text>
|
||
<Text selectable style={{ fontSize: 14, fontWeight: "600", color: theme.colors.primary }}>{post.budget}</Text>
|
||
</View>
|
||
)}
|
||
<View style={{ flexDirection: "row", alignItems: "center", gap: 8 }}>
|
||
<Text style={{ fontSize: 14, color: theme.colors.textSecondary }}>🗓 Објавено:</Text>
|
||
<Text style={{ fontSize: 14, color: theme.colors.text }}>{timeAgo}</Text>
|
||
</View>
|
||
</View>
|
||
</Card>
|
||
|
||
<Card>
|
||
<Text style={{ fontSize: 16, color: theme.colors.text, lineHeight: 24 }}>
|
||
{post.description}
|
||
</Text>
|
||
</Card>
|
||
</ScrollView>
|
||
|
||
{isOwner && isOpen && (
|
||
<View style={{ position: "absolute", bottom: 0, left: 0, right: 0, padding: theme.spacing.lg, backgroundColor: theme.colors.background }}>
|
||
<Button variant="destructive" onPress={handleClose}>
|
||
Затвори оглас
|
||
</Button>
|
||
</View>
|
||
)}
|
||
|
||
{!isOwner && isHandyman && isOpen && isAuthenticated && (
|
||
<View style={{ position: "absolute", bottom: 0, left: 0, right: 0, padding: theme.spacing.lg, backgroundColor: theme.colors.background }}>
|
||
<Button onPress={handleRespond}>
|
||
Одговори
|
||
</Button>
|
||
</View>
|
||
)}
|
||
</>
|
||
);
|
||
}
|
||
|
||
function getTimeAgo(timestamp: number): string {
|
||
const seconds = Math.floor((Date.now() - timestamp) / 1000);
|
||
if (seconds < 60) return "Пред неколку секунди";
|
||
const minutes = Math.floor(seconds / 60);
|
||
if (minutes < 60) return `Пред ${minutes} мин`;
|
||
const hours = Math.floor(minutes / 60);
|
||
if (hours < 24) return `Пред ${hours} ч`;
|
||
const days = Math.floor(hours / 24);
|
||
if (days < 30) return `Пред ${days} ден`;
|
||
const months = Math.floor(days / 30);
|
||
return `Пред ${months} месеци`;
|
||
} |