mirror of
https://github.com/randyjc/Rahoot.git
synced 2026-03-13 20:15:35 +01:00
adding option to upload from within theme editor
This commit is contained in:
@@ -24,6 +24,8 @@ const ThemeEditor = ({ onBack }: Props) => {
|
||||
const [customUrl, setCustomUrl] = useState("")
|
||||
const [items, setItems] = useState<MediaItem[]>([])
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [uploading, setUploading] = useState(false)
|
||||
const [uploadError, setUploadError] = useState<string | null>(null)
|
||||
|
||||
const previewUrl = useMemo(
|
||||
() => backgroundUrl || customUrl || background.src,
|
||||
@@ -61,6 +63,34 @@ const ThemeEditor = ({ onBack }: Props) => {
|
||||
handleSet(customUrl.trim())
|
||||
}
|
||||
|
||||
const handleUpload = async (file?: File | null) => {
|
||||
if (!file) return
|
||||
setUploading(true)
|
||||
setUploadError(null)
|
||||
try {
|
||||
const form = new FormData()
|
||||
form.append("file", file)
|
||||
const res = await fetch("/api/media", {
|
||||
method: "POST",
|
||||
body: form,
|
||||
})
|
||||
const data = await res.json()
|
||||
if (!res.ok) {
|
||||
throw new Error(data.error || "Upload failed")
|
||||
}
|
||||
if (data.media?.url) {
|
||||
handleSet(data.media.url)
|
||||
setCustomUrl(data.media.url)
|
||||
}
|
||||
load()
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Upload failed"
|
||||
setUploadError(message)
|
||||
} finally {
|
||||
setUploading(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleReset = () => {
|
||||
reset()
|
||||
setCustomUrl("")
|
||||
@@ -127,6 +157,25 @@ const ThemeEditor = ({ onBack }: Props) => {
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<div className="rounded-md border border-dashed border-gray-300 p-3">
|
||||
<label className="flex items-center gap-3 text-sm font-semibold text-gray-800">
|
||||
<input
|
||||
type="file"
|
||||
accept="image/*"
|
||||
className="text-sm"
|
||||
onChange={(e) => handleUpload(e.target.files?.[0])}
|
||||
disabled={uploading}
|
||||
/>
|
||||
{uploading ? "Uploading..." : "Upload image"}
|
||||
</label>
|
||||
<p className="mt-1 text-xs text-gray-500">
|
||||
Upload a background image (webp/png/jpg). It will be available immediately and selected as the current background.
|
||||
</p>
|
||||
{uploadError && (
|
||||
<p className="mt-1 text-xs font-semibold text-red-600">{uploadError}</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="grid gap-3 sm:grid-cols-2">
|
||||
{items.map((item) => (
|
||||
<button
|
||||
|
||||
Reference in New Issue
Block a user