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>
|
||||
|
||||
Reference in New Issue
Block a user