diff --git a/app/src/app/admin/settings/page.tsx b/app/src/app/admin/settings/page.tsx index fc6d083..a6f812b 100644 --- a/app/src/app/admin/settings/page.tsx +++ b/app/src/app/admin/settings/page.tsx @@ -1,11 +1,11 @@ 'use client' -import { useEffect, useState } from 'react' -import { Input } from '@/components/ui/Input' +import { useEffect, useRef, useState } from 'react' +import { Input, Textarea } from '@/components/ui/Input' import { Button } from '@/components/ui/Button' import { Alert } from '@/components/ui/Alert' -const DEFAULT_SETTINGS = [ +const GENERAL_SETTINGS = [ { key: 'site_name', label: 'Site Name', type: 'text', defaultValue: 'ShopX' }, { key: 'site_description', label: 'Site Description', type: 'text', defaultValue: 'Your online store' }, { key: 'support_email', label: 'Support Email', type: 'email', defaultValue: 'support@example.com' }, @@ -13,12 +13,24 @@ const DEFAULT_SETTINGS = [ { key: 'tax_rate', label: 'Tax Rate (%)', type: 'number', defaultValue: '0' }, ] +const FOOTER_SETTINGS = [ + { key: 'footer_copyright', label: 'Testo copyright', type: 'text', defaultValue: '' }, + { key: 'footer_links', label: '', type: 'textarea', defaultValue: '[]' }, +] + +const ALL_KEYS = [...GENERAL_SETTINGS, ...FOOTER_SETTINGS].map((s) => s.key) + export default function AdminSettingsPage() { const [values, setValues] = useState>({}) const [loading, setLoading] = useState(true) const [saving, setSaving] = useState(false) const [message, setMessage] = useState('') const [error, setError] = useState('') + const [faviconUrl, setFaviconUrl] = useState(null) + const [faviconMsg, setFaviconMsg] = useState('') + const [faviconErr, setFaviconErr] = useState('') + const [uploadingFavicon, setUploadingFavicon] = useState(false) + const faviconRef = useRef(null) useEffect(() => { fetch('/api/admin/settings') @@ -26,76 +38,146 @@ export default function AdminSettingsPage() { .then((data) => { const settings = data.settings || {} const initial: Record = {} - DEFAULT_SETTINGS.forEach((s) => { - initial[s.key] = String((settings[s.key] as string | undefined) ?? s.defaultValue) + ALL_KEYS.forEach((key) => { + const def = [...GENERAL_SETTINGS, ...FOOTER_SETTINGS].find((s) => s.key === key) + initial[key] = String((settings[key] as string | undefined) ?? def?.defaultValue ?? '') }) setValues(initial) + if (settings.favicon_url) setFaviconUrl(settings.favicon_url as string) setLoading(false) }) }, []) - async function saveSetting(key: string, value: string) { - setSaving(true) - setError('') - const res = await fetch('/api/admin/settings', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ key, value }), - }) - setSaving(false) - if (res.ok) { - setMessage('Settings saved!') - setTimeout(() => setMessage(''), 3000) - } else { - const data = await res.json() - setError(data.error || 'Failed to save') - } - } - async function handleSubmit(e: React.FormEvent) { e.preventDefault() setSaving(true) setError('') - - for (const s of DEFAULT_SETTINGS) { + for (const key of ALL_KEYS) { await fetch('/api/admin/settings', { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ key: s.key, value: values[s.key] }), + body: JSON.stringify({ key, value: values[key] }), }) } - setSaving(false) - setMessage('All settings saved!') + setMessage('Impostazioni salvate!') setTimeout(() => setMessage(''), 3000) } + async function handleFaviconUpload(e: React.ChangeEvent) { + const file = e.target.files?.[0] + if (!file) return + setFaviconErr('') + setFaviconMsg('') + setUploadingFavicon(true) + const fd = new FormData() + fd.append('file', file) + const res = await fetch('/api/admin/upload/favicon', { method: 'POST', body: fd }) + const data = await res.json() + setUploadingFavicon(false) + if (!res.ok) { + setFaviconErr(data.error || 'Upload fallito') + } else { + setFaviconUrl(data.url + '?t=' + Date.now()) + setFaviconMsg('Favicon aggiornata!') + setTimeout(() => setFaviconMsg(''), 3000) + } + if (faviconRef.current) faviconRef.current.value = '' + } + if (loading) return
Loading...
return ( -
-

Settings

+
+

Settings

- {error && {error}} - {message && {message}} + {error && {error}} + {message && {message}} -
- {DEFAULT_SETTINGS.map((setting) => ( - setValues((v) => ({ ...v, [setting.key]: e.target.value }))} - /> - ))} - -
- + + {/* Impostazioni generali */} +
+

Generale

+ {GENERAL_SETTINGS.map((s) => ( + setValues((v) => ({ ...v, [s.key]: e.target.value }))} + /> + ))}
+ + {/* Footer */} +
+

Footer

+ setValues((v) => ({ ...v, footer_copyright: e.target.value }))} + /> +
+ +