Files
lucent-laravel/front/js/entry/SchemaEntry/SchemaEntry.svelte
T

139 lines
4.8 KiB
Svelte
Raw Normal View History

2026-01-07 23:49:55 +02:00
<script>
2026-01-08 18:50:32 +02:00
import SchemaLayout from "../../layouts/SchemaLayout.svelte";
2026-01-08 15:19:08 +02:00
import Sortable from "../../common/Sortable.svelte";
2026-01-07 23:49:55 +02:00
import { post } from "../../modules/remote";
2026-01-08 13:10:18 +02:00
import { getApp } from "../../app";
2026-01-07 23:49:55 +02:00
let { channel, user, data } = $props();
let newSchemaName = $state("");
let newSchemaAlias = $state("");
2026-01-08 15:19:08 +02:00
let fields = $state(data.fields);
2026-01-08 13:10:18 +02:00
const app = getApp();
2026-01-10 02:09:50 +02:00
const createFieldUrl = (schema, type) =>
app.url(`fields/create?schema=${schema.id}&type=${type}`);
2026-01-07 23:49:55 +02:00
function handleSchemaCreate(e) {
e.preventDefault();
post(
channel.lucentUrl + "/schemas",
{
name: newSchemaName,
alias: newSchemaAlias,
},
(data, err) => {
if (err.isEmpty()) {
2026-01-08 13:10:18 +02:00
Turbo.visit(window.location.href);
2026-01-07 23:49:55 +02:00
}
},
);
}
2026-01-08 15:19:08 +02:00
function handleSortUpdate(updatedFields) {
let updatedFieldIds = updatedFields.map((f) => f.id);
fields = fields.filter((f) => !updatedFieldIds.includes(f.id));
fields = [...fields, ...updatedFields];
post(
channel.lucentUrl + "/fields/reorder",
{
ids: updatedFieldIds,
},
(data, err) => {},
);
}
2026-01-07 23:49:55 +02:00
</script>
2026-01-08 18:50:32 +02:00
<SchemaLayout {body} {channel} {user}></SchemaLayout>
2026-01-07 23:49:55 +02:00
{#snippet body()}
<h3 class="header-small mb-4 mt-5">Schemas</h3>
<details style="max-width: 400px;">
<summary>Create Schema</summary>
<form onsubmit={handleSchemaCreate}>
<fieldset>
<label>
Name
<input
bind:value={newSchemaName}
2026-01-07 23:54:55 +02:00
placeholder="ex. Blog Posts"
2026-01-07 23:49:55 +02:00
minlength="2"
maxlength="30"
required
/>
2026-01-07 23:54:55 +02:00
<small id="alias-helper">Plural is recommended</small>
2026-01-07 23:49:55 +02:00
</label>
<label>
Alias
<input
bind:value={newSchemaAlias}
2026-01-07 23:54:55 +02:00
placeholder="ex. blog_posts"
2026-01-07 23:49:55 +02:00
minlength="2"
maxlength="30"
required
2026-01-07 23:54:55 +02:00
aria-describedby="alias-helper"
2026-01-07 23:49:55 +02:00
/>
2026-01-07 23:54:55 +02:00
<small id="alias-helper">
Developers will use this to reference the field
</small>
2026-01-07 23:49:55 +02:00
</label>
</fieldset>
<button type="submit">Create</button>
</form>
</details>
<div style="display: flex;gap:20px">
{#each data.schemas as schema}
<article style="min-width: 300px;">
2026-01-08 15:19:08 +02:00
<header>
<a href={app.url("schemas/edit/" + schema.id)}
>{schema.name}</a
>
</header>
2026-01-07 23:49:55 +02:00
<details>
<summary>Fields</summary>
<table>
2026-01-08 15:19:08 +02:00
<Sortable
type="table"
onUpdate={handleSortUpdate}
items={fields.filter(
(field) => field.schemaId === schema.id,
)}
>
{#snippet itemView(field)}
<td
><a
href={app.url(
"fields/edit/" + field.id,
)}>{field.name}</a
></td
>
<td>{field.type}</td>
{/snippet}
</Sortable>
2026-01-07 23:49:55 +02:00
</table>
</details>
<details>
<summary>Add field</summary>
<ul>
<li>
2026-01-10 02:09:50 +02:00
<a href={createFieldUrl(schema, "text")}>Text</a>
2026-01-07 23:49:55 +02:00
</li>
<li>
2026-01-10 02:09:50 +02:00
<a href={createFieldUrl(schema, "file")}>File</a>
2026-01-07 23:49:55 +02:00
</li>
<li>
2026-01-10 02:09:50 +02:00
<a href={createFieldUrl(schema, "relation")}>
Relation
</a>
2026-01-07 23:49:55 +02:00
</li>
</ul>
</details>
<footer>
<small> Id: {schema.id}</small><br />
<small> Alias: {schema.alias}</small><br />
<small> Revisions: {schema.revisions}</small><br />
</footer>
</article>
{/each}
</div>
{/snippet}