feat(manager): Media page, photo field for products and categories (platform-v2 step 5.2)
- Management → Media (src/pages/media/): every picture and video, search, pictures / videos, batch upload (many files or drop on the page) with a result per file, details (preview, display name, kind, resolution, length, file size, uploaded file, when / who, the sizes made, what uses it, delete off while used). - MediaField: upload on the spot (into the library and set at once), pick from the library (MediaPicker, which can also upload), remove; after an upload on the spot "Name it after the product / category" renames it in Latin letters. Used by the product editor and the category dialog. - nav: `anyPerm` (any of the keys that manage pictures); the route guard uses the same rule. Product rows have test ids. - @xenia/i18n: latin() (Greek to Latin, the way venues write it) + tests. - tests: manager media.test.mjs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import { useState, useEffect, useRef } from 'react'
|
||||
import { useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import toast from 'react-hot-toast'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import client from '../../api/client'
|
||||
import { useTranslation } from '@xenia/i18n'
|
||||
import { SALES_POINTS } from '@xenia/access'
|
||||
import MediaField from '../media/MediaField'
|
||||
|
||||
// ── Small helpers ─────────────────────────────────────────────────────────────
|
||||
function moveItem(arr, i, dir) {
|
||||
@@ -202,6 +202,8 @@ function buildFormFromProduct(product) {
|
||||
digital_name: product.digital_name ?? '',
|
||||
digital_description: product.digital_description ?? '',
|
||||
digital_image_url: product.digital_image_url ?? '',
|
||||
// Its photo in the media library (plan step 5.2): { id, name, thumb, card, full } or null
|
||||
image: product.image ?? null,
|
||||
// Phase 2A — cost tracking
|
||||
cost_mode: product.cost_breakdown?.length ? 'detailed' : 'simple',
|
||||
cost_simple: product.cost_simple ?? '',
|
||||
@@ -261,9 +263,7 @@ export default function ProductFormModal({ product, categories, printers, prepZo
|
||||
const [form, setForm] = useState(() => buildFormFromProduct(product))
|
||||
const [activeTab, setActiveTab] = useState('favorites')
|
||||
const [leftTab, setLeftTab] = useState('info')
|
||||
const [imageFile, setImageFile] = useState(null)
|
||||
const [uploading, setUploading] = useState(false)
|
||||
const qc = useQueryClient()
|
||||
const { t: tm } = useTranslation('manager')
|
||||
|
||||
// Fetch fiscal VAT groups from settings (needed for the Ταμειακή tab dropdown)
|
||||
const { data: settingsData } = useQuery({
|
||||
@@ -277,7 +277,8 @@ export default function ProductFormModal({ product, categories, printers, prepZo
|
||||
})()
|
||||
|
||||
useEffect(() => {
|
||||
function onKey(e) { if (e.key === 'Escape') onClose() }
|
||||
// Escape inside a dialog on top (the media picker) closes only that dialog
|
||||
function onKey(e) { if (e.key === 'Escape' && !document.querySelector('.x-dialog')) onClose() }
|
||||
window.addEventListener('keydown', onKey)
|
||||
return () => window.removeEventListener('keydown', onKey)
|
||||
}, [onClose])
|
||||
@@ -286,7 +287,6 @@ export default function ProductFormModal({ product, categories, printers, prepZo
|
||||
setForm(buildFormFromProduct(product))
|
||||
setActiveTab('favorites')
|
||||
setLeftTab('info')
|
||||
setImageFile(null)
|
||||
}, [product.id, product.name])
|
||||
|
||||
function setField(k, v) { setForm(f => ({ ...f, [k]: v })) }
|
||||
@@ -575,33 +575,11 @@ export default function ProductFormModal({ product, categories, printers, prepZo
|
||||
// Fiscal printer (ΦΗΜ)
|
||||
fiscal_name: form.fiscal_name?.trim() || null,
|
||||
fiscal_vat_group_id: form.fiscal_vat_group_id ? parseInt(form.fiscal_vat_group_id, 10) : null,
|
||||
media_id: form.image?.id ?? null,
|
||||
}
|
||||
}
|
||||
|
||||
async function submit() {
|
||||
if (!imageFile) { onSave(buildBody()); return }
|
||||
if (!isNew) {
|
||||
onSave(buildBody())
|
||||
setUploading(true)
|
||||
try {
|
||||
const fd = new FormData(); fd.append('file', imageFile)
|
||||
await client.post(`/api/products/${product.id}/image`, fd)
|
||||
qc.invalidateQueries({ queryKey: ['products-all'] })
|
||||
} catch { toast.error('Σφάλμα ανεβάσματος εικόνας') }
|
||||
finally { setUploading(false) }
|
||||
} else {
|
||||
setUploading(true)
|
||||
try {
|
||||
const res = await client.post('/api/products/', buildBody())
|
||||
const newId = res.data.id
|
||||
const fd = new FormData(); fd.append('file', imageFile)
|
||||
await client.post(`/api/products/${newId}/image`, fd)
|
||||
qc.invalidateQueries({ queryKey: ['products-all'] })
|
||||
onClose()
|
||||
} catch { toast.error('Σφάλμα αποθήκευσης') }
|
||||
finally { setUploading(false) }
|
||||
}
|
||||
}
|
||||
function submit() { onSave(buildBody()) }
|
||||
|
||||
const isNew = !product.id
|
||||
const canSave = form.name.trim() && (form.is_service_item || (form.base_price !== '' && form.base_price !== null && form.base_price !== undefined))
|
||||
@@ -825,19 +803,10 @@ export default function ProductFormModal({ product, categories, printers, prepZo
|
||||
</button>
|
||||
</div>}
|
||||
|
||||
{!form.is_service_item && <div>
|
||||
<label className="label">Εικόνα προϊόντος</label>
|
||||
{product.image_url && (
|
||||
<img src={product.image_url}
|
||||
className="w-16 h-16 rounded-xl object-cover border border-gray-200 mb-2" alt="" />
|
||||
)}
|
||||
{imageFile && <div className="text-xs text-primary-700 font-medium mb-1 break-all">{imageFile.name}</div>}
|
||||
<label className="cursor-pointer inline-flex items-center gap-2 px-3 py-2 rounded-lg border border-gray-300 bg-white hover:bg-gray-50 text-sm text-gray-600 transition-colors">
|
||||
<svg width="14" height="14" viewBox="0 0 24 24" fill="none"><path d="M21 15v4a2 2 0 01-2 2H5a2 2 0 01-2-2v-4M17 8l-5-5-5 5M12 3v12" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/></svg>
|
||||
{imageFile ? 'Αλλαγή εικόνας' : 'Επιλογή εικόνας'}
|
||||
<input type="file" accept="image/*" className="sr-only" onChange={e => setImageFile(e.target.files[0] ?? null)} />
|
||||
</label>
|
||||
</div>}
|
||||
{!form.is_service_item && (
|
||||
<MediaField value={form.image} onChange={image => setField('image', image)} nameAfter={form.name}
|
||||
label={tm('media.photo')} testid="product-photo" />
|
||||
)}
|
||||
</>)}
|
||||
|
||||
{/* Tab 2: where it's sold (phase 3S) and what the customer apps show */}
|
||||
@@ -1330,8 +1299,8 @@ export default function ProductFormModal({ product, categories, printers, prepZo
|
||||
📋 Αντιγραφή
|
||||
</button>
|
||||
)}
|
||||
<button onClick={submit} disabled={!canSave || uploading} className="btn btn-primary px-8">
|
||||
{uploading ? 'Ανέβασμα…' : isNew ? 'Δημιουργία' : 'Αποθήκευση'}
|
||||
<button onClick={submit} disabled={!canSave} className="btn btn-primary px-8">
|
||||
{isNew ? 'Δημιουργία' : 'Αποθήκευση'}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -4,6 +4,8 @@ import toast from 'react-hot-toast'
|
||||
import client from '../../api/client'
|
||||
import { ConfirmModal } from '../../ui/Modal'
|
||||
import ProductFormModal from './ProductFormModal'
|
||||
import MediaField from '../media/MediaField'
|
||||
import { useTranslation } from '@xenia/i18n'
|
||||
|
||||
// ── Icons ─────────────────────────────────────────────────────────────────────
|
||||
function Icon({ name, size = 14, className = '' }) {
|
||||
@@ -50,7 +52,7 @@ function PhotoThumb({ product, size = 44 }) {
|
||||
if (product.image_url) {
|
||||
return (
|
||||
<img
|
||||
src={product.image_url}
|
||||
src={product.image?.thumb || product.image_url}
|
||||
alt={product.name}
|
||||
style={{ width: size, height: size }}
|
||||
className="rounded-lg object-cover shrink-0 border border-gray-100"
|
||||
@@ -814,6 +816,7 @@ function ProductRow({ p, cat, parentCat, selected, onToggleSelect, onEdit, onArc
|
||||
opacity: dragging ? 0.5 : 1,
|
||||
}}
|
||||
className="group hover:bg-gray-50"
|
||||
data-testid="product-row" data-name={p.name}
|
||||
>
|
||||
{/* Drag grip */}
|
||||
<span style={{ color: '#d1d5db', cursor: 'grab', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
|
||||
@@ -912,7 +915,7 @@ function ProductRow({ p, cat, parentCat, selected, onToggleSelect, onEdit, onArc
|
||||
)}
|
||||
|
||||
{/* Edit — pill style */}
|
||||
<button onClick={e => { e.stopPropagation(); onEdit(p) }}
|
||||
<button onClick={e => { e.stopPropagation(); onEdit(p) }} data-testid="product-edit"
|
||||
style={{
|
||||
display: 'flex', alignItems: 'center', gap: 5, padding: '4px 10px',
|
||||
background: '#f3f4f6', border: '1px solid #e5e7eb',
|
||||
@@ -1647,7 +1650,7 @@ export default function ProductsTab() {
|
||||
<CategoryModal
|
||||
cat={editCat}
|
||||
parentName={editCat.parent_id ? categories.find(c => c.id === editCat.parent_id)?.name : null}
|
||||
onSave={(name, color) => saveCat.mutate({ name, color, parent_id: editCat.parent_id ?? null })}
|
||||
onSave={(name, color, image) => saveCat.mutate({ name, color, parent_id: editCat.parent_id ?? null, media_id: image?.id ?? null })}
|
||||
onClose={() => setEditCat(null)}
|
||||
/>
|
||||
)}
|
||||
@@ -1711,12 +1714,14 @@ const COLORS = ['#6366f1','#0ea5e9','#10b981','#f59e0b','#ef4444','#ec4899','#8b
|
||||
function CategoryModal({ cat, parentName, onSave, onClose }) {
|
||||
const [name, setName] = useState(cat.name || '')
|
||||
const [color, setColor] = useState(cat.color || COLORS[0])
|
||||
const [image, setImage] = useState(cat.image ?? null)
|
||||
const { t } = useTranslation('manager')
|
||||
const isNew = !cat.id
|
||||
const isSub = !!cat.parent_id
|
||||
|
||||
return (
|
||||
<div style={{ position: 'fixed', inset: 0, background: 'rgba(0,0,0,0.4)', display: 'flex', alignItems: 'center', justifyContent: 'center', zIndex: 50, padding: 16 }}>
|
||||
<div style={{ background: '#fff', borderRadius: 16, boxShadow: '0 20px 60px rgba(0,0,0,0.15)', width: '100%', maxWidth: 380, padding: 24 }}>
|
||||
<div style={{ background: '#fff', borderRadius: 16, boxShadow: '0 20px 60px rgba(0,0,0,0.15)', width: '100%', maxWidth: 440, padding: 24 }}>
|
||||
<h2 style={{ margin: '0 0 4px', fontWeight: 700, fontSize: 16, color: '#111827' }}>
|
||||
{isNew ? (isSub ? 'Νέα υποκατηγορία' : 'Νέα κατηγορία') : (isSub ? 'Επεξεργασία υποκατηγορίας' : 'Επεξεργασία κατηγορίας')}
|
||||
</h2>
|
||||
@@ -1732,6 +1737,10 @@ function CategoryModal({ cat, parentName, onSave, onClose }) {
|
||||
style={{ width: '100%', padding: '9px 12px', border: '1px solid #e5e7eb', borderRadius: 8, fontSize: 14, outline: 'none', boxSizing: 'border-box' }}
|
||||
/>
|
||||
</div>
|
||||
<div style={{ marginBottom: 14 }}>
|
||||
<MediaField value={image} onChange={setImage} nameAfter={name} nameAfterLabel={t('media.nameAfterCategory')}
|
||||
label={t('media.photo')} testid="category-photo" />
|
||||
</div>
|
||||
<div style={{ marginBottom: 20 }}>
|
||||
<label style={{ fontSize: 12, fontWeight: 600, color: '#6b7280', display: 'block', marginBottom: 8 }}>Χρώμα</label>
|
||||
<div style={{ display: 'grid', gridTemplateColumns: 'repeat(6, 1fr)', gap: 8 }}>
|
||||
@@ -1746,7 +1755,7 @@ function CategoryModal({ cat, parentName, onSave, onClose }) {
|
||||
<button onClick={onClose} style={{ flex: 1, padding: '9px 0', border: '1px solid #e5e7eb', background: '#fff', borderRadius: 8, fontSize: 14, cursor: 'pointer', color: '#374151' }}>
|
||||
Ακύρωση
|
||||
</button>
|
||||
<button onClick={() => onSave(name, color)} disabled={!name.trim()}
|
||||
<button onClick={() => onSave(name, color, image)} disabled={!name.trim()}
|
||||
style={{ flex: 1, padding: '9px 0', border: 'none', background: name.trim() ? '#111827' : '#d1d5db', color: '#fff', borderRadius: 8, fontSize: 14, fontWeight: 600, cursor: name.trim() ? 'pointer' : 'not-allowed' }}>
|
||||
Αποθήκευση
|
||||
</button>
|
||||
|
||||
Reference in New Issue
Block a user