Multi-tenant digital menu platform (Collina Systems): public QR menu with glassmorphism + 3D macro flip cards, mobile-first editor with draft/publish versioning (5-version history + restore), AI consultant & translation (Claude Haiku), realtime orders via SSE, configurable palette, QR generation, superadmin panel, and Docker/Coolify infra.
421 lines
13 KiB
TypeScript
421 lines
13 KiB
TypeScript
'use client';
|
||
|
||
import { useState } from 'react';
|
||
import { useTranslations } from 'next-intl';
|
||
import type { EditorProduct, EditorSection } from './types';
|
||
import { MACRO_KEYS } from '@/lib/macros';
|
||
import { api } from './api';
|
||
|
||
const ALLERGENS = ['gluten', 'lácteos', 'frutos secos', 'mariscos', 'huevo', 'soya', 'pescado'];
|
||
const DIET_TAGS = ['vegetariano', 'vegano', 'sin gluten', 'sin lácteos', 'keto', 'picante'];
|
||
|
||
export default function ProductEditor({
|
||
slug,
|
||
product,
|
||
sections,
|
||
onClose,
|
||
onSave,
|
||
onDelete,
|
||
}: {
|
||
slug: string;
|
||
product: EditorProduct;
|
||
sections: EditorSection[];
|
||
onClose: () => void;
|
||
onSave: (p: EditorProduct) => void;
|
||
onDelete: (p: EditorProduct) => void;
|
||
}) {
|
||
const t = useTranslations('editor');
|
||
const tm = useTranslations('macros');
|
||
const [draft, setDraft] = useState<EditorProduct>(product);
|
||
const [saving, setSaving] = useState(false);
|
||
const [translating, setTranslating] = useState<string | null>(null);
|
||
const [uploading, setUploading] = useState(false);
|
||
|
||
function set<K extends keyof EditorProduct>(key: K, value: EditorProduct[K]) {
|
||
setDraft((d) => ({ ...d, [key]: value }));
|
||
}
|
||
|
||
async function save() {
|
||
setSaving(true);
|
||
try {
|
||
await api(slug, `/products/${draft.id}`, {
|
||
method: 'PATCH',
|
||
json: {
|
||
name: draft.name,
|
||
description: draft.description,
|
||
price: draft.price,
|
||
sectionId: draft.sectionId,
|
||
allergens: draft.allergens,
|
||
dietTags: draft.dietTags,
|
||
macros: draft.macros,
|
||
media: draft.media,
|
||
translationStatus: draft.translationStatus,
|
||
},
|
||
});
|
||
onSave(draft);
|
||
onClose();
|
||
} finally {
|
||
setSaving(false);
|
||
}
|
||
}
|
||
|
||
async function translate(field: 'name' | 'description') {
|
||
const from = draft[field].es ? 'es' : 'en';
|
||
const text = draft[field][from];
|
||
if (!text) return;
|
||
setTranslating(field);
|
||
try {
|
||
const res = await api<{ translation: string; to: 'es' | 'en' }>(slug, '/translate', {
|
||
method: 'POST',
|
||
json: { text, from },
|
||
});
|
||
setDraft((d) => ({
|
||
...d,
|
||
[field]: { ...d[field], [res.to]: res.translation },
|
||
translationStatus: { ...d.translationStatus, en: 'auto' as const },
|
||
}));
|
||
} finally {
|
||
setTranslating(null);
|
||
}
|
||
}
|
||
|
||
async function upload(files: FileList | null) {
|
||
if (!files?.length) return;
|
||
setUploading(true);
|
||
try {
|
||
const added: EditorProduct['media'] = [];
|
||
for (const file of Array.from(files)) {
|
||
const form = new FormData();
|
||
form.append('file', file);
|
||
const res = await api<{ path: string; type: 'image' | 'video' }>(slug, '/upload', {
|
||
method: 'POST',
|
||
body: form,
|
||
});
|
||
added.push({ type: res.type, path: res.path, order: draft.media.length + added.length });
|
||
}
|
||
setDraft((d) => ({ ...d, media: [...d.media, ...added] }));
|
||
} finally {
|
||
setUploading(false);
|
||
}
|
||
}
|
||
|
||
function moveMedia(index: number, dir: -1 | 1) {
|
||
setDraft((d) => {
|
||
const media = [...d.media];
|
||
const to = index + dir;
|
||
if (to < 0 || to >= media.length) return d;
|
||
[media[index], media[to]] = [media[to], media[index]];
|
||
return { ...d, media: media.map((m, i) => ({ ...m, order: i })) };
|
||
});
|
||
}
|
||
|
||
function removeMedia(index: number) {
|
||
setDraft((d) => ({
|
||
...d,
|
||
media: d.media.filter((_, i) => i !== index).map((m, i) => ({ ...m, order: i })),
|
||
}));
|
||
}
|
||
|
||
function toggleIn(list: string[], value: string): string[] {
|
||
return list.includes(value) ? list.filter((x) => x !== value) : [...list, value];
|
||
}
|
||
|
||
const enStatus = draft.translationStatus.en;
|
||
|
||
return (
|
||
<div className="fixed inset-0 z-50 flex items-end justify-center bg-black/40 p-2 backdrop-blur-sm sm:items-center">
|
||
<div className="glass flex max-h-[92vh] w-full max-w-lg flex-col gap-4 overflow-y-auto rounded-3xl p-4">
|
||
<div className="flex items-center justify-between">
|
||
<h3 className="display text-lg font-semibold">{t('edit')}</h3>
|
||
<button onClick={onClose} className="glass-pill rounded-full px-3 py-1">
|
||
✕
|
||
</button>
|
||
</div>
|
||
|
||
{/* Name ES/EN */}
|
||
<Field label={t('name')}>
|
||
<div className="flex flex-col gap-2">
|
||
<LocaleInput
|
||
prefix="ES"
|
||
value={draft.name.es ?? ''}
|
||
onChange={(v) => set('name', { ...draft.name, es: v })}
|
||
/>
|
||
<div className="flex items-center gap-2">
|
||
<LocaleInput
|
||
prefix="EN"
|
||
value={draft.name.en ?? ''}
|
||
onChange={(v) =>
|
||
setDraft((d) => ({
|
||
...d,
|
||
name: { ...d.name, en: v },
|
||
translationStatus: { ...d.translationStatus, en: 'reviewed' },
|
||
}))
|
||
}
|
||
/>
|
||
</div>
|
||
<TranslateRow
|
||
loading={translating === 'name'}
|
||
status={enStatus}
|
||
onClick={() => translate('name')}
|
||
/>
|
||
</div>
|
||
</Field>
|
||
|
||
{/* Description ES/EN */}
|
||
<Field label={t('description')}>
|
||
<div className="flex flex-col gap-2">
|
||
<textarea
|
||
value={draft.description.es ?? ''}
|
||
onChange={(e) => set('description', { ...draft.description, es: e.target.value })}
|
||
placeholder="ES"
|
||
rows={2}
|
||
className="glass-soft rounded-2xl px-3 py-2 text-sm outline-none"
|
||
/>
|
||
<textarea
|
||
value={draft.description.en ?? ''}
|
||
onChange={(e) =>
|
||
setDraft((d) => ({
|
||
...d,
|
||
description: { ...d.description, en: e.target.value },
|
||
translationStatus: { ...d.translationStatus, en: 'reviewed' },
|
||
}))
|
||
}
|
||
placeholder="EN"
|
||
rows={2}
|
||
className="glass-soft rounded-2xl px-3 py-2 text-sm outline-none"
|
||
/>
|
||
<TranslateRow
|
||
loading={translating === 'description'}
|
||
status={enStatus}
|
||
onClick={() => translate('description')}
|
||
/>
|
||
</div>
|
||
</Field>
|
||
|
||
{/* Price + Section */}
|
||
<div className="flex gap-3">
|
||
<Field label={t('price')} className="flex-1">
|
||
<input
|
||
type="number"
|
||
step="0.01"
|
||
min={0}
|
||
value={draft.price}
|
||
onChange={(e) => set('price', Number(e.target.value))}
|
||
className="glass-soft w-full rounded-2xl px-3 py-2 outline-none"
|
||
/>
|
||
</Field>
|
||
<Field label={t('section')} className="flex-1">
|
||
<select
|
||
value={draft.sectionId}
|
||
onChange={(e) => set('sectionId', e.target.value)}
|
||
className="glass-soft w-full rounded-2xl px-3 py-2 outline-none"
|
||
>
|
||
{sections.map((s) => (
|
||
<option key={s.id} value={s.id}>
|
||
{s.name.es || s.name.en}
|
||
</option>
|
||
))}
|
||
</select>
|
||
</Field>
|
||
</div>
|
||
|
||
{/* Media */}
|
||
<Field label={t('media')}>
|
||
<div className="flex flex-wrap gap-2">
|
||
{draft.media.map((m, i) => (
|
||
<div key={i} className="relative h-20 w-20 overflow-hidden rounded-xl bg-white/40">
|
||
{m.type === 'video' ? (
|
||
// eslint-disable-next-line jsx-a11y/media-has-caption
|
||
<video src={m.path} className="h-full w-full object-cover" muted />
|
||
) : (
|
||
// eslint-disable-next-line @next/next/no-img-element
|
||
<img src={m.path} alt="" className="h-full w-full object-cover" />
|
||
)}
|
||
<button
|
||
onClick={() => removeMedia(i)}
|
||
className="absolute right-0 top-0 bg-black/50 px-1 text-xs text-white"
|
||
>
|
||
✕
|
||
</button>
|
||
<div className="absolute bottom-0 flex w-full justify-between bg-black/40 text-xs text-white">
|
||
<button onClick={() => moveMedia(i, -1)} className="px-1">
|
||
‹
|
||
</button>
|
||
<button onClick={() => moveMedia(i, 1)} className="px-1">
|
||
›
|
||
</button>
|
||
</div>
|
||
</div>
|
||
))}
|
||
<label className="flex h-20 w-20 cursor-pointer items-center justify-center rounded-xl border border-dashed border-[var(--accent)]/40 text-2xl text-[var(--accent)]">
|
||
{uploading ? '…' : '+'}
|
||
<input
|
||
type="file"
|
||
accept="image/*,video/*"
|
||
multiple
|
||
hidden
|
||
onChange={(e) => upload(e.target.files)}
|
||
/>
|
||
</label>
|
||
</div>
|
||
</Field>
|
||
|
||
{/* Macros */}
|
||
<Field label="Macros">
|
||
<div className="grid grid-cols-3 gap-2">
|
||
{MACRO_KEYS.map((key) => (
|
||
<label key={key} className="flex flex-col gap-1 text-xs">
|
||
{tm(key)}
|
||
<input
|
||
type="number"
|
||
min={0}
|
||
value={draft.macros[key] ?? ''}
|
||
onChange={(e) =>
|
||
set('macros', {
|
||
...draft.macros,
|
||
[key]: e.target.value === '' ? undefined : Number(e.target.value),
|
||
} as Record<string, number>)
|
||
}
|
||
className="glass-soft rounded-xl px-2 py-1.5 outline-none"
|
||
/>
|
||
</label>
|
||
))}
|
||
</div>
|
||
</Field>
|
||
|
||
{/* Allergens */}
|
||
<Field label={t('allergens')}>
|
||
<Chips
|
||
options={ALLERGENS}
|
||
selected={draft.allergens}
|
||
onToggle={(v) => set('allergens', toggleIn(draft.allergens, v))}
|
||
/>
|
||
</Field>
|
||
|
||
{/* Diet tags */}
|
||
<Field label={t('dietTags')}>
|
||
<Chips
|
||
options={DIET_TAGS}
|
||
selected={draft.dietTags}
|
||
onToggle={(v) => set('dietTags', toggleIn(draft.dietTags, v))}
|
||
/>
|
||
</Field>
|
||
|
||
<div className="flex gap-2 pt-1">
|
||
<button
|
||
onClick={() => onDelete(draft)}
|
||
className="rounded-2xl bg-red-100 px-4 py-3 text-sm font-medium text-red-700"
|
||
>
|
||
{t('delete')}
|
||
</button>
|
||
<button
|
||
onClick={save}
|
||
disabled={saving}
|
||
className="flex-1 rounded-2xl bg-[var(--accent)] py-3 font-medium text-white disabled:opacity-50"
|
||
>
|
||
{t('save')}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
function Field({
|
||
label,
|
||
children,
|
||
className = '',
|
||
}: {
|
||
label: string;
|
||
children: React.ReactNode;
|
||
className?: string;
|
||
}) {
|
||
return (
|
||
<div className={`flex flex-col gap-1.5 ${className}`}>
|
||
<span className="text-xs font-medium text-[var(--text)]/60">{label}</span>
|
||
{children}
|
||
</div>
|
||
);
|
||
}
|
||
|
||
function LocaleInput({
|
||
prefix,
|
||
value,
|
||
onChange,
|
||
}: {
|
||
prefix: string;
|
||
value: string;
|
||
onChange: (v: string) => void;
|
||
}) {
|
||
return (
|
||
<div className="glass-soft flex items-center gap-2 rounded-2xl px-3">
|
||
<span className="text-[10px] font-bold text-[var(--text)]/40">{prefix}</span>
|
||
<input
|
||
value={value}
|
||
onChange={(e) => onChange(e.target.value)}
|
||
className="flex-1 bg-transparent py-2 text-sm outline-none"
|
||
/>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
function TranslateRow({
|
||
loading,
|
||
status,
|
||
onClick,
|
||
}: {
|
||
loading: boolean;
|
||
status?: 'auto' | 'reviewed';
|
||
onClick: () => void;
|
||
}) {
|
||
const t = useTranslations('editor');
|
||
return (
|
||
<div className="flex items-center gap-2">
|
||
<button
|
||
onClick={onClick}
|
||
disabled={loading}
|
||
className="glass-pill rounded-full px-3 py-1 text-xs font-medium disabled:opacity-50"
|
||
>
|
||
{loading ? '…' : `✨ ${t('translateAi')}`}
|
||
</button>
|
||
{status && (
|
||
<span
|
||
className={`rounded-full px-2 py-0.5 text-[10px] ${
|
||
status === 'auto' ? 'bg-amber-100 text-amber-700' : 'bg-green-100 text-green-700'
|
||
}`}
|
||
>
|
||
{status === 'auto' ? t('autoTranslation') : t('reviewedTranslation')}
|
||
</span>
|
||
)}
|
||
</div>
|
||
);
|
||
}
|
||
|
||
function Chips({
|
||
options,
|
||
selected,
|
||
onToggle,
|
||
}: {
|
||
options: string[];
|
||
selected: string[];
|
||
onToggle: (v: string) => void;
|
||
}) {
|
||
return (
|
||
<div className="flex flex-wrap gap-2">
|
||
{options.map((o) => (
|
||
<button
|
||
key={o}
|
||
onClick={() => onToggle(o)}
|
||
className={`rounded-full px-3 py-1 text-xs transition ${
|
||
selected.includes(o)
|
||
? 'bg-[var(--accent)] text-white'
|
||
: 'glass-soft text-[var(--text)]/70'
|
||
}`}
|
||
>
|
||
{o}
|
||
</button>
|
||
))}
|
||
</div>
|
||
);
|
||
}
|