Add resources defined in config and a bundle of the site's own screens
This commit is contained in:
1 parent
37a29d9195
commit
3345797141
35 files changed
+905
-19
No files matched your search
@@ -0,0 +1,35 @@
|
||||
<script setup lang="ts">
|
||||
import { 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<{
|
||||
resource: { key: string; label: string; singular: string; fields: Field[] };
|
||||
record: { id: number | null; values: Record<string, any> };
|
||||
}>();
|
||||
|
||||
const base = `${boot.base}/records/${props.resource.key}`;
|
||||
const form = useForm({ ...props.record.values });
|
||||
|
||||
async function submit(): Promise<void> {
|
||||
const saved = props.record.id === null ? await form.send('post', base) : await form.send('put', `${base}/${props.record.id}`);
|
||||
|
||||
if (saved) {
|
||||
flash(t('Saved.'));
|
||||
void navigate(base);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="mb-6 flex items-center justify-between gap-3">
|
||||
<h1 class="font-display text-2xl font-bold">{{ record.id === null ? t('New') + ': ' + t(resource.singular) : t(resource.singular) }}</h1>
|
||||
<Link :href="base" 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">
|
||||
<FormFields :fields="resource.fields" :values="form.values" :errors="form.errors" />
|
||||
<button type="submit" class="m-btn m-btn-primary" :disabled="form.busy">{{ form.busy ? t('Saving…') : t('Save') }}</button>
|
||||
</form>
|
||||
</template>
|
||||
@@ -0,0 +1,68 @@
|
||||
<script setup lang="ts">
|
||||
import { api, boot } from '../api';
|
||||
import type { Field } from '../components/FormFields.vue';
|
||||
import Link from '../components/Link.vue';
|
||||
import { t } from '../i18n';
|
||||
import { reload } from '../state';
|
||||
|
||||
const props = defineProps<{
|
||||
resource: { key: string; label: string; singular: string; columns: string[]; fields: Field[]; can_manage: boolean };
|
||||
rows: { id: number; values: Record<string, any> }[];
|
||||
current_page: number;
|
||||
last_page: number;
|
||||
}>();
|
||||
|
||||
const base = `${boot.base}/records/${props.resource.key}`;
|
||||
const labelOf = (key: string) => props.resource.fields.find((field) => field.key === key)?.label ?? key;
|
||||
|
||||
function shown(row: { values: Record<string, any> }, key: string): string {
|
||||
const value = row.values[key];
|
||||
const field = props.resource.fields.find((f) => f.key === key);
|
||||
|
||||
if (field?.type === 'toggle') {
|
||||
return value ? '✓' : '';
|
||||
}
|
||||
|
||||
return field?.type === 'select' ? (field.options[value] ?? value) : String(value ?? '');
|
||||
}
|
||||
|
||||
async function remove(row: { id: number }): Promise<void> {
|
||||
if (!window.confirm(t('Are you sure?'))) {
|
||||
return;
|
||||
}
|
||||
|
||||
await api.delete(`${base}/${row.id}`);
|
||||
await reload();
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="mb-6 flex items-center justify-between gap-3">
|
||||
<h1 class="font-display text-2xl font-bold">{{ t(resource.label) }}</h1>
|
||||
<Link v-if="resource.can_manage" :href="base + '/create'" class="m-btn m-btn-primary">{{ t('Add') }}</Link>
|
||||
</div>
|
||||
<p v-if="!rows.length" class="m-card p-5 text-sm text-muted">{{ t('Nothing here yet.') }}</p>
|
||||
<div v-else class="m-card overflow-x-auto">
|
||||
<table class="w-full text-left text-sm">
|
||||
<thead class="border-b border-line text-xs uppercase tracking-wide text-muted">
|
||||
<tr>
|
||||
<th v-for="column in resource.columns" :key="column" class="px-4 py-3 font-semibold">{{ t(labelOf(column)) }}</th>
|
||||
<th v-if="resource.can_manage" class="px-4 py-3" />
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="divide-y divide-line">
|
||||
<tr v-for="row in rows" :key="row.id">
|
||||
<td v-for="column in resource.columns" :key="column" class="max-w-xs truncate px-4 py-3">{{ shown(row, column) }}</td>
|
||||
<td v-if="resource.can_manage" class="flex justify-end gap-2 px-4 py-2">
|
||||
<Link :href="`${base}/${row.id}/edit`" class="m-btn m-btn-secondary">{{ t('Edit') }}</Link>
|
||||
<button type="button" class="m-btn m-btn-danger" @click="remove(row)">{{ t('Delete') }}</button>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<nav v-if="last_page > 1" class="mt-4 flex gap-2">
|
||||
<Link v-if="current_page > 1" :href="`${base}?page=${current_page - 1}`" class="m-btn m-btn-secondary">{{ t('Previous') }}</Link>
|
||||
<Link v-if="current_page < last_page" :href="`${base}?page=${current_page + 1}`" class="m-btn m-btn-secondary">{{ t('Next') }}</Link>
|
||||
</nav>
|
||||
</template>
|
||||
Reference in new issue
Block a user