import { useEffect, useState } from 'react' import { Link, useNavigate, useParams } from 'react-router-dom' import { ArrowLeft, Download, ExternalLink, Star, Trash2 } from 'lucide-react' import { useApi } from '../../hooks/useApi' import api, { errorMessage, fieldErrors } from '../../services/api' import { ErrorBlock, LoadingBlock, useFeedback } from '../components/feedback' import ImageUploader from '../components/ImageUploader' import { aInput, AField, Btn, Card, PageHeader, Pill, Toggle } from '../components/kit' const EMPTY = { title: '', category: 'residential', location: '', capacity_kw: '', installation_date: '', description: '', status: 'completed', is_published: false } function toForm(p) { return { title: p.title, category: p.category, location: p.location || '', capacity_kw: p.capacity_kw ?? '', installation_date: p.installation_date || '', description: p.description || '', status: p.status, is_published: p.is_published, } } function toPayload(f) { return { ...f, location: f.location.trim() || null, description: f.description.trim() || null, capacity_kw: f.capacity_kw === '' ? null : Number(f.capacity_kw), installation_date: f.installation_date || null, } } async function downloadOriginal(img) { const res = await api.get(img.original_download_path.replace(/^\/api/, ''), { responseType: 'blob' }) const url = URL.createObjectURL(res.data) const a = document.createElement('a') a.href = url a.download = img.original_filename a.click() setTimeout(() => URL.revokeObjectURL(url), 2000) } function Gallery({ project, onChange }) { const { toast, confirm } = useFeedback() const [busyId, setBusyId] = useState(null) const [captions, setCaptions] = useState({}) const act = async (id, fn, okMsg) => { setBusyId(id) try { const r = await fn() if (r?.data?.images) onChange(r.data) if (okMsg) toast(okMsg) return true } catch (e) { toast(errorMessage(e), 'error') return false } finally { setBusyId(null) } } const remove = async (img) => { const ok = await confirm({ title: 'Delete photo?', message: `"${img.original_filename}" and its watermarked copy will be permanently deleted.`, confirmLabel: 'Delete photo', danger: true }) if (!ok) return const done = await act(img.id, () => api.delete(`/admin/project-images/${img.id}`), 'Photo deleted') if (done) { const r = await api.get(`/admin/projects/${project.id}`) onChange(r.data) } } return (

Photos ({project.images.length})

The public gallery shows watermarked copies only.
{ onChange(res.project) toast(`${res.uploaded.length} photo${res.uploaded.length === 1 ? '' : 's'} uploaded and watermarked`) }} /> {project.images.length > 0 && ( )}
) } export default function ProjectEditorPage() { const { id } = useParams() const isNew = !id const navigate = useNavigate() const { toast, confirm } = useFeedback() const { data, loading, error, reload, setData } = useApi(`/admin/projects/${id}`, { enabled: !isNew }) const [form, setForm] = useState(EMPTY) const [errors, setErrors] = useState({}) const [saving, setSaving] = useState(false) // Fill the form once per loaded project (photo changes must not wipe unsaved edits). const loadedId = data?.id useEffect(() => { if (data) setForm(toForm(data)) // eslint-disable-next-line react-hooks/exhaustive-deps }, [loadedId]) const set = (k) => (e) => { setForm((f) => ({ ...f, [k]: e?.target ? e.target.value : e })) setErrors((er) => ({ ...er, [k]: undefined })) } const save = async (ev) => { ev?.preventDefault() const e = {} if (form.title.trim().length < 2) e.title = 'Enter a project title' if (form.capacity_kw !== '' && !(Number(form.capacity_kw) > 0)) e.capacity_kw = 'Capacity must be a positive number' setErrors(e) if (Object.keys(e).length) return setSaving(true) try { if (isNew) { const r = await api.post('/admin/projects', toPayload(form)) toast('Project created — now add photos') navigate(`/admin/projects/${r.data.id}`, { replace: true }) } else { const r = await api.put(`/admin/projects/${id}`, toPayload(form)) setData(r.data) setForm(toForm(r.data)) toast('Project saved') } } catch (err) { setErrors(fieldErrors(err)) toast(errorMessage(err), 'error') } finally { setSaving(false) } } const remove = async () => { const ok = await confirm({ title: 'Delete project?', message: `"${data.title}" and all ${data.images.length} photos will be permanently deleted.`, confirmLabel: 'Delete project', danger: true }) if (!ok) return try { await api.delete(`/admin/projects/${id}`) toast('Project deleted') navigate('/admin/projects', { replace: true }) } catch (err) { toast(errorMessage(err), 'error') } } if (!isNew && error) return if (!isNew && (!data || String(data.id) !== String(id))) return return ( <> Projects {data.is_published ? 'Published' : 'Draft'} /{data.slug}} actions={ !isNew && ( <> {data.is_published && ( View on site )} Delete ) } />