- Add type (Mailing/Billing/Shipping) and isDefault fields to Address schema with migration. Unique default enforced server-side. - Rewrite profiles page with inline address creation form and editable user name/email within the profile modal. - Add upsertAddress and updateUser form actions. - Fix form redirect by adding use:enhance to AdminForm. - Disable Update button in AdminForm until a field is changed (prevents accidental submissions).
328 lines
10 KiB
Svelte
328 lines
10 KiB
Svelte
<script lang="ts">
|
|
import type { PrismaScalarType } from '$lib/svelte-admin/types';
|
|
import { fieldNameToLabel } from '$lib/svelte-admin/introspect';
|
|
import { enhance } from '$app/forms';
|
|
import TipTap from '$lib/components/tiptap/TipTap.svelte';
|
|
|
|
let {
|
|
fields = [],
|
|
fieldTypes = {} as Record<string, PrismaScalarType>,
|
|
relationOptions = {} as Record<
|
|
string,
|
|
{ model: string; labelField: string; valueField: string }
|
|
>,
|
|
relationOptionsData = {} as Record<string, { label: string; value: string | number }[]>,
|
|
record = {} as Record<string, unknown>,
|
|
readonlyFields = [] as string[],
|
|
action = '?/upsert',
|
|
submitLabel = 'Save',
|
|
fieldErrors = {} as Record<string, string>,
|
|
formError = '',
|
|
cascadeFields = {} as Record<string, { parentField: string }>,
|
|
cascadeOptionParentMap = {} as Record<
|
|
string,
|
|
Record<string | number, string | number>
|
|
>
|
|
}: {
|
|
fields: string[];
|
|
fieldTypes: Record<string, PrismaScalarType>;
|
|
relationOptions?: Record<string, { model: string; labelField: string; valueField: string }>;
|
|
relationOptionsData?: Record<string, { label: string; value: string | number }[]>;
|
|
record?: Record<string, unknown>;
|
|
readonlyFields?: string[];
|
|
action?: string;
|
|
submitLabel?: string;
|
|
fieldErrors?: Record<string, string>;
|
|
formError?: string;
|
|
cascadeFields?: Record<string, { parentField: string }>;
|
|
cascadeOptionParentMap?: Record<string, Record<string | number, string | number>>;
|
|
} = $props();
|
|
|
|
let dirty = $state(false);
|
|
|
|
$effect(() => {
|
|
dirty = false;
|
|
});
|
|
|
|
function markDirty() {
|
|
if (record?.id) dirty = true;
|
|
}
|
|
|
|
function isCascadeParent(field: string): boolean {
|
|
return Object.values(cascadeFields).some(
|
|
(c: { parentField: string }) => c.parentField === field
|
|
);
|
|
}
|
|
|
|
function getChildFieldName(parentField: string): string | null {
|
|
for (const [child, cascade] of Object.entries(cascadeFields)) {
|
|
const cfg = cascade as { parentField: string };
|
|
if (cfg.parentField === parentField) return child;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function getFilteredChildOptions(
|
|
childField: string,
|
|
parentValue: string | number | undefined
|
|
): { label: string; value: string | number }[] {
|
|
const all = relationOptionsData[childField] ?? [];
|
|
if (parentValue == null || parentValue === '') return [];
|
|
const parentMap = cascadeOptionParentMap[childField] ?? {};
|
|
const parentNum = Number(parentValue);
|
|
return all.filter(
|
|
(opt: { label: string; value: string | number }) => parentMap[opt.value] === parentNum
|
|
);
|
|
}
|
|
|
|
function formatReadonly(value: unknown, type: string): string {
|
|
if (value == null) return '';
|
|
if (type === 'DateTime') {
|
|
if (value instanceof Date) return value.toLocaleString();
|
|
return new Date(value as string).toLocaleString();
|
|
}
|
|
if (type === 'Boolean') return value ? 'Active' : 'Inactive';
|
|
if (type === 'Int') {
|
|
const n = Number(value);
|
|
return Number.isNaN(n) ? String(value) : String(n);
|
|
}
|
|
if (type === 'Float') {
|
|
const n = Number(value);
|
|
return Number.isNaN(n) ? String(value) : n.toFixed(2);
|
|
}
|
|
return String(value);
|
|
}
|
|
|
|
function formatDateTimeValue(value: unknown): string {
|
|
if (value == null) return '';
|
|
const d = value instanceof Date ? value : new Date(value as string);
|
|
if (isNaN(d.getTime())) return '';
|
|
const pad = (n: number) => String(n).padStart(2, '0');
|
|
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}`;
|
|
}
|
|
</script>
|
|
|
|
<form {action} method="POST" use:enhance oninput={markDirty}>
|
|
{#if record?.id}
|
|
<input type="hidden" name="id" value={String(record.id)} />
|
|
{/if}
|
|
|
|
{#if formError}
|
|
<div class="mb-4 border border-red-300 bg-red-50 px-4 py-2 text-sm text-red-700">
|
|
{formError}
|
|
</div>
|
|
{/if}
|
|
|
|
<div class="flex flex-col gap-4">
|
|
{#each fields as field (field)}
|
|
{@const type = fieldTypes[field] ?? 'String'}
|
|
{@const ro = readonlyFields.includes(field)}
|
|
{@const rel = relationOptions[field]}
|
|
{@const value = record?.[field]}
|
|
{@const err = fieldErrors[field]}
|
|
{@const isCascadeParentField = isCascadeParent(field)}
|
|
{@const isChildField = getChildFieldName(field) != null}
|
|
|
|
{#if isCascadeParentField}
|
|
{@const childField = getChildFieldName(field)!}
|
|
{@const childVal = record?.[childField]}
|
|
{@const childErr = fieldErrors[childField]}
|
|
<div>
|
|
<label
|
|
for="field-{field}"
|
|
class="mb-1 block text-sm font-medium text-gray-700 dark:text-gray-300"
|
|
>
|
|
{fieldNameToLabel(field)}
|
|
</label>
|
|
<select
|
|
id="field-{field}"
|
|
name={field}
|
|
value={value != null ? String(value) : ''}
|
|
class="w-full border px-3 py-1.5 text-sm {err
|
|
? 'border-red-400'
|
|
: 'border-gray-300 dark:border-gray-600'} dark:bg-gray-800 dark:text-gray-100"
|
|
>
|
|
<option value="">-- Select {fieldNameToLabel(field)} --</option>
|
|
{#each relationOptionsData[field] ?? [] as opt (String(opt.value))}
|
|
<option value={opt.value}>{opt.label}</option>
|
|
{/each}
|
|
</select>
|
|
{#if err}
|
|
<p class="mt-1 text-xs text-red-600">{err}</p>
|
|
{/if}
|
|
</div>
|
|
<div>
|
|
<label
|
|
for="field-{childField}"
|
|
class="mb-1 block text-sm font-medium text-gray-700 dark:text-gray-300"
|
|
>
|
|
{fieldNameToLabel(childField)}
|
|
</label>
|
|
<select
|
|
id="field-{childField}"
|
|
name={childField}
|
|
value={childVal != null ? String(childVal) : ''}
|
|
class="w-full border px-3 py-1.5 text-sm {childErr
|
|
? 'border-red-400'
|
|
: 'border-gray-300 dark:border-gray-600'} dark:bg-gray-800 dark:text-gray-100"
|
|
>
|
|
<option value="">-- Select {fieldNameToLabel(childField)} --</option>
|
|
{#each getFilteredChildOptions(childField, value as string | number | undefined) as opt (String(opt.value))}
|
|
<option value={opt.value}>{opt.label}</option>
|
|
{/each}
|
|
</select>
|
|
{#if childErr}
|
|
<p class="mt-1 text-xs text-red-600">{childErr}</p>
|
|
{/if}
|
|
</div>
|
|
{:else if isChildField}
|
|
<!-- child fields rendered inline with parent -->
|
|
{:else if ro}
|
|
<div>
|
|
<label
|
|
for="field-{field}"
|
|
class="mb-1 block text-sm font-medium text-gray-700 dark:text-gray-300"
|
|
>
|
|
{fieldNameToLabel(field)}
|
|
</label>
|
|
<p
|
|
class="border border-gray-200 bg-gray-50 px-3 py-1.5 text-sm text-gray-500 dark:border-gray-700 dark:bg-gray-800 dark:text-gray-400"
|
|
>
|
|
{formatReadonly(value, type)}
|
|
</p>
|
|
</div>
|
|
{:else if type === 'Tiptap'}
|
|
<div>
|
|
<label
|
|
for="field-{field}"
|
|
class="mb-1 block text-sm font-medium text-gray-700 dark:text-gray-300"
|
|
>
|
|
{fieldNameToLabel(field)}
|
|
</label>
|
|
<div id="field-{field}">
|
|
<TipTap initialContent={typeof value === 'string' ? value : ''} name={field} />
|
|
</div>
|
|
{#if err}
|
|
<p class="mt-1 text-xs text-red-600">{err}</p>
|
|
{/if}
|
|
</div>
|
|
{:else if rel}
|
|
<div>
|
|
<label
|
|
for="field-{field}"
|
|
class="mb-1 block text-sm font-medium text-gray-700 dark:text-gray-300"
|
|
>
|
|
{fieldNameToLabel(field)}
|
|
</label>
|
|
<select
|
|
id="field-{field}"
|
|
name={field}
|
|
value={value != null ? String(value) : ''}
|
|
class="w-full border px-3 py-1.5 text-sm {err
|
|
? 'border-red-400'
|
|
: 'border-gray-300 dark:border-gray-600'} dark:bg-gray-800 dark:text-gray-100"
|
|
>
|
|
<option value="">-- Select {fieldNameToLabel(field)} --</option>
|
|
{#each relationOptionsData[field] ?? [] as opt (String(opt.value))}
|
|
<option value={opt.value}>{opt.label}</option>
|
|
{/each}
|
|
</select>
|
|
{#if err}
|
|
<p class="mt-1 text-xs text-red-600">{err}</p>
|
|
{/if}
|
|
</div>
|
|
{:else if type === 'Boolean'}
|
|
<div>
|
|
<label
|
|
for="field-{field}"
|
|
class="mb-1 block text-sm font-medium text-gray-700 dark:text-gray-300"
|
|
>
|
|
{fieldNameToLabel(field)}
|
|
</label>
|
|
<input
|
|
type="checkbox"
|
|
id="field-{field}"
|
|
name={field}
|
|
checked={!!value}
|
|
class="h-4 w-4 border-gray-300 dark:border-gray-600"
|
|
/>
|
|
</div>
|
|
{:else if type === 'DateTime'}
|
|
<div>
|
|
<label
|
|
for="field-{field}"
|
|
class="mb-1 block text-sm font-medium text-gray-700 dark:text-gray-300"
|
|
>
|
|
{fieldNameToLabel(field)}
|
|
</label>
|
|
<input
|
|
type="datetime-local"
|
|
id="field-{field}"
|
|
name={field}
|
|
value={formatDateTimeValue(value)}
|
|
class="w-full border px-3 py-1.5 text-sm {err
|
|
? 'border-red-400'
|
|
: 'border-gray-300 dark:border-gray-600'} dark:bg-gray-800 dark:text-gray-100"
|
|
/>
|
|
{#if err}
|
|
<p class="mt-1 text-xs text-red-600">{err}</p>
|
|
{/if}
|
|
</div>
|
|
{:else if type === 'Int' || type === 'Float'}
|
|
<div>
|
|
<label
|
|
for="field-{field}"
|
|
class="mb-1 block text-sm font-medium text-gray-700 dark:text-gray-300"
|
|
>
|
|
{fieldNameToLabel(field)}
|
|
</label>
|
|
<input
|
|
type="number"
|
|
id="field-{field}"
|
|
name={field}
|
|
value={value != null ? String(value) : ''}
|
|
step={type === 'Int' ? '1' : '0.01'}
|
|
class="w-full border px-3 py-1.5 text-sm {err
|
|
? 'border-red-400'
|
|
: 'border-gray-300 dark:border-gray-600'} dark:bg-gray-800 dark:text-gray-100"
|
|
/>
|
|
{#if err}
|
|
<p class="mt-1 text-xs text-red-600">{err}</p>
|
|
{/if}
|
|
</div>
|
|
{:else}
|
|
<div>
|
|
<label
|
|
for="field-{field}"
|
|
class="mb-1 block text-sm font-medium text-gray-700 dark:text-gray-300"
|
|
>
|
|
{fieldNameToLabel(field)}
|
|
</label>
|
|
<input
|
|
type="text"
|
|
id="field-{field}"
|
|
name={field}
|
|
value={value != null ? String(value) : ''}
|
|
class="w-full border px-3 py-1.5 text-sm {err
|
|
? 'border-red-400'
|
|
: 'border-gray-300 dark:border-gray-600'} dark:bg-gray-800 dark:text-gray-100"
|
|
/>
|
|
{#if err}
|
|
<p class="mt-1 text-xs text-red-600">{err}</p>
|
|
{/if}
|
|
</div>
|
|
{/if}
|
|
{/each}
|
|
|
|
<div class="flex justify-end gap-2 pt-2">
|
|
<button
|
|
type="submit"
|
|
disabled={!!record?.id && !dirty}
|
|
class="border border-gray-300 bg-gray-100 px-4 py-1.5 text-sm font-medium hover:bg-gray-200 disabled:cursor-not-allowed disabled:opacity-50 dark:border-gray-600 dark:bg-gray-800 dark:text-gray-300 dark:hover:bg-gray-700"
|
|
>
|
|
{submitLabel}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</form>
|