Add screens for accounts, groups, categories and tags, pages and reviews
This commit is contained in:
1 parent
6993ad3557
commit
d14de1d44d
10 files changed
+317
-4
No files matched your search
@@ -0,0 +1,103 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from 'vue';
|
||||
import { api, boot, useForm } from '../api';
|
||||
import { t } from '../i18n';
|
||||
import { flash, reload } from '../state';
|
||||
|
||||
interface Group { id: number; name: string; description: string | null; can_give: boolean }
|
||||
interface Account {
|
||||
id: number; name: string; email: string; is_super_admin: boolean; is_you: boolean;
|
||||
groups: { id: number; name: string }[]; can_change: boolean; can_delete: boolean;
|
||||
}
|
||||
|
||||
defineProps<{ accounts: Account[]; groups: Group[] }>();
|
||||
|
||||
const editing = ref<Account | 'new' | null>(null);
|
||||
const form = useForm({ name: '', email: '', password: '', groups: [] as number[] });
|
||||
|
||||
function open(account: Account | 'new'): void {
|
||||
editing.value = account;
|
||||
form.errors = {};
|
||||
form.values.name = account === 'new' ? '' : account.name;
|
||||
form.values.email = account === 'new' ? '' : account.email;
|
||||
form.values.password = '';
|
||||
form.values.groups = account === 'new' ? [] : account.groups.map((g) => g.id);
|
||||
}
|
||||
|
||||
async function save(): Promise<void> {
|
||||
const account = editing.value;
|
||||
const saved = account === 'new'
|
||||
? await form.send('post', `${boot.base}/accounts`)
|
||||
: await form.send('put', `${boot.base}/accounts/${(account as Account).id}`);
|
||||
|
||||
if (saved) {
|
||||
flash(t('Saved.'));
|
||||
editing.value = null;
|
||||
await reload();
|
||||
}
|
||||
}
|
||||
|
||||
async function remove(account: Account): Promise<void> {
|
||||
if (window.confirm(t('Are you sure?'))) {
|
||||
await api.delete(`${boot.base}/accounts/${account.id}`);
|
||||
flash(t('Deleted.'));
|
||||
await reload();
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="mb-6 flex flex-wrap items-center justify-between gap-3">
|
||||
<h1 class="font-display text-2xl font-bold">{{ t('Accounts') }}</h1>
|
||||
<button type="button" class="m-btn m-btn-primary" @click="open('new')">{{ t('New account') }}</button>
|
||||
</div>
|
||||
|
||||
<form v-if="editing" class="m-card mb-6 max-w-2xl space-y-4 p-5" @submit.prevent="save">
|
||||
<div class="grid gap-4 sm:grid-cols-2">
|
||||
<div>
|
||||
<label class="m-label" for="a-name">{{ t('Name') }}</label>
|
||||
<input id="a-name" v-model="form.values.name" required maxlength="255" class="m-input" />
|
||||
<p v-if="form.errors.name" class="m-error">{{ form.errors.name }}</p>
|
||||
</div>
|
||||
<div>
|
||||
<label class="m-label" for="a-email">{{ t('Email address') }}</label>
|
||||
<input id="a-email" v-model="form.values.email" type="email" required maxlength="255" class="m-input" />
|
||||
<p v-if="form.errors.email" class="m-error">{{ form.errors.email }}</p>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<label class="m-label" for="a-pass">{{ t('Password') }}</label>
|
||||
<input id="a-pass" v-model="form.values.password" type="password" minlength="12" maxlength="200" :required="editing === 'new'" class="m-input" autocomplete="new-password" />
|
||||
<p class="m-help">{{ editing === 'new' ? t('At least 12 characters.') : t('Leave empty to keep the password you have.') }}</p>
|
||||
<p v-if="form.errors.password" class="m-error">{{ form.errors.password }}</p>
|
||||
</div>
|
||||
<fieldset v-if="groups.length">
|
||||
<legend class="m-label">{{ t('Groups') }}</legend>
|
||||
<label v-for="group in groups" :key="group.id" class="flex items-center gap-2 py-0.5 text-sm" :class="group.can_give ? '' : 'opacity-50'">
|
||||
<input v-model="form.values.groups" type="checkbox" :value="group.id" :disabled="!group.can_give" />
|
||||
{{ group.name }}
|
||||
</label>
|
||||
</fieldset>
|
||||
<div class="flex gap-2">
|
||||
<button type="submit" class="m-btn m-btn-primary" :disabled="form.busy">{{ form.busy ? t('Saving…') : t('Save') }}</button>
|
||||
<button type="button" class="m-btn m-btn-secondary" @click="editing = null">{{ t('Cancel') }}</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<ul class="m-card divide-y divide-line">
|
||||
<li v-for="account in accounts" :key="account.id" class="flex flex-wrap items-center justify-between gap-3 p-4">
|
||||
<div class="min-w-0">
|
||||
<p class="truncate font-semibold">{{ account.name }} <span v-if="account.is_you" class="text-xs font-normal text-muted">({{ t('you') }})</span></p>
|
||||
<p class="truncate text-xs text-muted">{{ account.email }}</p>
|
||||
<p class="mt-1 flex flex-wrap gap-1">
|
||||
<span v-if="account.is_super_admin" class="m-badge">{{ t('Super admin') }}</span>
|
||||
<span v-for="group in account.groups" :key="group.id" class="m-badge">{{ group.name }}</span>
|
||||
</p>
|
||||
</div>
|
||||
<div class="flex gap-2">
|
||||
<button v-if="account.can_change" type="button" class="m-btn m-btn-secondary" @click="open(account)">{{ t('Edit') }}</button>
|
||||
<button v-if="account.can_delete" type="button" class="m-btn m-btn-danger" @click="remove(account)">{{ t('Delete') }}</button>
|
||||
</div>
|
||||
</li>
|
||||
</ul>
|
||||
</template>
|
||||
@@ -0,0 +1,97 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from 'vue';
|
||||
import { api, boot, useForm } from '../api';
|
||||
import { t } from '../i18n';
|
||||
import { flash, reload } from '../state';
|
||||
|
||||
interface Perm { value: string; label: string; needs: string[] }
|
||||
interface Group { id: number; name: string; description: string | null; permissions: string[]; accounts: number }
|
||||
|
||||
const props = defineProps<{ groups: Group[]; catalogue: { section: string; permissions: Perm[] }[] }>();
|
||||
|
||||
const editing = ref<Group | 'new' | null>(null);
|
||||
const form = useForm({ name: '', description: '', permissions: [] as string[] });
|
||||
const all = props.catalogue.flatMap((s) => s.permissions);
|
||||
|
||||
function open(group: Group | 'new'): void {
|
||||
editing.value = group;
|
||||
form.errors = {};
|
||||
form.values.name = group === 'new' ? '' : group.name;
|
||||
form.values.description = group === 'new' ? '' : (group.description ?? '');
|
||||
form.values.permissions = group === 'new' ? [] : [...group.permissions];
|
||||
}
|
||||
|
||||
/** A permission that needs others brings them along; the server does the same, this only shows it. */
|
||||
function toggled(perm: Perm): void {
|
||||
if (form.values.permissions.includes(perm.value)) {
|
||||
form.values.permissions = [...new Set([...form.values.permissions, ...perm.needs])];
|
||||
} else {
|
||||
form.values.permissions = form.values.permissions.filter((value) => !all.find((p) => p.value === value)?.needs.includes(perm.value));
|
||||
}
|
||||
}
|
||||
|
||||
async function save(): Promise<void> {
|
||||
const group = editing.value;
|
||||
const saved = group === 'new'
|
||||
? await form.send('post', `${boot.base}/groups`)
|
||||
: await form.send('put', `${boot.base}/groups/${(group as Group).id}`);
|
||||
|
||||
if (saved) {
|
||||
flash(t('Saved.'));
|
||||
editing.value = null;
|
||||
await reload();
|
||||
}
|
||||
}
|
||||
|
||||
async function remove(group: Group): Promise<void> {
|
||||
if (window.confirm(t('Are you sure?'))) {
|
||||
await api.delete(`${boot.base}/groups/${group.id}`);
|
||||
flash(t('Deleted.'));
|
||||
await reload();
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="mb-6 flex flex-wrap items-center justify-between gap-3">
|
||||
<h1 class="font-display text-2xl font-bold">{{ t('Groups') }}</h1>
|
||||
<button type="button" class="m-btn m-btn-primary" @click="open('new')">{{ t('New group') }}</button>
|
||||
</div>
|
||||
|
||||
<form v-if="editing" class="m-card mb-6 max-w-3xl space-y-4 p-5" @submit.prevent="save">
|
||||
<div>
|
||||
<label class="m-label" for="g-name">{{ t('Name') }}</label>
|
||||
<input id="g-name" v-model="form.values.name" required maxlength="60" class="m-input" />
|
||||
<p v-if="form.errors.name" class="m-error">{{ form.errors.name }}</p>
|
||||
</div>
|
||||
<div>
|
||||
<label class="m-label" for="g-desc">{{ t('Description') }}</label>
|
||||
<textarea id="g-desc" v-model="form.values.description" rows="2" maxlength="500" class="m-input" />
|
||||
</div>
|
||||
<fieldset v-for="section in catalogue" :key="section.section" class="rounded-lg border border-line p-3">
|
||||
<legend class="px-1 text-sm font-semibold">{{ section.section }}</legend>
|
||||
<label v-for="perm in section.permissions" :key="perm.value" class="flex items-center gap-2 py-0.5 text-sm">
|
||||
<input v-model="form.values.permissions" type="checkbox" :value="perm.value" @change="toggled(perm)" />
|
||||
{{ perm.label }}
|
||||
</label>
|
||||
</fieldset>
|
||||
<div class="flex gap-2">
|
||||
<button type="submit" class="m-btn m-btn-primary" :disabled="form.busy">{{ form.busy ? t('Saving…') : t('Save') }}</button>
|
||||
<button type="button" class="m-btn m-btn-secondary" @click="editing = null">{{ t('Cancel') }}</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<p v-if="!groups.length" class="m-card p-5 text-sm text-muted">{{ t('Nothing here yet.') }}</p>
|
||||
<ul v-else class="m-card divide-y divide-line">
|
||||
<li v-for="group in groups" :key="group.id" class="flex flex-wrap items-center justify-between gap-3 p-4">
|
||||
<div class="min-w-0">
|
||||
<p class="font-semibold">{{ group.name }} <span class="text-xs font-normal text-muted">· {{ group.accounts }} {{ t('accounts') }}</span></p>
|
||||
<p v-if="group.description" class="text-xs text-muted">{{ group.description }}</p>
|
||||
</div>
|
||||
<div class="flex gap-2">
|
||||
<button type="button" class="m-btn m-btn-secondary" @click="open(group)">{{ t('Edit') }}</button>
|
||||
<button type="button" class="m-btn m-btn-danger" @click="remove(group)">{{ t('Delete') }}</button>
|
||||
</div>
|
||||
</li>
|
||||
</ul>
|
||||
</template>
|
||||
@@ -0,0 +1,66 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from 'vue';
|
||||
import { api, boot, ApiError } from '../api';
|
||||
import { t } from '../i18n';
|
||||
import { flash, reload } from '../state';
|
||||
|
||||
type Label = { name: string; posts: number };
|
||||
defineProps<{ categories: Label[]; tags: Label[] }>();
|
||||
|
||||
const kinds = [
|
||||
{ type: 'category', title: 'Categories' },
|
||||
{ type: 'tag', title: 'Tags' },
|
||||
] as const;
|
||||
const adding = ref<Record<string, string>>({ category: '', tag: '' });
|
||||
const error = ref('');
|
||||
|
||||
async function run(action: () => Promise<unknown>): Promise<void> {
|
||||
error.value = '';
|
||||
try {
|
||||
await action();
|
||||
await reload();
|
||||
} catch (e) {
|
||||
if (e instanceof ApiError && e.status === 422) {
|
||||
error.value = Object.values(e.errors)[0] ?? e.message;
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const add = (type: string) => run(async () => {
|
||||
await api.post(`${boot.base}/labels`, { type, name: adding.value[type] });
|
||||
adding.value[type] = '';
|
||||
flash(t('Created.'));
|
||||
});
|
||||
const rename = (type: string, name: string) => {
|
||||
const to = window.prompt(t('New name'), name);
|
||||
|
||||
return to && to !== name ? run(() => api.put(`${boot.base}/labels`, { type, name, new_name: to })) : undefined;
|
||||
};
|
||||
const remove = (type: string, name: string) =>
|
||||
window.confirm(t('Are you sure?')) ? run(() => api.delete(`${boot.base}/labels`, { type, name })) : undefined;
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<h1 class="font-display mb-6 text-2xl font-bold">{{ t('Categories and tags') }}</h1>
|
||||
<p v-if="error" class="m-error mb-4">{{ error }}</p>
|
||||
<div class="grid gap-6 md:grid-cols-2">
|
||||
<section v-for="kind in kinds" :key="kind.type" class="m-card p-4">
|
||||
<h2 class="mb-3 font-semibold">{{ t(kind.title) }}</h2>
|
||||
<form class="mb-3 flex gap-2" @submit.prevent="add(kind.type)">
|
||||
<input v-model="adding[kind.type]" required maxlength="60" class="m-input" :aria-label="t(kind.title)" />
|
||||
<button type="submit" class="m-btn m-btn-primary">{{ t('Add') }}</button>
|
||||
</form>
|
||||
<ul class="divide-y divide-line">
|
||||
<li v-for="label in kind.type === 'category' ? categories : tags" :key="label.name" class="flex items-center justify-between gap-2 py-2 text-sm">
|
||||
<span class="min-w-0 truncate">{{ label.name }} <span class="text-xs text-muted">· {{ label.posts }}</span></span>
|
||||
<span class="flex gap-1">
|
||||
<button type="button" class="m-btn m-btn-secondary" @click="rename(kind.type, label.name)">{{ t('Rename') }}</button>
|
||||
<button type="button" class="m-btn m-btn-danger" @click="remove(kind.type, label.name)">{{ t('Delete') }}</button>
|
||||
</span>
|
||||
</li>
|
||||
</ul>
|
||||
</section>
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,34 @@
|
||||
<script setup lang="ts">
|
||||
import { computed } from 'vue';
|
||||
import Link from '../components/Link.vue';
|
||||
import { t } from '../i18n';
|
||||
import { state } from '../state';
|
||||
|
||||
// Pages and reviews are still plain lists (a paginated answer), so one read-only screen shows both.
|
||||
const props = defineProps<{ data: Record<string, any>[]; current_page: number; last_page: number; path: string }>();
|
||||
|
||||
const kind = computed(() => (state.path.includes('/reviews') ? 'reviews' : 'pages'));
|
||||
const heading = computed(() => (kind.value === 'reviews' ? 'Reviews' : 'Pages'));
|
||||
const title = (row: Record<string, any>) => (kind.value === 'reviews' ? row.author : row.title);
|
||||
const detail = (row: Record<string, any>) => (kind.value === 'reviews' ? (row.rating ? '★'.repeat(row.rating) : '') : row.slug);
|
||||
const live = (row: Record<string, any>) => (kind.value === 'reviews' ? row.is_visible : row.is_published);
|
||||
const page = (n: number) => `${state.path.split('?')[0]}?page=${n}`;
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<h1 class="font-display mb-6 text-2xl font-bold">{{ t(heading) }}</h1>
|
||||
<p v-if="!props.data.length" class="m-card p-5 text-sm text-muted">{{ t('Nothing here yet.') }}</p>
|
||||
<ul v-else class="m-card divide-y divide-line">
|
||||
<li v-for="row in props.data" :key="row.id" class="flex items-center justify-between gap-3 p-4">
|
||||
<div class="min-w-0">
|
||||
<p class="truncate font-semibold">{{ title(row) }}</p>
|
||||
<p class="truncate text-xs text-muted">{{ detail(row) }}</p>
|
||||
</div>
|
||||
<span class="m-badge">{{ live(row) ? t('Published') : t('Draft') }}</span>
|
||||
</li>
|
||||
</ul>
|
||||
<nav v-if="props.last_page > 1" class="mt-4 flex gap-2">
|
||||
<Link v-if="props.current_page > 1" :href="page(props.current_page - 1)" class="m-btn m-btn-secondary">{{ t('Previous') }}</Link>
|
||||
<Link v-if="props.current_page < props.last_page" :href="page(props.current_page + 1)" class="m-btn m-btn-secondary">{{ t('Next') }}</Link>
|
||||
</nav>
|
||||
</template>
|
||||
Reference in new issue
Block a user