|
| 1 | +<template> |
| 2 | + <div class="container mx-auto px-4 py-8 max-w-5xl"> |
| 3 | + <h1 class="text-2xl font-bold text-accent-primary mb-6">Gallery Admin</h1> |
| 4 | + |
| 5 | + <div class="grid grid-cols-1 gap-8"> |
| 6 | + <div class="bg-fill-secondary border border-separator-primary rounded-xl p-4"> |
| 7 | + <h2 class="text-lg font-semibold text-accent-primary mb-4">Upload new image</h2> |
| 8 | + <form class="flex flex-col gap-3" @submit.prevent="uploadImage" enctype="multipart/form-data"> |
| 9 | + <input ref="fileInput" type="file" accept="image/jpeg,image/jpg,image/png,image/gif,image/webp" |
| 10 | + @change="handleFileSelect" |
| 11 | + class="w-full p-2 rounded bg-transparent border border-separator-primary text-label-primary" |
| 12 | + required /> |
| 13 | + <div v-if="previewUrl" class="w-full"> |
| 14 | + <img :src="previewUrl" alt="Preview" |
| 15 | + class="max-w-full max-h-64 rounded border border-separator-primary" /> |
| 16 | + </div> |
| 17 | + <input v-model="altText" type="text" placeholder="Alt text (optional)" |
| 18 | + class="w-full p-2 rounded bg-transparent border border-separator-primary text-label-primary" /> |
| 19 | + <MainButton button-style="primary" size="M" :label="uploading ? 'Uploading…' : 'Upload'" /> |
| 20 | + </form> |
| 21 | + <p v-if="uploadError" class="text-red-500 mt-2">{{ uploadError }}</p> |
| 22 | + <p v-if="uploadOk" class="text-green-600 mt-2">Uploaded successfully</p> |
| 23 | + </div> |
| 24 | + </div> |
| 25 | + |
| 26 | + <div class="mt-10 bg-fill-secondary border border-separator-primary rounded-xl p-4"> |
| 27 | + <div class="flex items-center justify-between mb-3"> |
| 28 | + <h2 class="text-lg font-semibold text-accent-primary">Gallery Images</h2> |
| 29 | + </div> |
| 30 | + <div v-if="listPending">Loading…</div> |
| 31 | + <div v-else-if="items.length === 0" class="text-label-secondary text-center py-8"> |
| 32 | + No gallery images yet. Upload one above to get started. |
| 33 | + </div> |
| 34 | + <div v-else class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4"> |
| 35 | + <div v-for="item in items" :key="item.id" class="relative group"> |
| 36 | + <div class="aspect-square rounded overflow-hidden bg-fill-tertiary border border-separator-primary"> |
| 37 | + <img :src="`/api/gallery/image/${item.imageFilename}`" :alt="item.altText || 'Gallery image'" |
| 38 | + class="w-full h-full object-cover" /> |
| 39 | + </div> |
| 40 | + <div class="mt-2 text-xs text-label-secondary truncate" v-if="item.altText"> |
| 41 | + {{ item.altText }} |
| 42 | + </div> |
| 43 | + <div class="mt-1 text-xs text-label-secondary"> |
| 44 | + {{ new Date(item.createdAt).toLocaleDateString() }} |
| 45 | + </div> |
| 46 | + <div class="absolute top-2 right-2 opacity-0 group-hover:opacity-100 transition-opacity"> |
| 47 | + <MainButton button-style="ghost" size="S" label="Delete" @click="deleteImage(item.id)" /> |
| 48 | + </div> |
| 49 | + </div> |
| 50 | + </div> |
| 51 | + </div> |
| 52 | + </div> |
| 53 | +</template> |
| 54 | + |
| 55 | +<script setup lang="ts"> |
| 56 | +import { definePageMeta } from '#imports' |
| 57 | +import { ref } from 'vue' |
| 58 | +
|
| 59 | +definePageMeta({ layout: 'default', middleware: ['auth'] }) |
| 60 | +
|
| 61 | +const fileInput = ref<HTMLInputElement | null>(null) |
| 62 | +const selectedFile = ref<File | null>(null) |
| 63 | +const previewUrl = ref<string | null>(null) |
| 64 | +const altText = ref('') |
| 65 | +const uploading = ref(false) |
| 66 | +const uploadError = ref('') |
| 67 | +const uploadOk = ref(false) |
| 68 | +
|
| 69 | +function handleFileSelect(event: Event) { |
| 70 | + const target = event.target as HTMLInputElement |
| 71 | + const file = target.files?.[0] |
| 72 | + if (file) { |
| 73 | + selectedFile.value = file |
| 74 | + // Create preview |
| 75 | + const reader = new FileReader() |
| 76 | + reader.onload = (e) => { |
| 77 | + previewUrl.value = e.target?.result as string |
| 78 | + } |
| 79 | + reader.readAsDataURL(file) |
| 80 | + } |
| 81 | +} |
| 82 | +
|
| 83 | +async function uploadImage() { |
| 84 | + if (!selectedFile.value) { |
| 85 | + uploadError.value = 'Please select an image file' |
| 86 | + return |
| 87 | + } |
| 88 | +
|
| 89 | + uploading.value = true |
| 90 | + uploadError.value = '' |
| 91 | + uploadOk.value = false |
| 92 | +
|
| 93 | + try { |
| 94 | + const formData = new FormData() |
| 95 | + formData.append('image', selectedFile.value) |
| 96 | + if (altText.value.trim()) { |
| 97 | + formData.append('altText', altText.value.trim()) |
| 98 | + } |
| 99 | +
|
| 100 | + const response = await fetch('/api/gallery', { |
| 101 | + method: 'POST', |
| 102 | + body: formData, |
| 103 | + credentials: 'include' |
| 104 | + }) |
| 105 | +
|
| 106 | + if (!response.ok) { |
| 107 | + const error = await response.json() |
| 108 | + throw new Error(error.statusMessage || 'Failed to upload image') |
| 109 | + } |
| 110 | +
|
| 111 | + uploadOk.value = true |
| 112 | + selectedFile.value = null |
| 113 | + previewUrl.value = null |
| 114 | + altText.value = '' |
| 115 | + if (fileInput.value) { |
| 116 | + fileInput.value.value = '' |
| 117 | + } |
| 118 | + await refreshList() |
| 119 | + setTimeout(() => { |
| 120 | + uploadOk.value = false |
| 121 | + }, 3000) |
| 122 | + } catch (e: any) { |
| 123 | + uploadError.value = e?.message || 'Failed to upload image' |
| 124 | + } finally { |
| 125 | + uploading.value = false |
| 126 | + } |
| 127 | +} |
| 128 | +
|
| 129 | +const items = ref< |
| 130 | + Array<{ |
| 131 | + id: string |
| 132 | + imageFilename: string |
| 133 | + altText: string |
| 134 | + createdAt: string |
| 135 | + }> |
| 136 | +>([]) |
| 137 | +const listPending = ref(false) |
| 138 | +const deleting = ref<string | null>(null) |
| 139 | +
|
| 140 | +async function refreshList() { |
| 141 | + listPending.value = true |
| 142 | + try { |
| 143 | + const data = await $fetch('/api/gallery') |
| 144 | + items.value = data || [] |
| 145 | + } catch (error) { |
| 146 | + console.error('Failed to refresh gallery list:', error) |
| 147 | + } finally { |
| 148 | + listPending.value = false |
| 149 | + } |
| 150 | +} |
| 151 | +
|
| 152 | +async function deleteImage(id: string) { |
| 153 | + if (!confirm('Are you sure you want to delete this image?')) { |
| 154 | + return |
| 155 | + } |
| 156 | +
|
| 157 | + deleting.value = id |
| 158 | + try { |
| 159 | + const response = await fetch(`/api/gallery/${id}`, { |
| 160 | + method: 'DELETE', |
| 161 | + credentials: 'include' |
| 162 | + }) |
| 163 | +
|
| 164 | + if (!response.ok) { |
| 165 | + const error = await response.json() |
| 166 | + throw new Error(error.statusMessage || 'Failed to delete image') |
| 167 | + } |
| 168 | +
|
| 169 | + await refreshList() |
| 170 | + } catch (e: any) { |
| 171 | + alert(e?.message || e?.statusMessage || 'Failed to delete image') |
| 172 | + } finally { |
| 173 | + deleting.value = null |
| 174 | + } |
| 175 | +} |
| 176 | +
|
| 177 | +await refreshList() |
| 178 | +</script> |
0 commit comments