'use client'; import { useEffect, useState } from 'react'; import { useI18n } from '../../components/I18nProvider'; import type { Locale } from '../../../lib/i18n'; type ImageInput = { url: string; altText?: string }; export default function NewListingPage() { const { t, locale: uiLocale } = useI18n(); const [slug, setSlug] = useState(''); const [locale, setLocale] = useState(uiLocale); const [title, setTitle] = useState(''); const [description, setDescription] = useState(''); const [teaser, setTeaser] = useState(''); const [country, setCountry] = useState('Finland'); const [region, setRegion] = useState(''); const [city, setCity] = useState(''); const [streetAddress, setStreetAddress] = useState(''); const [addressNote, setAddressNote] = useState(''); const [latitude, setLatitude] = useState(''); const [longitude, setLongitude] = useState(''); const [contactName, setContactName] = useState(''); const [contactEmail, setContactEmail] = useState(''); const [maxGuests, setMaxGuests] = useState(4); const [bedrooms, setBedrooms] = useState(2); const [beds, setBeds] = useState(3); const [bathrooms, setBathrooms] = useState(1); const [price, setPrice] = useState(''); const [hasSauna, setHasSauna] = useState(true); const [hasFireplace, setHasFireplace] = useState(true); const [hasWifi, setHasWifi] = useState(true); const [petsAllowed, setPetsAllowed] = useState(false); const [byTheLake, setByTheLake] = useState(false); const [hasAirConditioning, setHasAirConditioning] = useState(false); const [evCharging, setEvCharging] = useState<'NONE' | 'FREE' | 'PAID'>('NONE'); const [imagesText, setImagesText] = useState(''); const [coverImageIndex, setCoverImageIndex] = useState(1); const [message, setMessage] = useState(null); const [error, setError] = useState(null); const [loading, setLoading] = useState(false); const [isAuthed, setIsAuthed] = useState(false); useEffect(() => { setLocale(uiLocale); }, [uiLocale]); useEffect(() => { // simple check if session exists fetch('/api/auth/me', { cache: 'no-store' }) .then((res) => res.json()) .then((data) => setIsAuthed(Boolean(data.user))) .catch(() => setIsAuthed(false)); }, []); function parseImages(): ImageInput[] { return imagesText .split('\n') .map((line) => line.trim()) .filter(Boolean) .map((line) => ({ url: line })); } async function onSubmit(e: React.FormEvent) { e.preventDefault(); setMessage(null); setError(null); setLoading(true); try { const res = await fetch('/api/listings', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ slug, locale, title, description, teaser, country, region, city, streetAddress, addressNote, latitude: latitude === '' ? null : latitude, longitude: longitude === '' ? null : longitude, contactName, contactEmail, maxGuests, bedrooms, beds, bathrooms, priceHintPerNightCents: price === '' ? null : Number(price), hasSauna, hasFireplace, hasWifi, petsAllowed, byTheLake, hasAirConditioning, evCharging, coverImageIndex, images: parseImages(), }), }); const data = await res.json(); if (!res.ok) { setError(data.error || 'Failed to create listing'); } else { setMessage(t('createListingSuccess', { id: data.listing.id, status: data.listing.status })); setSlug(''); setTitle(''); setDescription(''); setTeaser(''); setRegion(''); setCity(''); setStreetAddress(''); setAddressNote(''); setLatitude(''); setLongitude(''); setContactName(''); setContactEmail(''); setImagesText(''); setCoverImageIndex(1); } } catch (err) { setError('Failed to create listing'); } finally { setLoading(false); } } if (!isAuthed) { return (

{t('createListingTitle')}

{t('loginToCreate')}

); } return (

{t('createListingTitle')}