-
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
import { useState } from 'react'
|
||||
import { Link } from 'react-router-dom'
|
||||
import { ImageOff, Plus } from 'lucide-react'
|
||||
import { useApi } from '../../hooks/useApi'
|
||||
import { EmptyBlock, ErrorBlock, LoadingBlock } from '../components/feedback'
|
||||
import { aInput, Btn, fmtDay, PageHeader, Pagination, Pill } from '../components/kit'
|
||||
|
||||
const PAGE_SIZE = 20
|
||||
|
||||
export default function ProjectsPage() {
|
||||
const [filters, setFilters] = useState({ search: '', category: '', is_published: '' })
|
||||
const [page, setPage] = useState(1)
|
||||
const params = Object.fromEntries(Object.entries({ ...filters, page, page_size: PAGE_SIZE }).filter(([, v]) => v !== ''))
|
||||
const { data, loading, error, reload } = useApi('/admin/projects', { params })
|
||||
const set = (k) => (e) => {
|
||||
setFilters((f) => ({ ...f, [k]: e.target.value }))
|
||||
setPage(1)
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<PageHeader
|
||||
title="Projects"
|
||||
subtitle="Completed installations shown in the public project gallery."
|
||||
actions={<Link to="/admin/projects/new"><Btn as="span"><Plus className="size-4" /> Add project</Btn></Link>}
|
||||
/>
|
||||
<div className="mb-4 grid gap-3 sm:grid-cols-[1fr_180px_180px]">
|
||||
<input className={aInput} placeholder="Search title or location" value={filters.search} onChange={set('search')} aria-label="Search projects" />
|
||||
<select className={aInput} value={filters.category} onChange={set('category')} aria-label="Category">
|
||||
<option value="">All categories</option>
|
||||
<option value="residential">Residential</option>
|
||||
<option value="commercial">Commercial</option>
|
||||
</select>
|
||||
<select className={aInput} value={filters.is_published} onChange={set('is_published')} aria-label="Visibility">
|
||||
<option value="">Published & drafts</option>
|
||||
<option value="true">Published</option>
|
||||
<option value="false">Drafts</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
{loading && !data ? (
|
||||
<LoadingBlock />
|
||||
) : error ? (
|
||||
<ErrorBlock message={error} onRetry={reload} />
|
||||
) : data.items.length === 0 ? (
|
||||
<EmptyBlock title="No projects found" text="Add a project, upload its photos and publish it to show it on the website." action={<Link to="/admin/projects/new"><Btn as="span"><Plus className="size-4" /> Add project</Btn></Link>} />
|
||||
) : (
|
||||
<ul className={`grid gap-4 sm:grid-cols-2 xl:grid-cols-3 ${loading ? 'opacity-60' : ''}`}>
|
||||
{data.items.map((p) => (
|
||||
<li key={p.id}>
|
||||
<Link to={`/admin/projects/${p.id}`} className="group block overflow-hidden rounded-2xl bg-white ring-1 ring-line transition hover:ring-brand/40">
|
||||
<div className="relative aspect-[16/10] bg-surface">
|
||||
{p.featured_image ? (
|
||||
<img src={p.featured_image.thumbnail_url} alt="" className="size-full object-cover" loading="lazy" />
|
||||
) : (
|
||||
<div className="grid size-full place-items-center text-[#98A2B3]"><ImageOff className="size-7" /></div>
|
||||
)}
|
||||
<span className="absolute top-3 left-3"><Pill on={p.is_published}>{p.is_published ? 'Published' : 'Draft'}</Pill></span>
|
||||
<span className="absolute right-3 bottom-3 rounded-full bg-black/60 px-2.5 py-0.5 text-[12px] font-semibold text-white">{p.images.length} photo{p.images.length === 1 ? '' : 's'}</span>
|
||||
</div>
|
||||
<div className="p-4">
|
||||
<p className="font-bold text-navy group-hover:text-brand">{p.title}</p>
|
||||
<p className="mt-1 text-[13px] text-slate capitalize">
|
||||
{p.category}{p.capacity_kw ? ` · ${p.capacity_kw} kW` : ''}{p.location ? ` · ${p.location}` : ''}
|
||||
</p>
|
||||
<p className="mt-1 text-[12px] text-[#98A2B3]">Installed {fmtDay(p.installation_date)}</p>
|
||||
</div>
|
||||
</Link>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
{data && <Pagination page={page} pageSize={PAGE_SIZE} total={data.total} onPage={setPage} />}
|
||||
</>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user