145 lines
8.7 KiB
Vue
145 lines
8.7 KiB
Vue
<script setup>
|
||
import { computed, ref } from 'vue';
|
||
import { Link, useForm } from '@inertiajs/vue3';
|
||
import { ChevronLeft, ImagePlus, LoaderCircle, TriangleAlert, X } from '@lucide/vue';
|
||
import { AppLayout, Button, FormField, Input, PageHeader, Panel, Switch, Textarea, toast } from '@inhaus/ui';
|
||
|
||
const props = defineProps({
|
||
listing: { type: Object, default: null },
|
||
types: { type: Object, required: true },
|
||
categories: { type: Object, required: true },
|
||
maxPhotos: { type: Number, default: 8 },
|
||
defaultLocation: { type: String, default: null },
|
||
});
|
||
|
||
const l = props.listing;
|
||
const form = useForm({
|
||
type: l?.type ?? 'sell',
|
||
category: l?.category ?? '',
|
||
title: l?.title ?? '',
|
||
description: l?.description ?? '',
|
||
price: l?.price ?? '',
|
||
negotiable: l?.negotiable ?? false,
|
||
location: l?.location ?? props.defaultLocation ?? '',
|
||
phone: l?.phone ?? '',
|
||
photos: [],
|
||
remove_photos: [],
|
||
});
|
||
|
||
const existing = ref(l?.photos ?? []);
|
||
const previews = ref([]);
|
||
const fileInput = ref(null);
|
||
const withPrice = computed(() => ['sell', 'search'].includes(form.type));
|
||
const photoCount = computed(() => existing.value.length + form.photos.length);
|
||
|
||
function addPhotos(files) {
|
||
for (const file of files) {
|
||
if (!file.type.startsWith('image/')) continue;
|
||
if (photoCount.value >= props.maxPhotos) {
|
||
toast.error(`Höchstens ${props.maxPhotos} Fotos.`);
|
||
break;
|
||
}
|
||
|
||
form.photos.push(file);
|
||
previews.value.push(URL.createObjectURL(file));
|
||
}
|
||
}
|
||
|
||
function removeNew(i) {
|
||
URL.revokeObjectURL(previews.value[i]);
|
||
form.photos.splice(i, 1);
|
||
previews.value.splice(i, 1);
|
||
}
|
||
|
||
function removeExisting(photo) {
|
||
form.remove_photos.push(photo.id);
|
||
existing.value = existing.value.filter((p) => p.id !== photo.id);
|
||
}
|
||
|
||
function submit() {
|
||
form.post(l ? `/kleinanzeigen/${l.id}` : '/kleinanzeigen', { forceFormData: true, preserveScroll: true });
|
||
}
|
||
|
||
const willReview = computed(() => l && ['active', 'rejected', 'expired'].includes(l.status));
|
||
</script>
|
||
|
||
<template>
|
||
<AppLayout :title="l ? 'Anzeige bearbeiten' : 'Anzeige aufgeben'">
|
||
<PageHeader :title="l ? 'Anzeige bearbeiten' : 'Anzeige aufgeben'" :breadcrumbs="[{ label: 'Kleinanzeigen', href: '/kleinanzeigen' }]" />
|
||
|
||
<p v-if="l?.review_note" class="ih:mb-4 ih:flex ih:items-start ih:gap-2 ih:rounded-md ih:border ih:border-destructive/30 ih:bg-destructive/5 ih:p-3 ih:text-sm">
|
||
<TriangleAlert class="ih:mt-0.5 ih:size-4 ih:shrink-0 ih:text-destructive" /> Hinweis der Moderation: {{ l.review_note }}
|
||
</p>
|
||
|
||
<form class="ih:grid ih:max-w-3xl ih:gap-6" @submit.prevent="submit">
|
||
<Panel>
|
||
<div class="ih:grid ih:gap-5">
|
||
<fieldset>
|
||
<legend class="ih:mb-2 ih:text-sm ih:font-medium">Ich …</legend>
|
||
<div class="ih:flex ih:flex-wrap ih:gap-2">
|
||
<label v-for="(label, key) in types" :key="key" class="ih:cursor-pointer ih:rounded-full ih:border ih:px-4 ih:py-1.5 ih:text-sm ih:has-[:checked]:border-brand ih:has-[:checked]:bg-brand ih:has-[:checked]:text-brand-foreground ih:has-[:focus-visible]:ring-3 ih:has-[:focus-visible]:ring-ring/30">
|
||
<input v-model="form.type" type="radio" name="type" :value="key" class="ih:sr-only" />{{ label.toLowerCase() }}
|
||
</label>
|
||
</div>
|
||
</fieldset>
|
||
|
||
<FormField id="ka-title" label="Titel" :error="form.errors.title">
|
||
<Input id="ka-title" v-model="form.title" maxlength="120" placeholder="z. B. Kinderfahrrad 20 Zoll, blau" />
|
||
</FormField>
|
||
|
||
<FormField id="ka-category" label="Kategorie" :error="form.errors.category">
|
||
<select id="ka-category" v-model="form.category" class="ih:h-9 ih:w-full ih:rounded-md ih:border ih:border-input ih:bg-card ih:px-2 ih:text-sm">
|
||
<option value="" disabled>Bitte wählen</option>
|
||
<option v-for="(label, key) in categories" :key="key" :value="key">{{ label }}</option>
|
||
</select>
|
||
</FormField>
|
||
|
||
<FormField id="ka-description" label="Beschreibung" :error="form.errors.description" hint="Zustand, Maße, Abholung – alles, was Interessierte wissen möchten.">
|
||
<Textarea id="ka-description" v-model="form.description" rows="6" maxlength="5000" />
|
||
</FormField>
|
||
|
||
<div v-if="withPrice" class="ih:grid ih:gap-4 ih:sm:grid-cols-2">
|
||
<FormField id="ka-price" :label="form.type === 'search' ? 'Budget (optional)' : 'Preis in € (leer = auf Anfrage)'" :error="form.errors.price">
|
||
<Input id="ka-price" v-model="form.price" inputmode="decimal" placeholder="z. B. 25 oder 12,50" />
|
||
</FormField>
|
||
<label v-if="form.type === 'sell'" class="ih:flex ih:items-center ih:gap-3 ih:self-end ih:pb-2 ih:text-sm"><Switch v-model="form.negotiable" /> Verhandlungsbasis (VB)</label>
|
||
</div>
|
||
|
||
<div class="ih:grid ih:gap-4 ih:sm:grid-cols-2">
|
||
<FormField id="ka-location" label="Ort bzw. Abholung" :error="form.errors.location">
|
||
<Input id="ka-location" v-model="form.location" maxlength="120" placeholder="z. B. Haus A oder Mainz-Gonsenheim" />
|
||
</FormField>
|
||
<FormField id="ka-phone" label="Telefon (optional)" :error="form.errors.phone" hint="Wird in der Anzeige gezeigt.">
|
||
<Input id="ka-phone" v-model="form.phone" type="tel" maxlength="40" />
|
||
</FormField>
|
||
</div>
|
||
</div>
|
||
</Panel>
|
||
|
||
<Panel :title="`Fotos (${photoCount}/${maxPhotos})`">
|
||
<div class="ih:grid ih:grid-cols-3 ih:gap-3 ih:sm:grid-cols-4" @dragover.prevent @drop.prevent="addPhotos([...$event.dataTransfer.files])">
|
||
<div v-for="photo in existing" :key="photo.id" class="ih:relative ih:aspect-[4/3] ih:overflow-hidden ih:rounded-md ih:border">
|
||
<img :src="photo.url" alt="" class="ih:size-full ih:object-cover" />
|
||
<button type="button" class="ih:absolute ih:top-1 ih:right-1 ih:grid ih:size-6 ih:place-items-center ih:rounded-full ih:bg-foreground/75 ih:text-background" aria-label="Foto entfernen" @click="removeExisting(photo)"><X class="ih:size-3.5" /></button>
|
||
</div>
|
||
<div v-for="(url, i) in previews" :key="url" class="ih:relative ih:aspect-[4/3] ih:overflow-hidden ih:rounded-md ih:border">
|
||
<img :src="url" alt="" class="ih:size-full ih:object-cover" />
|
||
<button type="button" class="ih:absolute ih:top-1 ih:right-1 ih:grid ih:size-6 ih:place-items-center ih:rounded-full ih:bg-foreground/75 ih:text-background" aria-label="Foto entfernen" @click="removeNew(i)"><X class="ih:size-3.5" /></button>
|
||
</div>
|
||
<button v-if="photoCount < maxPhotos" type="button" class="ih:flex ih:aspect-[4/3] ih:flex-col ih:items-center ih:justify-center ih:gap-1 ih:rounded-md ih:border-2 ih:border-dashed ih:text-sm ih:text-muted-foreground ih:hover:border-brand ih:hover:text-brand-text" @click="fileInput?.click()">
|
||
<ImagePlus class="ih:size-6" /> Fotos hinzufügen
|
||
</button>
|
||
</div>
|
||
<input ref="fileInput" type="file" accept="image/*" multiple class="ih:hidden" @change="addPhotos([...$event.target.files]); $event.target.value = ''" />
|
||
<p v-if="form.errors.photos || form.errors['photos.0']" class="ih:mt-2 ih:text-[13px] ih:text-destructive">{{ form.errors.photos || form.errors['photos.0'] }}</p>
|
||
<p class="ih:mt-3 ih:text-xs ih:text-muted-foreground">Das erste Foto erscheint in der Übersicht. Fotos sind nur für angemeldete Mitarbeitende sichtbar.</p>
|
||
</Panel>
|
||
|
||
<div class="ih:flex ih:flex-wrap ih:items-center ih:gap-3">
|
||
<Button type="submit" :disabled="form.processing"><LoaderCircle v-if="form.processing" class="ih:animate-spin" /> {{ l ? 'Speichern' : 'Zur Freigabe einreichen' }}</Button>
|
||
<Button variant="ghost" as-child><Link :href="l ? '/kleinanzeigen/meine' : '/kleinanzeigen'"><ChevronLeft /> Abbrechen</Link></Button>
|
||
<p class="ih:text-xs ih:text-muted-foreground">{{ l ? (willReview ? 'Änderungen an Inhalt oder Fotos werden erneut geprüft.' : '') : 'Die Moderation prüft Ihre Anzeige; danach ist sie 30 Tage online.' }}</p>
|
||
</div>
|
||
</form>
|
||
</AppLayout>
|
||
</template>
|