- Add per-route form components: UserRoleForm, UserForm, ProclamationForm (with category cascade), CategoryForm, FreqForm, LevelForm - Update all admin +page.svelte files to use AdminTable + AdminModal + their specific form component instead of AdminPage - Update profiles page to inline fields instead of using AdminForm - Add custom Proclamation server action with category fallback logic (categoryId falls back to parentCategoryId when no sub-category) - Remove cascade/were/fieldLabels logic from generic crud.ts - Remove outdated cascade/where tests from crud.spec.ts - Remove fieldLabels from AdminModelConfig type
91 lines
2.3 KiB
Svelte
91 lines
2.3 KiB
Svelte
<script lang="ts">
|
|
import type { PageProps } from './$types';
|
|
import type { ActionResult } from '$lib/svelte-admin/types';
|
|
import AdminTable from '$lib/components/admin/AdminTable.svelte';
|
|
import AdminModal from '$lib/components/admin/AdminModal.svelte';
|
|
import Toast from '$lib/components/admin/Toast.svelte';
|
|
import { getModelConfig } from '$lib/svelte-admin/registry';
|
|
import LevelForm from './LevelForm.svelte';
|
|
|
|
let { data, form }: PageProps = $props();
|
|
|
|
const config = getModelConfig('subscription-levels')!;
|
|
const canDelete = $derived((data.adminUser?.roleId ?? 4) <= 2);
|
|
|
|
let modalOpen = $state(false);
|
|
let editingRecord = $state<Record<string, unknown> | null>(null);
|
|
let toastMessage = $state('');
|
|
let toastType = $state<'success' | 'error'>('success');
|
|
let fieldErrors = $state<Record<string, string>>({});
|
|
let formError = $state('');
|
|
|
|
$effect(() => {
|
|
if (form && 'success' in (form as any)) {
|
|
const result = form as any;
|
|
if (result.success) {
|
|
toastMessage = 'Saved successfully';
|
|
toastType = 'success';
|
|
closeModal();
|
|
} else {
|
|
formError = result.error ?? '';
|
|
fieldErrors = result.fieldErrors ?? {};
|
|
}
|
|
}
|
|
});
|
|
|
|
function openCreate() {
|
|
editingRecord = null;
|
|
formError = '';
|
|
fieldErrors = {};
|
|
modalOpen = true;
|
|
}
|
|
|
|
function openEdit(record: Record<string, unknown>) {
|
|
editingRecord = record;
|
|
formError = '';
|
|
fieldErrors = {};
|
|
modalOpen = true;
|
|
}
|
|
|
|
function closeModal() {
|
|
modalOpen = false;
|
|
editingRecord = null;
|
|
formError = '';
|
|
fieldErrors = {};
|
|
}
|
|
</script>
|
|
|
|
<div class="p-6">
|
|
<h1 class="mb-4 text-xl font-bold text-gray-900 dark:text-gray-100">{config.displayName}</h1>
|
|
<AdminTable
|
|
records={data.records}
|
|
listFields={config.listFields}
|
|
searchFields={config.searchFields}
|
|
fieldTypes={config.fieldTypes}
|
|
singular={config.displayNameSingular}
|
|
onRowClick={openEdit}
|
|
onNew={openCreate}
|
|
/>
|
|
</div>
|
|
|
|
<AdminModal
|
|
title={editingRecord
|
|
? `Edit ${config.displayNameSingular}`
|
|
: `Create ${config.displayNameSingular}`}
|
|
open={modalOpen}
|
|
onClose={closeModal}
|
|
{canDelete}
|
|
recordId={editingRecord?.id as string | undefined}
|
|
>
|
|
<LevelForm
|
|
record={editingRecord ?? {}}
|
|
{fieldErrors}
|
|
{formError}
|
|
submitLabel={editingRecord ? 'Update' : 'Create'}
|
|
/>
|
|
</AdminModal>
|
|
|
|
{#if toastMessage}
|
|
<Toast message={toastMessage} type={toastType} />
|
|
{/if}
|