import { View, Text, ScrollView, Pressable, Alert } from "react-native"; import { useState } from "react"; import { Stack, useRouter, useLocalSearchParams } from "expo-router"; import { Ionicons } from "@expo/vector-icons"; 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 { Badge } from "../../components/ui/badge"; import { Loading } from "../../components/ui/loading"; import { Card } from "../../components/ui/card"; import { CATEGORIES, CATEGORY_EMOJI, getCategoryName } from "../../lib/constants"; const STEPS = ["Категорија", "Детали", "Локација", "Цена"]; export default function CreateAdScreen() { const theme = useTheme(); const router = useRouter(); const { token, isAuthenticated, userId } = useAuth(); const params = useLocalSearchParams<{ editId?: string }>(); const isEditing = !!params.editId; const existingAd = useQuery( api.ads.getById, isEditing ? { id: params.editId as any } : "skip" ); const user = useQuery( api.users.getCurrentUser, isAuthenticated ? { token: token! } : "skip" ); const createAd = useMutation(api.ads.create); const updateAd = useMutation(api.ads.update); const [category, setCategory] = useState(""); const [title, setTitle] = useState(""); const [description, setDescription] = useState(""); const [location, setLocation] = useState(""); const [priceRange, setPriceRange] = useState(""); const [availability, setAvailability] = useState(""); const [submitting, setSubmitting] = useState(false); const [step, setStep] = useState(0); const isHandyman = user?.role === "handyman"; const canProceed = step === 0 ? !!category : step === 1 ? title.trim().length >= 3 && description.trim().length >= 10 : step === 2 ? location.trim().length > 0 : true; if (!isAuthenticated) { return ( <> Најава потребна Треба да бидете најавени за да креирате оглас. ); } if (user && !isHandyman) { return ( <> Само за мајстори Само мајстори можат да креираат огласи. ); } if (user === undefined) { return ( <> ); } async function handleSubmit() { if (submitting) return; setSubmitting(true); try { if (isEditing && existingAd) { await updateAd({ token: token!, adId: existingAd._id as any, title: title.trim(), description: description.trim(), category, location: location.trim(), priceRange: priceRange.trim() || undefined, availability: availability.trim() || undefined, }); } else { const adId = await createAd({ token: token!, title: title.trim(), description: description.trim(), category, location: location.trim(), priceRange: priceRange.trim() || undefined, availability: availability.trim() || undefined, }); router.replace(`/ad/${adId}`); return; } router.back(); } catch (e: any) { Alert.alert("Грешка", e.message || "Неуспешно зачувување"); } finally { setSubmitting(false); } } return ( <> {/* Progress bar */} {STEPS.map((label, i) => ( setStep(i)} style={{ flex: 1, height: 6, borderRadius: 3, backgroundColor: step >= i ? theme.colors.primary : theme.colors.borderLight, }} /> ))} {STEPS.map((label, i) => ( = i ? theme.colors.primary : theme.colors.textTertiary, }} > {label} ))} {/* Step 0: Category */} {step === 0 && ( Изберете категорија {CATEGORIES.map((cat) => ( setCategory(cat.slug)} style={{ flexDirection: "row", alignItems: "center", gap: theme.spacing.md, padding: theme.spacing.lg, borderRadius: theme.radius.md, borderWidth: 2, borderColor: category === cat.slug ? theme.colors.primary : theme.colors.border, backgroundColor: category === cat.slug ? theme.colors.primaryLight : theme.colors.card, }} > {cat.name} {category === cat.slug && ( )} ))} )} {/* Step 1: Title + Description */} {step === 1 && ( Детали за огласот Наслов * {title.length > 0 && title.length < 3 && ( Насловот треба да има најмалку 3 знаци )} Опис * {description.length > 0 && description.length < 10 && ( Описот треба да има најмалку 10 знаци )} {category && } )} {/* Step 2: Location */} {step === 2 && ( Локација Адреса / град * Избор на локација на мапа — доаѓа наскоро )} {/* Step 3: Price + Availability */} {step === 3 && ( Цена и расположивост Ценовен опсег Расположивост Слики Додавање слики — доаѓа наскоро {/* Summary */} Преглед Категорија: {getCategoryName(category)} Наслов: {title || "—"} Локација: {location || "—"} Цена: {priceRange || "—"} )} {/* Navigation buttons */} {step < 3 && ( {step > 0 && ( )} )} {step === 3 && step > 0 && ( )} ); }