Add pages with fields, a field schema and the screens to edit them
This commit is contained in:
1 parent
ce1a8a1370
commit
517039b625
19 files changed
+660
-13
No files matched your search
@@ -0,0 +1,68 @@
|
||||
<script setup lang="ts">
|
||||
import { api, boot, useForm } from '../api';
|
||||
import FormFields, { type Field } from '../components/FormFields.vue';
|
||||
import Link from '../components/Link.vue';
|
||||
import { t } from '../i18n';
|
||||
import { flash, navigate } from '../state';
|
||||
|
||||
const props = defineProps<{
|
||||
page: {
|
||||
slug: string; title: string; meta_description: string; defined: boolean; fields: Field[];
|
||||
content: Record<string, any> | null; body: string | null; is_published: boolean | null;
|
||||
};
|
||||
}>();
|
||||
|
||||
const page = props.page;
|
||||
const form = useForm({
|
||||
title: page.title,
|
||||
meta_description: page.meta_description ?? '',
|
||||
...(page.defined ? { content: { ...page.content } } : { body: page.body ?? '', is_published: page.is_published ?? false }),
|
||||
} as Record<string, any>);
|
||||
|
||||
async function submit(): Promise<void> {
|
||||
if (await form.send('put', `${boot.base}/pages/${page.slug}`)) {
|
||||
flash(t('Saved.'));
|
||||
}
|
||||
}
|
||||
|
||||
async function remove(): Promise<void> {
|
||||
if (!window.confirm(t('Are you sure?'))) {
|
||||
return;
|
||||
}
|
||||
|
||||
await api.delete(`${boot.base}/pages/${page.slug}`);
|
||||
void navigate(`${boot.base}/pages`);
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="mb-6 flex items-center justify-between gap-3">
|
||||
<h1 class="font-display text-2xl font-bold">{{ page.title }}</h1>
|
||||
<Link :href="boot.base + '/pages'" class="m-btn m-btn-secondary">{{ t('Back') }}</Link>
|
||||
</div>
|
||||
<form class="m-card max-w-3xl space-y-5 p-5" @submit.prevent="submit">
|
||||
<div>
|
||||
<label for="title" class="m-label">{{ t('Title') }}</label>
|
||||
<input id="title" v-model="form.values.title" required maxlength="255" class="m-input" />
|
||||
<p v-if="form.errors.title" class="m-error">{{ form.errors.title }}</p>
|
||||
</div>
|
||||
<div>
|
||||
<label for="meta" class="m-label">{{ t('Description for search engines') }}</label>
|
||||
<input id="meta" v-model="form.values.meta_description" maxlength="255" class="m-input" />
|
||||
<p v-if="form.errors.meta_description" class="m-error">{{ form.errors.meta_description }}</p>
|
||||
</div>
|
||||
<FormFields v-if="page.defined" :fields="page.fields" :values="form.values.content" :errors="form.errors" prefix="content." />
|
||||
<template v-else>
|
||||
<div>
|
||||
<label for="body" class="m-label">{{ t('Text (Markdown)') }}</label>
|
||||
<textarea id="body" v-model="form.values.body" rows="14" class="m-input font-mono" />
|
||||
<p v-if="form.errors.body" class="m-error">{{ form.errors.body }}</p>
|
||||
</div>
|
||||
<label class="flex items-center gap-2 text-sm"><input v-model="form.values.is_published" type="checkbox" /> {{ t('Publish') }}</label>
|
||||
</template>
|
||||
<div class="flex gap-3">
|
||||
<button type="submit" class="m-btn m-btn-primary" :disabled="form.busy">{{ form.busy ? t('Saving…') : t('Save') }}</button>
|
||||
<button v-if="!page.defined" type="button" class="m-btn m-btn-danger" @click="remove">{{ t('Delete') }}</button>
|
||||
</div>
|
||||
</form>
|
||||
</template>
|
||||
@@ -0,0 +1,46 @@
|
||||
<script setup lang="ts">
|
||||
import { boot, useForm } from '../api';
|
||||
import Link from '../components/Link.vue';
|
||||
import { t } from '../i18n';
|
||||
import { navigate } from '../state';
|
||||
|
||||
defineProps<{ pages: { slug: string; title: string; defined: boolean; saved: boolean; is_published?: boolean; updated_at: string | null }[] }>();
|
||||
|
||||
const form = useForm({ slug: '', title: '' });
|
||||
|
||||
async function create(): Promise<void> {
|
||||
if (await form.send('post', `${boot.base}/pages`)) {
|
||||
void navigate(`${boot.base}/pages/${form.values.slug}`);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<h1 class="font-display mb-6 text-2xl font-bold">{{ t('Pages') }}</h1>
|
||||
<ul class="m-card divide-y divide-line">
|
||||
<li v-for="page in pages" :key="page.slug" class="flex items-center justify-between gap-3 p-4">
|
||||
<div class="min-w-0">
|
||||
<p class="truncate font-semibold">{{ page.title }}</p>
|
||||
<p class="truncate text-xs text-muted">/{{ page.slug }}</p>
|
||||
</div>
|
||||
<div class="flex items-center gap-2">
|
||||
<span v-if="!page.defined" class="m-badge">{{ page.is_published ? t('Published') : t('Draft') }}</span>
|
||||
<Link :href="`${boot.base}/pages/${page.slug}`" class="m-btn m-btn-secondary">{{ t('Edit') }}</Link>
|
||||
</div>
|
||||
</li>
|
||||
<li v-if="!pages.length" class="p-4 text-sm text-muted">{{ t('Nothing here yet.') }}</li>
|
||||
</ul>
|
||||
<form class="m-card mt-6 grid max-w-3xl gap-4 p-5 sm:grid-cols-[1fr_1fr_auto] sm:items-end" @submit.prevent="create">
|
||||
<div>
|
||||
<label for="new-title" class="m-label">{{ t('Title') }}</label>
|
||||
<input id="new-title" v-model="form.values.title" required maxlength="255" class="m-input" />
|
||||
<p v-if="form.errors.title" class="m-error">{{ form.errors.title }}</p>
|
||||
</div>
|
||||
<div>
|
||||
<label for="new-slug" class="m-label">{{ t('Address (slug)') }}</label>
|
||||
<input id="new-slug" v-model="form.values.slug" required maxlength="120" class="m-input" />
|
||||
<p v-if="form.errors.slug" class="m-error">{{ form.errors.slug }}</p>
|
||||
</div>
|
||||
<button type="submit" class="m-btn m-btn-primary" :disabled="form.busy">{{ t('New page') }}</button>
|
||||
</form>
|
||||
</template>
|
||||
Reference in new issue
Block a user