inhaus-plugin-gruppen/resources/js/components/PostComposer.vue
Friederich Loheide 5bdfb8a082 Gruppen-Plugin 1.0.0
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 17:17:10 +00:00

112 lines
6.4 KiB
Vue

<script setup>
import { computed, ref } from 'vue';
import { BarChart3, File as FileGlyph, ImagePlus, LoaderCircle, Paperclip, Plus, X } from '@lucide/vue';
import { Button, PersonAvatar, Switch, http, toast } from '@inhaus/ui';
import { fileSize } from '../lib/format';
const props = defineProps({
group: { type: Object, required: true },
me: { type: Object, required: true },
});
const emit = defineEmits(['created']);
const body = ref('');
const files = ref([]);
const poll = ref(null); // Liste der Antworten oder null
const multiple = ref(false);
const busy = ref(false);
const photoInput = ref(null);
const fileInput = ref(null);
const focused = ref(false);
const canSend = computed(() => !busy.value && (body.value.trim() || files.value.length || (poll.value && poll.value.filter((o) => o.trim()).length >= 2)));
function add(list) {
for (const file of list) {
if (files.value.length >= 10) {
toast.error('Höchstens 10 Dateien je Beitrag.');
break;
}
files.value.push({ file, url: file.type.startsWith('image/') ? URL.createObjectURL(file) : null });
}
focused.value = true;
}
function removeFile(i) {
const [f] = files.value.splice(i, 1);
f?.url && URL.revokeObjectURL(f.url);
}
async function submit() {
busy.value = true;
const data = new FormData();
body.value.trim() && data.append('body', body.value);
files.value.forEach((f) => data.append('files[]', f.file));
if (poll.value) {
poll.value.filter((o) => o.trim()).forEach((o) => data.append('poll[]', o));
data.append('multiple', multiple.value ? '1' : '0');
}
try {
const { post } = await http.post(`/gruppen/${props.group.slug}/beitraege`, data);
emit('created', post);
files.value.forEach((f) => f.url && URL.revokeObjectURL(f.url));
body.value = '';
files.value = [];
poll.value = null;
focused.value = false;
} catch (error) {
toast.error(error.message);
} finally {
busy.value = false;
}
}
</script>
<template>
<section class="ih:rounded-xl ih:border ih:bg-card ih:p-4 ih:shadow-xs" aria-label="Neuer Beitrag" @dragover.prevent @drop.prevent="add([...$event.dataTransfer.files])">
<div class="ih:flex ih:gap-3">
<PersonAvatar :person="me" class="ih:size-10" />
<textarea
v-model="body"
:rows="focused || body ? 4 : 1"
maxlength="10000"
:placeholder="`Was möchten Sie mit „${group.name}“ teilen?`"
aria-label="Beitrag schreiben"
class="ih:min-h-10 ih:flex-1 ih:resize-none ih:rounded-2xl ih:bg-muted ih:px-4 ih:py-2 ih:text-[15px] ih:focus-visible:bg-background ih:focus-visible:ring-3 ih:focus-visible:ring-ring/30 ih:focus-visible:outline-none"
@focus="focused = true"
@paste="$event.clipboardData?.files?.length && ($event.preventDefault(), add([...$event.clipboardData.files]))"
/>
</div>
<ul v-if="files.length" class="ih:mt-3 ih:flex ih:flex-wrap ih:gap-2 ih:pl-13">
<li v-for="(f, i) in files" :key="i" class="ih:relative">
<img v-if="f.url" :src="f.url" :alt="f.file.name" class="ih:size-20 ih:rounded-md ih:border ih:object-cover" />
<span v-else class="ih:flex ih:h-20 ih:w-40 ih:items-center ih:gap-2 ih:rounded-md ih:border ih:bg-muted ih:px-2 ih:text-xs"><FileGlyph class="ih:size-5 ih:shrink-0" /><span class="ih:min-w-0"><span class="ih:block ih:truncate ih:font-medium">{{ f.file.name }}</span>{{ fileSize(f.file.size) }}</span></span>
<button type="button" class="ih:absolute ih:-top-1.5 ih:-right-1.5 ih:grid ih:size-5 ih:place-items-center ih:rounded-full ih:bg-foreground ih:text-background" :aria-label="`${f.file.name} entfernen`" @click="removeFile(i)"><X class="ih:size-3" /></button>
</li>
</ul>
<div v-if="poll" class="ih:mt-3 ih:grid ih:gap-2 ih:rounded-lg ih:border ih:p-3 ih:sm:ml-13">
<div class="ih:flex ih:items-center ih:justify-between">
<span class="ih:text-sm ih:font-medium">Umfrage</span>
<button type="button" class="ih:text-xs ih:text-muted-foreground ih:hover:text-foreground" @click="poll = null">Entfernen</button>
</div>
<input v-for="(o, i) in poll" :key="i" v-model="poll[i]" maxlength="120" :placeholder="`Antwort ${i + 1}`" :aria-label="`Antwort ${i + 1}`" class="ih:h-9 ih:rounded-md ih:border ih:border-input ih:bg-background ih:px-3 ih:text-sm" />
<div class="ih:flex ih:flex-wrap ih:items-center ih:justify-between ih:gap-2">
<button v-if="poll.length < 10" type="button" class="ih:inline-flex ih:items-center ih:gap-1 ih:text-sm ih:text-brand-text ih:hover:underline" @click="poll.push('')"><Plus class="ih:size-4" /> Antwort</button>
<label class="ih:flex ih:items-center ih:gap-2 ih:text-sm"><Switch v-model="multiple" /> Mehrere Antworten erlauben</label>
</div>
</div>
<div v-if="focused || body || files.length || poll" class="ih:mt-3 ih:flex ih:flex-wrap ih:items-center ih:gap-1 ih:border-t ih:pt-3">
<button type="button" class="ih:inline-flex ih:items-center ih:gap-1.5 ih:rounded-md ih:px-2.5 ih:py-1.5 ih:text-sm ih:text-muted-foreground ih:hover:bg-accent" @click="photoInput?.click()"><ImagePlus class="ih:size-4" /> Foto</button>
<button type="button" class="ih:inline-flex ih:items-center ih:gap-1.5 ih:rounded-md ih:px-2.5 ih:py-1.5 ih:text-sm ih:text-muted-foreground ih:hover:bg-accent" @click="fileInput?.click()"><Paperclip class="ih:size-4" /> Datei</button>
<button v-if="!poll" type="button" class="ih:inline-flex ih:items-center ih:gap-1.5 ih:rounded-md ih:px-2.5 ih:py-1.5 ih:text-sm ih:text-muted-foreground ih:hover:bg-accent" @click="poll = ['', '']"><BarChart3 class="ih:size-4" /> Umfrage</button>
<span class="ih:flex-1" />
<Button :disabled="!canSend" @click="submit"><LoaderCircle v-if="busy" class="ih:animate-spin" /> Veröffentlichen</Button>
<input ref="photoInput" type="file" accept="image/*" multiple class="ih:hidden" @change="add([...$event.target.files]); $event.target.value = ''" />
<input ref="fileInput" type="file" multiple class="ih:hidden" @change="add([...$event.target.files]); $event.target.value = ''" />
</div>
</section>
</template>